Spring transaction management configuration

Source: Internet
Author: User

Generally, we allocate transactions to the service layer and use the service's business logic interface for unified management.
Why not use the dao layer?
Because a service may call multiple dao, and these dao may be related to each other, sometimes an operation needs to call the database multiple times, but multiple calls may be submitted completely, or full rollback.
Therefore, calling transactions at the dao layer is theoretically not a wise choice. The service layer of the business logic layer should be responsible for transactions and unified processing.


The transaction configuration in the Spring configuration file is always composed of three parts: DataSource, TransactionManager, and proxy mechanism,
Regardless of the configuration method, the proxy mechanism is generally changed.
DataSource and TransactionManager only change the data access mode. For example, when Hibernate is used for data access,
DataSource is actually SessionFactory, and the implementation of TransactionManager is HibernateTransactionManager.

Method 1: Each Bean has a proxy


Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">


Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">






Class = "org. springframework. orm. hibernate3.HibernateTransactionManager">










Class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean">







PROPAGATION_REQUIRED






Method 2: All beans share a Proxy Base Class



Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">


Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">






Class = "org. springframework. orm. hibernate3.HibernateTransactionManager">




Class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean"
Lazy-init = "true" abstract = "true">





PROPAGATION_REQUIRED















Method 3: Use interceptor



Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">


Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">






Class = "org. springframework. orm. hibernate3.HibernateTransactionManager">




Class = "org. springframework. transaction. interceptor. TransactionInterceptor">




PROPAGATION_REQUIRED








* Dao




TransactionInterceptor










Method 4: Use the interceptor configured with the tx tag



Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">






Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">






Class = "org. springframework. orm. hibernate3.HibernateTransactionManager">












Expression = "execution (* com. bluesky. spring. dao. *. * (..)"/>
Pointcut-ref = "interceptorPointCuts"/>


Method 5: Full Annotation





Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">









Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">






Class = "org. springframework. orm. hibernate3.HibernateTransactionManager">





@ Transactional annotation must be added to DAO as follows:


Package com. bluesky. spring. dao;


Import java. util. List;


Import org. hibernate. SessionFactory;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. orm. hibernate3.support. HibernateDaoSupport;
Import org. springframework. stereotype. Component;


Import com. bluesky. spring. domain. User;


@ Transactional
@ Component ("userDao ")
Public class UserDaoImpl extends HibernateDaoSupport implements UserDao {


Public List ListUsers (){
Return this. getSession (). createQuery ("from User"). list ();
}
}

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.