Configure Hibernate transaction Management in spring

Source: Internet
Author: User

<!--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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.