Transaction management with PicoContainer and Nanning

zhaozj2021-02-11  181

Transaction management with PicoContainer and Nanning

Transaction Manager, with picocontainer and nanning

Author: Ice Cloud Blog: http: //icecloud.51.netEmail: icecloud (AT) sina.com

PicoContainer is the IOC container of Constructor Injector. Nanning is an implementation of Dynamic AOP. I use Pico as my microcaround in the project, and the AOP needs to be used in some places, the most typical: Transaction Manager.

First consider the premise of the application, a DAO needs to perform database operations and requires transactions. Below is the two interface declarations:

Public interface DAO {void update ();} public interface txmanager {void begin (); void comml (); void rollback ();

If the implementation method is implemented with OO, it may be necessary to make the Sampledao implementation class simultaneously implement two interfaces. According to the principle of a single responsibility, Sampledao should not understand transaction management, or in some cases, there is no need for transaction, this should be possible to shut down the transaction.

Here is an aspect (aspect) to be responsible for transaction processing. Or, the transaction is actually one aspect of service.

However, a problem is that transactions are often related to the database. If you want to control your transaction, you must be able to start the Connection, Session, Transaction, etc. of the transaction. These things must be passed to Service and TxManager simultaneously.

public interface ObjectReference {Object get (); void set (Object obj);} public class DatabaseReference implements ObjectReference {private Connection conn; public DatabaseReference () {// obtain Connection} Objcet get () {return conn somewhere;} void Set (Object Obj) {this.conn = (connection) Obj;}}

TXManager's implementation class can have an instance of a DatabaseReference to get the object related to Connection. Service's instance is also to get the same DatabaseReference so that TXManager controls the same connection.

Examples are as follows, I am using Hibernate's session

public class TxManagerImpl implements TxManager {private Connection conn; public TxManagerImpl (DatabaseReference dref) {this.conn = (Connection) dref;} public void begin () {conn.setAutoCommit (false);} public void commit () {conn.commit ();} public void rollback () {conn.rollback ();}} public class SampleDao implements Dao {Connection conn; public SampleDao (DatabaseReference dref) {this.conn = (Connection) dref.get ();} void update () {Conn.executeQuery ("...");}} There is a tie between DAO and TXManager: DatabaseReference. However, if you want to control your transaction, you also need a control class to place all DAO operations within transaction management.

public class TransactionAspect implements Aspect {Pointcut transactionPointcut = P.all (); TxManager txManager; public TransactionAspect (TxManager transactionManager) {this.txManager = transactionManager;} public void introduce (AspectInstance arg0) {} public void advise (AspectInstance instance) {transactionPointcut .advise (instance, new MethodInterceptor () {public Object invoke (Invocation invocation) throws Throwable {txManager.begin (); try {Object o = invocation.invokeNext (); txManager.commit (); return o;} catch (Exception e) {TXManager.rollback (); throw e;}}});}}

At this time, you can create an example of a call, which requires PicoContainer to be responsible for the creation and management of objects:

MutablePicoContainer pico = new DefaultPicoContainer (new CachingComponentAdapterFactory (new NanningComponentAdapterFactory ())); pico.registerComponentImplementation (TxManager.class, TxManagerImpl.class); pico.registerComponentImplementation (TransactionAspect.class, TransactionAspect.class); pico.registerComponentImplementation (SampleDao.class) ; pico.getComponentInstances (); Dao dao = (Dao) pico.getComponentInstance (SampleDao.class); dao.update (); used above NanningComponentAdapterFactory, nanning is NanoContainer the package provides, Nanning instance to be responsible for integrating PicoContainer.

As can be seen from the log, Begin and Commit appear before and after the execution of DAO, so that we integrate AOP and IOC.

This article requires readers to have certain AOP and IOC knowledge. This article only provides example implementation and provides a thinking. Welcome people who are interested in me to discuss my blog.

Copyright Notice: This article is completed by ice cloud, started 9CBS, the author retains Chinese copyright. No commercial use is not permitted without permission. Welcome to the reprint, but please keep the article and copyright statement complete. For contact, please send an email: Icecloud (at) sina.com

转载请注明原文地址:https://www.9cbs.com/read-4948.html

New Post(0)