1. Configure common Controller,service, DAO beans.
<!--Configure DAO, service - <BeanID= "Bookshopdao"class= "Com.liujl.spring.tx.xml.BookShopDaoImpl"> < Propertyname= "JdbcTemplate"ref= "JdbcTemplate"></ Property> </Bean> <BeanID= "Bookshopservice"class= "Com.liujl.spring.tx.xml.serivice.impl.BookShopServiceImpl"> < Propertyname= "Bookshopdao"ref= "Bookshopdao"></ Property> </Bean> <BeanID= "Cashier"class= "Com.liujl.spring.tx.xml.serivice.impl.CashierImpl"> < Propertyname= "Bookshopservice"ref= "Bookshopservice"></ Property> </Bean>
2. Configure the transaction manager bean
<!--configuration transaction manager hibernate, JPA are similar to this - <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean>
3. Introduction of the TX namespace
xmlns:tx= "Http://www.springframework.org/schema/tx"
4. Configure the transaction properties (the method name can use wildcard characters *)
<!--Configure transaction Properties - <Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager"> <tx:attributes> <Tx:methodname= "Purchase"Propagation= "REQUIRED"/> <Tx:methodname= "Checkout"Propagation= "REQUIRED"/> <Tx:methodname= "get*"read-only= "true"/> <Tx:methodname= "find*"read-only= "true"/> </tx:attributes> </Tx:advice>
5. Introduction of the AOP namespace
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
6. Configure the tangency point of the transaction and associate the transaction pointcut with the transaction properties
<!--configures the tangency point of a transaction and does not correlate transaction pointcuts with transaction properties - <Aop:config> <Aop:pointcutexpression= "Execution (* com.liujl.spring.tx.xml.serivice.*.* (..))"ID= "Txpointcut"/> <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Txpointcut"/> </Aop:config>
Properties of the transaction:
Propagation Declares the propagation property of a transaction by default, REQUIRED, which is included in the transaction above, discarding itself to handle the transaction Requires_new This method as the execution unit, opening a new transaction ( External transactions are suspended before and after method execution) isolation Specifies the isolation level of the transaction, the most commonly used value is read_committed read and submit
3. By default, spring's declarative transactions are rolled back as long as all runtimes . norollbackfor Specifies the exception that does not roll back, other similar
readOnly Specifies whether a transaction is read- only , which means that the transaction reads only the data but does not update the data, which helps the database engine to optimize the transaction. If it is really a read-only database worthwhile method, you should set Readonly=true
Timeout Specifies the amount of time a transaction can occupy before forcing a rollback
Spring4 declarative Transaction -02 XML configuration method