Using transactions in Java development

Source: Internet
Author: User

XML, using the TX tag to configure the interceptor to implement transaction two, annotation mode

XML, using the TX tag to configure the interceptor to implement the transaction

In the main configuration is the Tx:advice and aop:config two configuration sections, which implement transaction management in the form of Spring AOP.

Tx:advice the manager that configured the transaction is TransactionManager, and Tx:method also specifies that the transaction is used if the method name matches the "add*" and "get*" methods. Propagation is the level of propagation that sets the transaction. In addition to the "add*" and "get*" methods, the transactions of the other methods are read-only (typically, you set this property to true for transactions that only execute queries, and read-only transactions fail if an update, insert, or DELETE statement occurs)

AOP:CONFIG Specifies a aop:pointcut to refer to the top advice.

This enables the transaction through an AOP interception mechanism, which does not have to be @transactional.

Second, the annotation way

In the configuration file

<!--notifications-
<!--<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
&lt;! &ndash; Communication Behavior &ndash;&gt;
<tx:method name= "delete*" propagation= "REQUIRED" read-only= "false" rollback-for= "Java.lang.Exception" No-rollback-for= "Java.lang.RuntimeException"/>
<tx:method name= "insert*" propagation= "REQUIRED" read-only= "false" rollback-for= "java.lang.RuntimeException"/ >
<tx:method name= "add*" propagation= "REQUIRED" read-only= "false" rollback-for= "Java.lang.RuntimeException"/>
<tx:method name= "create*" propagation= "REQUIRED" read-only= "false" rollback-for= "java.lang.RuntimeException"/ >
<tx:method name= "update*" propagation= "REQUIRED" read-only= "false" rollback-for= "Java.lang.Exception"/>
<tx:method name= "save*" propagation= "REQUIRED" read-only= "false" rollback-for= "Java.lang.Exception"/>
<tx:method name= "find*" propagation= "SUPPORTS" read-only= "true"/>
<tx:method name= "get*" propagation= "SUPPORTS" read-only= "true"/>
<tx:method name= "select*" propagation= "SUPPORTS" read-only= "true"/>
<tx:method name= "query*" propagation= "SUPPORTS" read-only= "true"/>
</tx:attributes>
</tx:advice>
&lt;! &ndash; Facets &ndash;&gt;
<aop:config>
<aop:pointcut id= "transactionpointcut" expression= "Execution (* com.xwg.service.*.* (..))"/>
<aop:advisor pointcut-ref= "Transactionpointcut" advice-ref= "Txadvice"/>
</aop:config>

This part is commented out, and then the method above adds @transactional annotations on it,

If you add this annotation to the class, it means that all of the methods under this class enable transactions

Using transactions in Java development

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.