A brief analysis of the transaction management method provided by spring

Source: Internet
Author: User
Tags aop define commit key rollback thread
The transaction management provided by spring can be divided into two categories: programmatic and declarative. Programmatic, more flexible, but a large number of code, there are more than duplicate code, and declarative more flexible than programmatic. This article will discuss the differences between the two transaction management.

   traditional JDBC Transaction management

In the past, when using JDBC for data operations, the general use of DataSource, from the data source to get connection, we know that the data source is thread-safe, and the connection is not thread-safe, so for each request is from the data source to retrieve a connection. A generic data source is managed by a container, including a connection pool. These Java-EE commercial containers, such as tomcat,websphere,weblogic, provide this functionality.

In the past we used JDBC to write code, which might be the case for transaction management:

Connection conn = null;
Try
{
conn = dbconnectionfactory.getconnection;
Conn.setautocommit (FALSE);
Do something
Conn.commit (); Commit Transcation
}
catch (Exception e)
{
Conn.rollback ();
Do STH
}
Finally
{
Try
{
Conn.close ();
}
catch (SQLException se) {//do sth.}
Close Resultset,preparedstatement,connection
Notice:maybe Ocurr Exception when u close rs,pstmt,conn
}
According to the previous ideas to write code, the code is relatively long, and easy to ignore, forget some try/catch, throw some exceptions can not catch, although sometimes we will write Dbtool class, to close these resources, and ensure that when the resources are closed, do not throw out exceptions.

   programmatic transaction processing provided by spring

Spring provides several classes on transaction processing:

· Transactiondefinition//Transaction attribute definition
· Transcationstatus//Represents the current transaction, can be committed, rolled back.
· Platformtransactionmanager This is the basic interface that spring provides for managing transactions, under which there is an abstract class of implementation Abstractplatformtransactionmanager, The transaction management classes we use, such as Datasourcetransactionmanager, are subclasses of this class.

We may use the programmatic transaction management process as follows:

1 Declaring a data source

2 Declare a transaction management class, such as Datasourcetransactionmanager,hibernatetransactionmanger,jtatransactionmanager, etc.

3 Add transaction code to our code:

Transactiondefinition td = New Transactiondefinition ();
Transactionstatus ts = transactionmanager.gettransaction (TD);
Try
{
Do STH
Transactionmanager.commit (TS);
}
catch (Exception e) {transactionmanager.rollback (TS);}
Using the transaction templates provided by spring Transactiontemplate

void Add ()
{
Transactiontemplate.execute (New Transactioncallback () {
Pulic Object dointransaction (transactionstatus ts)
{//do STH}
}
}
Transactiontemplate is also for us to save part of the transaction commit, rollback code, when defining the transaction template, you need to inject the transaction management object.

   Spring Declarative transaction processing

Spring declarative transaction processing also mainly uses the IOC,AOP idea, provides the Transactioninterceptor interceptor and the common proxy class Transactionproxyfactorybean, can directly to the component transaction Proxy.

Using the Transactioninterceptor step

1. Define data source, transaction management class

2. Define the transaction interceptor, such as:

<bean id = "Transactioninterceptor" class= "Org.springframework.transaction.interceptor.TransactionInterceptor"
<property name= "TransactionManager" ><ref bean= "TransactionManager"/> </property>
<property name= "Transactionattributesource"
<value>
Com.test.usermanager.*r=propagation_required
</value>
</property>
</bean>
3. Declare a proxy class for a component: Proxyfactorybean

<bean id= "Usermanager" class= "Org.springframework.aop.framework.ProxyFactoryBean"
<property name= "Proxyinterfaces" > <value> com.test.UserManager </value> </property>
<property name= "Interceptornames"
<list>
<idref local= "Transactioninterceptor"/>
</list>
</property>
</bean>
Use Transactionproxyfactorybean:

<bean id= "Usermanager" class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
<property name= "TransactionManager" ><ref bean= "TransactionManager"/> </property>
<property name= "target" ><ref local= "Usermanagertarget"/> </property>
<property name= "Transactionattributes"
<props>
<prop key= "insert*" >propagation_required </prop>
<prop key= "update*" >propagation_required </prop>
<prop key= "*" >propagation_required,readonly </prop>
</props>
</property>
</bean>
Transactionproxyfactorybean is just a transaction agent for the component, and if we want to add some business-side validation to the component, we can add multiple interceptors to the component using the Transactiontemplate plus interceptor, spring AOP provides three classes of advice, that is, pre-enhancement, post-enhancement, and enhanced when throwing exceptions, and can be used flexibly.

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.