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 ();
}
}