The basic environment used: SPRINGMVC, MyBatis, MySQL, Tomcat
Spring transaction management breaks down the disadvantages of traditional global transaction management and local transaction management, making it possible to use a unified transaction management model in any environment, you can write code once, and then configure different transaction management policies from your code in different environments. Spring offers two transaction management policies: One is declarative transaction management policy and the other is programmatic transaction management policy, which focuses on declarative transaction management policies
As a result of the use of SPRINGMVC, MyBatis, so the unified use of annotations to declare service, Controller
Because the order in which the configuration files are loaded at server startup is web.xml---root-context.xml (Spring's profile)---servlet-context.xml (SPRINGMVC profile), Because the controller in the Root-context.xml configuration file is scanned for assembly first, the service has not yet been enhanced for transaction processing, and will be given the same service (no transaction processing capability), so we have to Root-conte The controller is not scanned in Xt.xml and is configured as follows:
<!--automatic scanning components, here to remove the controller below the controler, they are configured in Spring3-servlet.xml, if not removed will affect the transaction management. -->
<context:component-scan base-package= "com.sence" >
<context:exclude-filter type= " Annotation "expression=" Org.springframework.stereotype.Controller "/>
.</context:component-scan>
<!--automatic scanning components, here to remove the controller below the controler, they are configured in Spring3-servlet.xml, if not removed will affect the transaction management. -->
<context:component-scan base-package= "com.sence" >
<context:exclude-filter type= " Annotation "expression=" Org.springframework.stereotype.Controller "/>
</context:component-scan>
Scan the controller in Servlet-context.xml without scanning the service, configured as follows:
<!--scans all controller but does not scan service-->
<context:component-scan base-package= "Com.sence" >
< Context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
< Context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/> </
Context:component-scan>
<!--scans all controller but does not scan service-->
<context:component-scan Base-package= "Com.sence" >
<context:include-filter type= "annotation" Org.springframework.stereotype.Controller "/>
<context:exclude-filter type=" annotation "expression=" Org.springframework.stereotype.Service "/>
</context:component-scan>
The following can be configured for declarative transaction management, configured as follows:
<!--transaction manager, use Datasourcetransactionmanager--> <bean id= "Txmanager" class= "Org.springframewor" K.jdbc.datasource.datasourcetransactionmanager "> <property name=" DataSource "ref=" DataSource "/> </bean > <!--Spring Declarative transaction Management--> <aop:config> <aop:pointcut id= "Fooservicemeth ODS "expression=" Execution (* com.sence.*.service.impl.*.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Fooservicemethods"/> </aop:config> <tx:advic E id= "Txadvice" transaction-manager= "Txmanager" > <tx:attributes> <tx:method name= "find*" true "/> <tx:method name=" load* read-only= "true"/> <tx:method "*" Name= "rollback-for=" Customexception & Lt;/tx:attributes> </tx:advice> <!--transaction manager, use Datasourcetransactionmanager--> <bea n id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManagEr "> <property name=" dataSource "ref=" DataSource "/> </bean> <!--Spring declarative transaction-Mana Gement--> <aop:config> <aop:pointcut id= "fooservicemethods" expression= "Execution" (* com.sence.*. Service.impl.*.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Fooservicemethods"/> </aop:config> <tx:advice Id= "Txadvice" transaction-manager= "Txmanager" > <tx:attributes> <tx:method name= "find*" read-only= "true"
/> <tx:method name= "load*" read-only= "true"/> <tx:method "*" Name= "rollback-for=" customexception </tx:attributes> </tx:advice>
My configuration is complete, but after my test, when I add objects to the MySQL database table in batches, when one of the objects is wrong, throw customexception transaction but not rollback, this is really a headache, so I continue to find, the steps are as follows:
1. Find out if declarative transaction management error, such as pointcut write wrong
2. Find out if the Controller scan part is configured correctly
But I checked both of these. or the transaction did not roll back, this time I have no way, can only use the ultimate weapon: Check the source, start debug program, found into the transaction, and there was an exception, after the capture into the rollback program, but the database has not rolled back, To avoid the interference of spring's own abstractplatformtransactionmanager, I customized a transaction management class and inherited the Datasourcetransactionmanager class from the configuration file. This can clearly see the trajectory of the program, continue to debug, or an exception, after the capture into the rollback program, but the database did not roll back, now I began to doubt the MySQL database transaction support function, so the Internet to find MySQL support for the transaction, found that MySQL4.0 can support transactions, but the MySQL data table is divided into two categories, one is the traditional data table, the other is the support of the Transaction data table. Data tables that support transactions are divided into two categories: InnoDB and BerkeleyDB
To use the command: Show CREATE TABLE * * * View the properties of my database table to see that my table was originally a traditional type of table, so I used navicat to change the type of the table to be: InnoDB, and then run the program to discover that the transaction was rolled back
To this SPRINGMVC declarative transaction management configuration complete and run correctly