Spring annotation transactions, which declare transactions to coexist in the case of sequencing

Source: Internet
Author: User

Recently in the project, because a global declarative transaction has been configured. Do not know if there is a problem, resulting in a multi-threaded insertion method will fail because the connection pool is not enough.

You're going to try out the annotated transaction, and then find out that it doesn't work, and you want to be a problem with nested transactions. Then find this buddy's article and solve the problem. A copy is reproduced here

Reprint: http://blog.csdn.net/jeep_ouc/article/details/43056977

COME from

First look at the configuration file:

<!--Hibernate -      <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean">          < Propertyname= "DataSource"ref= "DataSource" />          < Propertyname= "Hibernateproperties">              <Props>                  <propKey= "Hibernate.hbm2ddl.auto">Update</prop>                  <propKey= "Hibernate.show_sql">True</prop>                  <propKey= "Hibernate.format_sql">True</prop>                  <propKey= "Connection.autoreconnect">True</prop>                  <propKey= "Connection.autoreconnectforpools">True</prop>                  <propKey= "Connection.is-connection-validation-required">True</prop>                  <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</prop>              </Props>          </ Property>          < Propertyname= "Mappingdirectorylocations">              <List>                  <value>classpath*:* Oddtech/bean</value>              </List>          </ Property>      </Bean>        <!--Task Management -      <BeanID= "Txmanager"class= "Org.springframework.orm.hibernate3.HibernateTransactionManager">          < Propertyname= "Sessionfactory"ref= "Sessionfactory" />      </Bean>      <!--support for the interpretation of the service -      <Tx:annotation-drivenTransaction-manager= "Txmanager"Order= "0"/>      <!--Service Business Registration section -      <Aop:config>          <Aop:pointcutexpression= "Execution (* oddtech.service.impl.*.* (..))"ID= "Txpoint"  />          <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Txpoint"Order= "1"/>      </Aop:config>                  <Tx:adviceTransaction-manager= "Txmanager"ID= "Txadvice">          <tx:attributes>              <Tx:methodname= "find*"Propagation= "REQUIRED"read-only= "true"rollback-for= "Exception" />              <Tx:methodname= "select*"Propagation= "REQUIRED"read-only= "true"rollback-for= "Exception" />              <Tx:methodname= "insert*"Propagation= "REQUIRED"rollback-for= "Exception" />              <Tx:methodname= "delete*"Propagation= "REQUIRED"rollback-for= "Exception" />              <Tx:methodname= "update*"Propagation= "REQUIRED"rollback-for= "Exception" />              <Tx:methodname= "modify*"Propagation= "REQUIRED"rollback-for= "Exception" />              <Tx:methodname="*"read-only= "true"rollback-for= "Exception" />          </tx:attributes>      </Tx:advice>  

When a method (insert) of a class (Test) under the Oddtech.service.impl package uses @transactional, it is equivalent to executing a new Test (). The Insert () method performs 2 AOP facets. Then we need to define the order of the AOP facets by the Order property. The smaller the order, the more forward in the chain of the AOP, the more executed first. (Chain mode)

So we need to add the Order property to/> in <tx:annotation-driven transaction-manager= "Txmanager" order= "0" 0,<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpoint" order= "1"/> Add the Order property to 1. Then the two are executed in the following order:

。 This is a transaction nesting.

So let's look at the example above: When a Class (Test) is using @transactional in a Oddtech.service.impl package, we want the Insert method to be read-only, Read-only=true, So we need to define this: @Transactional (readOnly = true,propagation=propagation.required), why?

In a declarative transaction, our transaction propagation level for insert is defined as: REQUIRED, for transaction propagation levels, refer to http://blog.csdn.net/feng27156/article/details/ 8534609, we define the required transaction in the annotation transaction. Declares transactions at the transaction level defined by using annotations.

Note Transactions do not conflict with declarative transactions, except in special cases. Declaring a transaction defines a uniform rule, and if you want a particular method to have a special transaction propagation mechanism, then don't conflict with the uniform rules.

<tx:method name= "*" read-only= "true" rollback-for= "Exception"/>

In accordance with the rules, define a test method, add: @Transactional definition. The test method is read-only=false,propagation=required. This is implied. The system <tx:method name= "*" read-only= "true" rollback-for= "Exception"/> does not conflict with the test method's interpretation.

Spring annotation transactions, which declare transactions to coexist in the case of sequencing

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.