Transaction management and application of Spring framework

Source: Internet
Author: User

Introduction to Spring Framework
The spring framework is an open source project that emerged in the February 2003, and the Open source project originated from the basic code of Rod Johnson's book "Expert One-on-one, development," which was published at the end of 2002. In this book, Rod Johnson advocates the design idea of the Java EE Pragmatism, and the spring framework is the more comprehensive and concrete realization of this idea. The spring framework consists of a container, a framework for configuring and organizing components, and a set of built-in services for transactional, persistent, and Web user interfaces. As a lightweight Java EE framework, Spring provides an effective way to build and organize a Java EE application.
Spring Features
The IoC (inversion of control; controlled inversion), also known as DI (Dependency injection; Dependency injection), is a new programming idea in the object-oriented field and the essence of spring. In short, the IOC means that the relationship between the programs is controlled by the container, not the traditional implementation, which is manipulated directly by the program code. This is the concept of the so-called "inversion of Control": control is transferred from the application code to the external container, and the transfer of control is called reversal. The IOC moves the responsibility to control the creation into the framework and separates it from the application code. When using the spring IOC container, only the object required by the component is indicated, and at runtime the IOC container of spring is provided to it based on the XML configuration data.
The Spring IoC, with its dependency injection design pattern, allows developers to ignore the object's own life cycle and its relationships, and to improve the use of patterns by developers. The management of an object is not difficult, it is difficult to manage the whole object group. Dependency injection allows the container to manage objects, i.e. "Don t call me, I'll call you." The life cycle of the object itself and the relationship between objects are no longer a bother for developers.
Spring AOP, with the help of spring implementation interceptors, enables developers to use enterprise-class services in a reputable manner, such as security services and transactional services. AOP is a reasonable complement to OOP, and with spring AOP, developers can use the Java EE service efficiently.
The spring service abstraction, with the help of various Java EE API abstractions, allows developers to consistently use the Java EE technology, regardless of what the Java EE API is, and with the help of the spring service abstraction, the code is greatly reduced to meet "less code, fewer bugs" software design principles.
Spring ioc+spring aop+spring Service abstraction, together with the formation of spring, such an organism, makes it possible to build a lightweight Java EE.
Transaction management provided by spring
The transaction management that spring provides can be divided into two categories: programmatic and declarative. Programmatic, more flexible, but the code is large, there are more duplicated code, declarative more flexible than the programming type of convenience.
1, traditional use of JDBC transaction management
In the past, using JDBC for data manipulation, using DataSource, getting connection from the data source, we know that the data source is thread-safe, and the connection is not thread-safe, so that each request is re-fetched from the data source for a connection. A generic data source is managed by the container, including the connection pool. These Java EE Commercial containers, such as tomcat,websphere,weblogic, provide this functionality.
In the past, when we used JDBC to write code, transaction management might be:
Connection conn = null;   try{conn = dbconnectionfactory.getconnection; Conn.setautocommit (FALSE); Do something Conn.commit ();   Commit transcation}catch (Exception e) {conn.rollback ();   }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 neglect, 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 these resources are closed, do not throw out the exception, But doing so can lead to extra trouble.
2. The programmatic transaction processing provided by spring
Spring provides several classes for transaction processing:
Transactiondefinition//Transaction Property Definition
Transcationstatus//Represents the current transaction, which can be committed and rolled back.
Platformtransactionmanager This is the underlying interface that spring provides for managing transactions, with an implementation of an abstract class 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) Declare the data source.
(2) Declaring a transaction management class, for example: Datasourcetransactionmanager,hibernatetransactionmanger,jtatransactionmanager, etc.
(3) Add the 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); }
Use the transaction template provided by spring Transactiontemplate:
void Add () {Transactiontemplate.execute (new Transactioncallback () {public Object dointransaction (transactionstatus ts ) {//do sth}}}
Transactiontemplate is also for us to omit some of the transaction commit, rollback code; When defining a transaction template, the transaction management object needs to be injected.
3. Spring Declarative transaction Processing
Spring declarative transaction processing also mainly uses the IOC,AOP idea, provides the Transactioninterceptor interceptor and the commonly used proxy class Transactionproxyfactorybean, can directly carries on the transaction proxy to the component.
Steps to use Transactioninterceptor:
(1) Define a data source, transaction management class
(2) Define a transaction interceptor, for example:
class= "Org.springframework.transaction.interceptor.TransactionInterceptor" >
Com.test.usermanager.*r=propagation_required
(3) Declare a proxy class for the component: Proxyfactorybean
class= "Org.springframework.aop.framework.ProxyFactoryBean" >
Com.test.UserManager
Using Transactionproxyfactorybean:
class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
Propagation_required
Propagation_required
Propagation_required,readonly
Transactionproxyfactorybean just for the component's transaction agent, if we want to add some business aspects of the component validation, and so on, you can use the Transactiontemplate plus interceptors to add multiple interceptors for the component, spring AOP provides three classes of advice, which are pre-enhanced, post-enhanced, and enhanced when throwing exceptions, and can be used flexibly.
Conclusion
Spring can simply incorporate common Java class into transaction management, and declarative transactions are easy to manipulate. With spring, declarative transactions are no longer unique to EJBS, and we don't have to tolerate the inconvenience of EJB to get the functionality of declarative transactions. Spring also provides a unique transaction management abstraction that provides a consistent programming model on top of various underlying transaction management techniques, such as JTA or JDBC.

Transaction management and application of Spring framework

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.