<!--Transationmanager -<BeanID= "TransactionManager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager"> < Propertyname= "Sessionfactory"ref= "Sessionfactory"/></Bean><!--Advice -<Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager"> <tx:attributes> <Tx:methodname= "insert*"Propagation= "REQUIRED"/> <Tx:methodname= "delete*"Propagation= "REQUIRED"/> <Tx:methodname= "update*"Propagation= "REQUIRED"/> <Tx:methodname= "select*"Propagation= "SUPPORTS"read-only= "true"/> <Tx:methodname="*"rollback-for= "Java.lang.Exception"Timeout= "+" /> </tx:attributes></Tx:advice><Aop:configProxy-target-class= "true"> <Aop:pointcutID= "Interceptorpointcuts"expression= "Execution (public * com.bai.du.*.service.impl.*.* (..))" /> <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Interceptorpointcuts" /></Aop:config>
The naming rules for transaction-related methods in the service implementation class are defined here, such as the new method, where the method name must begin with insert, the method name must begin with Delete, the method name must begin with the update, and the query method must start with a select. With what start or end this can be customized, only such a method can be managed by spring's transaction, why do you configure the transaction at the service level? This is because the service is defined as a business operation (such as an access payment), and this business operation may require multiple operations of the database, in order to prevent the operation of the database process may be partially successful, partial failure, so put the transaction in the service layer, so that only the database operation is all successful (not reported exception ), the entire transaction is committed.
The above is the use of XML configuration transactions, the advantage is that only one configuration, you can implement all matching transactions, transaction configuration and code height separation, the disadvantage is to follow the defined naming rules, there is another way is to use annotations, only need to add in the XML file:
<transaction-manager= "TransactionManager"/>
and add @transactional () annotations on methods that require transaction management (note that the spring package), and () can also define attributes such as propagation,read-only,rollback-for. The advantage of annotations is that the configuration is more flexible, the XML file configuration is relatively concise, the disadvantage is that the need to add a note, more cumbersome, but also increase the coupling of the code. It is recommended to use XML configuration methods.
Configure Hibernate transaction Management in spring