<title>Ibatis Transaction Processing--blog channel-csdn.net</title>
the Ibatis transaction is closely associated with DAO. ?
when using DAO, insert a new record and then update it, as in the following code: ?
? ? ? ? Userdao.insertuser (user); Starts transaction?
? ? ? ? User.setname ("wh");?
? ? ? ? Userdao.updateuser (user); Starts a new transaction
because the transaction was not explicitly started, Ibatis would consider this to be a two transaction, taking two connection from the connection pool, respectively. each of the DAO subclasses that we wrote (inherited from Com.ibatis.dao.client.template.SqlMapDaoTemplate) has defaulted to a transaction (via dynamic proxy). Such a transaction is an implicit transaction.
? ? ? ? Ibatis is a transaction that governs DAO subclasses through the Daomanager class.
? ? ? ? The many DAO subclasses are produced by Daomanager, as follows:
? ? ? ? Reader Reader =resources.getresourceasreader ("Dao.xml");?
? ? ? ? Daomanager Daomanager =daomanagerbuilder.builddaomanager (reader);? ?
? ? ? ? Userdao Userdao = (Userdao) Daomanager.getdao (Userdao.class);
? ? ? ? Userdao is the user's own definition of the interface, obtained is in the dao.xml specified in the corresponding Sqlmapdao implementation class, thus realizing the loose coupling. In a well-layered design,
?
? ? ? ? However, the general transaction needs to be put into the business layer, because a business needs to be atomic, the transaction is placed in the DAO layer is not consistent with the effect of the business, then if you want to put transactions to the business layer, you need to use display transactions in the business layer to declare processing.
?
? ? ? ? Explicitly declare the Transact-SQL statement, as follows:
? ? ? ? try {?
? ? ? ? ? ? ? ? Daomanager.starttransaction ();?
? ? ? ? ? ? ? ? Userdao.insertuser (user);??
? ? ? ? ? ? ? ? User.setname ("wh");?
? ? ? ? ? ? ? ? Userdao. UpdateUser (user);??
? ? ? ? ? ? ? ? Otherdao.dosomething (other);?
? ? ? ? ? ? ? ? ?...?
? ? ? ? ? ? ? ? Daomanager.committransaction ();?
? ? ? ?} Finally {?
? ? ? ? ? ? ? ? Daomanager.endtransaction ();?
? ? ? ?}
???? This keeps the atomicity, the whole as a transaction, or all execution succeeds, or rollback.
?
???? the only problem now is whether the DAO layer's transactions have been discarded or the resulting transaction nesting problem has an impact on performance. ?
of course, IBatis can do this: build a declarative interface: IService, and then use the dynamic proxy, the user's own serivce subclass through the dynamic proxy automatically package the transaction code, the default each business method is a transaction. ?
The Master 's heart, if it can be easily guessed, is not a master:), it is estimated that the master thought that this is over-design, he thinks it is appropriate to give this flexibility to the user, a considerable number of service methods only invoke a DAO method, such as CRUD operations. ?
Again, the processing of transactions in Ibatis is configurable, the most commonly used type is "JDBC", or it can be declared as "JTA" or "EXTERNAL".
?
In the project, spring is used in conjunction with Ibatis so that transaction management can be configured in spring, eliminating the business layer's display of transaction code.
? ? ? ? The basic wording of the spring configuration file is:
[HTML]View Plaincopyprint?
- <? XML ? version = "1.0" ? encoding = "UTF-8" ?> ??
- < Beansxmlns Beansxmlns = "Http://www.springframework.org/schema/beans" xmlns : XSi = "Http://www.w3.org/2001/XMLSchema-instance" ???
- ??? XMLNS:AOP = "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns : TX = "Http://www.springframework.org/schema/tx" ???
- ??? Xmlns:context = "Http://www.springframework.org/schema/context" ???
- ??? xsi:schemalocation ="????
- ?????????? Http://www.springframework.org/schema/beans?????
- ?????????? http://www.springframework.org/schema/beans/spring-beans-2.0.xsd??
- ?????????? HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP?????
- ?????????? http://www.springframework.org/schema/aop/spring-aop-2.0.xsd????
- ?????????? HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX?????
- ?????????? http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">??
- ???????????
- ???????????
- < Bean ? ID = "DataSource" class = "Org.apache.commons.dbcp.BasicDataSource"? > ??
- ??? < Property ? name = "Driverclassname" value = "Com.mysql.jdbc.Driver"? /> ??
- ??? < Property ? name = "url" value = "Jdbc:mysql://10.11.0.145:3306/carrefour?" characterencoding = gb2312 " /> ??
- ??? < Property ? name = "username" ? value = "DEV01" ? /> ??
- ??? < Property ? name = "Password" ? value = "123456" /> ??
- </ Bean > ??
- < Bean ? ID = "SqlClient" class = "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > ??
- ??? < Property ? name = "DataSource" > ??
- ?????? < ref ? Local = "DataSource" ? /> ??
- ??? </ Property > ??
- ??? < Property ? name = "Configlocation" > ??
- ?????? < value > Classpath:sqlmaps.xml </ value > ??
- ??? </ Property > ??
- </ Bean > ??
- <!--configuration transaction manager - ??
- < Bean ? ID = "TransactionManager" ? class = "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > ??
- ???????? < PropertyName PropertyName = "DataSource" ? ref = "DataSource" > </ Property > ??
- </ Bean > ??
- <!--which methods to configure, and when to roll back-- ??
- < Tx:advice ? ID = "Serviceadvice" Transaction -manager = "TransactionManager" > ???
- ???? < tx:attributes > ????
- ???????? <!--Rollback when the method in the agent's service layer throws an exception, you must add the Rollback-for parameter -- ??
- ???????? < Tx:methodname Tx:methodname = "insert*" ? Propagation = "REQUIRED" rollback -for = "Throwable" /> ??
- ???????? < Tx:methodname Tx:methodname = "del*" ? Propagation = "REQUIRED" rollback -for = "Throwable" /> ???
- ???????? < Tx:methodname Tx:methodname = "update*" ? Propagation = "REQUIRED" rollback -for = "Throwable" /> ???
- ???????? <!--In addition to the method identified above, the other methods are all read-only method- ??
- ???????? < Tx:methodname Tx:methodname = "*" ? read-only = "true" /> ???
- ???? </ tx:attributes > ???
- </ Tx:advice > ???
- <!--? Configure which classes of methods require transaction management?--> ???
- < Aop:config ? Proxy-target-class = "true" > ???
- < Aop:pointcut ? ID = "Servicepointcut" expression = "Execution (*?com.wh.service.*.* (..))" /> ???
- < Aop:advisor ? Pointcut-ref = "Servicepointcut" Advice -ref = "Serviceadvice" /> ???
- </ Aop:config > ??
<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/s Chema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.or G/schema/tx/spring-tx-2.0.xsd "> <bean id=" dataSource "class=" Org.apache.commons.dbcp.BasicDataSource "> <property name=" driverclassname "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" JDBC: Mysql://10.11.0.145:3306/carrefour?characterencoding=gb23"/> <property name=" username "value=" dev01 "/> <property name=" password "value=" 123456 "/></BEAN&G T;<bean id= "sqlClient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > <property name= " DataSource "> <ref local=" dataSource "/> </property> <property name=" Configlocation "> &L t;value>classpath:sqlmaps.xml</value> </property></bean><!--configuration transaction manager--><bean id= " TransactionManager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "> < Propertyname= "DataSource" ref= "DataSource" ></property></bean><!--which methods to configure, and when to roll back--><tx : Advice id= "Serviceadvice" transaction-manager= "TransactionManager" > <tx:attributes> <!--when agent Servi The method in the CE layer does not roll back when it throws an exception, you must add the Rollback-for parameter--<tx:methodname= "insert*" propagation= "REQUIRED" rollback-for= " Throwable "/> <tx:methodname=" del* "propagation=" REQUIRED "rollback-for= "Throwable"/> <tx:methodname= "update*" propagation= "REQUIRED" rollback-for= "Throwable"/> < !--except for the method identified above, all methods are read-only-<tx:methodname= "*" read-only= "true"/> </tx:attributes> </tx:advice > <!--Configure which classes of methods require transaction management--<aop:config proxy-target-class= "true" > <aop:pointcut id= " Servicepointcut "expression=" Execution (* com.wh.service.*.* (..)) " /> <aop:advisor pointcut-ref= "servicepointcut" advice-ref= "Serviceadvice"/> </aop:config>
? ? ? ?? This is in the case of Spring+ibatis, through the AOP control of the package and the specific method that requires the transaction, the transaction is controlled at the service layer, to achieve the transaction at the business level to commit and rollback. Maintain the atomicity of the business.
From for notes (Wiz)
Ibatis Transaction Processing--blog channel-csdn.net