1. xml configuration
<! -- Configure the Transaction Manager --> < Bean ID = "Txmanager" Class = "Org. springframework. JDBC. datasource. datasourcetransactionmanager" > < Property Name = "Datasource" Ref = "Datasource" /> </ Bean > <! -- Configure the service Bean --> < Bean ID = "Personservice" Class = "Com. Persia. Service. impl. personserviceimpl" > < Property Name = "Ds" Ref = "Datasource" > </ Property > </ Bean > <! -- Use XML for Transaction Management --> < AOP : Config > <! -- Configure a plane and the classes and methods to be intercepted --> < AOP : Pointcut ID = "Transactionpointcut" Expression = "Execution (* COM. Persia. Service ..*.*(..))" /> < AOP : Advisor Advice - Ref = "Txadvice" Pointcut - Ref = "Transactionpointcut" /> </ AOP : Config > <! -- Configure a transaction notification --> < TX : Advice ID = "Txadvice" Transaction - Manager = "Txmanager" > < TX : Attributes > <! -- The method starts with get and does not use transactions --> < TX : Method Name = "Get *" Read - Only = "True" Propagation ="Not_supported" /> <! -- Other methods are performed with default transactions --> < TX : Method Name = "*" /> </ TX : Attributes > </ TX : Advice >
2. No annotations are required for the service bean.
Public VoidDelete (integer ID ){// Todo auto-generated method stubJdbctemplate. Update ("Delete from person where id =?",NewObject [] {ID },New Int[] {Java. SQL. types. Integer}); jdbctemplate. Update ("Delete from person1 where id =?",NewObject [] {ID },New Int[] {Java. SQL. types. Integer });// Throw new runtimeexception ("exception during runtime ");}
3. junit4 Testing
@ TestPublic VoidTestdelete () {ps. Delete (5 );}
4. Conclusion:
It can be seen that the xml configuration transaction plays a role, but also uses AOP to add a transaction to the method when blocking the method. .
We recommend that you use annotations to use transactions.