1. 在需要事务管理的地方加@Transactional 注解。
@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。
2. @Transactional 注解只能应用到 public 可见度的方法上。
如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。
3. 注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据。必须在配置文件中使用配置元素,才真正开启了事务行为。
详见最下面的配置实例
4. 通过 元素的 "proxy-target-class" 属性值来控制是基于接口的还是基于类的代理被创建。
如果 "proxy-target-class" 属值被设置为 "true",那么基于类的代理将起作用(这时需要CGLIB库cglib.jar在CLASSPATH中)。
如果 "proxy-target-class" 属值被设置为 "false" 或者这个属性被省略,那么标准的JDK基于接口的代理将起作用。
如
<!--标准的JDK基于接口的代理将起作用-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<!--基于类的代理将起作用 ,同时 cglib.jar必须在CLASSPATH中-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
非JTA事务(即非分布式事务), 事务配置的时候 ,事务管理器(transactionManager)需要指定dataSource属性(非分布式事务,事务是在数据库创建的链接上开启。)
JTA事务(分布式事务), 事务配置的时候 ,事务管理器(transactionManager)不能指定dataSource属性(分布式事务,是有全局事务来管理数据库链接的)
注解@Transactional cglib与java动态代理最大区别是代理目标对象不用实现接口,那么注解要是写到接口方法上,要是使用cglib代理,这是注解事物就失效了,为了保持兼容注解最好都写到实现类方法上。
5. Spring团队建议在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。
在接口上使用 @Transactional 注解,只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的,这就意味着如果正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装。
6. @Transactional 的事务开启 ,或者是基于接口的 或者是基于类的代理被创建。
所以在同一个类中一个方法调用另一个方法有事务的方法,事务是不会起作用的。
- public interface PersonService {
-
- public void delete(Integer personid) ;
-
- public void delete(Integer personid,boolean flag) ;
- }
- public class PersonServiceBean implements PersonService {
- private JdbcTemplate jdbcTemplate;
-
-
- public void delete(Integer personid){
- try{
- this.delete(personid,true)
- System.out.println("delete success");
- }catch(Exception e){
- System.out.println("delete failed");
- }
- }
- @Transactional
-
- if(flag == ture){
- jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
- new int[]{java.sql.Types.INTEGER});
- throw new RuntimeException("运行期例外");
- }
- }
- }
- public class PersonServiceBeanTest{
- PersonService ps = new PersonServiceBean ();
- ps.delete(5);
- }
7. Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback;如果发生的异常是checked异常,默认情况下数据库操作还是会提交的。
- public interface PersonService {
-
- public void delete(Integer personid) ;
-
- public Person getPerson(Integer personid);
- }
-
- @Transactional public class PersonServiceBean implements PersonService {
- private JdbcTemplate jdbcTemplate;
-
-
-
- public void delete(Integer personid){
- jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
- new int[]{java.sql.Types.INTEGER});
- throw new RuntimeException("运行期例外");
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------------------
- public interface PersonService {
-
- public void delete(Integer personid) throws Exception;
-
- public Person getPerson(Integer personid);
- }
- @Transactional
- public class PersonServiceBean implements PersonService {
-
-
- public void delete(Integer personid) throws Exception{
- jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
- new int[]{java.sql.Types.INTEGER});
- throw new Exception("运行期例外");
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------------------
- 但是,对于checked这种例外,默认情况下它是不会进行事务回滚的,但是如果我们需要它进行事务回滚,这时候可以在delete方法上通过@Transaction这个注解来修改它的行为。
- @Transactional
- public class PersonServiceBean implements PersonService {
-
-
- @Transactional(rollbackFor=Exception.class)
-
- public void delete(Integer personid) throws Exception{
- jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
- new int[]{java.sql.Types.INTEGER});
- throw new Exception("运行期例外");
- }
- }
在PersonServiceBean这个业务bean里面,有
一些事务是不需要事务管理的,好比说获取数据的getPersons方法,getPerson方法。因为@Transactional 放在了类的上面。
此时,可以采用propagation这个事务属性@Transactional(propagation=Propagation.NOT_SUPPORTED),
propagation这个属性指定了事务传播行为,我们可以指定它不支持事务,当我们这么写了之后,Spring容器在getPersons方法执行前就不会开启事务.
- @Transactional
- public class PersonServiceBean implements PersonService {
-
-
- @Transactional(propagation=Propagation.NOT_SUPPORTED)
-
- public Person getPerson(Integer personid)
- {
- }
- }
#######################################################
混合事务配置实例:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
- <property name="driverClass" value="${db.driver}"></property>
- <property name="jdbcUrl" value="${db.url}"></property>
- <property name="user" value="${db.username}"></property>
- <property name="password" value="${db.password}"></property>
- <property name="acquireIncrement" value="2"></property>
- <property name="preferredTestQuery" value="select 1 from dual"></property>
-
- <property name="initialPoolSize" value="${db.minPoolSize}"></property>
- <property name="minPoolSize" value="${db.minPoolSize}"></property>
- <property name="maxPoolSize" value="${db.maxPoolSize}"></property>
- <property name="maxIdleTime" value="${db.maxIdleTime}"></property>
- </bean>
-
- <!-- Transaction manager :
- 非JTA事务(即非分布式事务), 事务配置的时候 ,事务管理器(transactionManager)需要指定dataSource属性(非分布式事务,事务是在数据库创建的链接上开启。)
- JTA事务(分布式事务), 事务配置的时候 ,事务管理器(transactionManager)不能指定dataSource属性(分布式事务,是有全局事务来管理数据库链接的)
- -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
-
- <!-- 使用注解annotation定义事务:
- 如果 "proxy-target-class" 属值被设置为 "true",那么基于类的代理将起作用(这时需要CGLIB库cglib.jar在CLASSPATH中)。
- 如果 "proxy-target-class" 属值被设置为 "false" 或者这个属性被省略,那么标准的JDK基于接口的代理将起作用。
-
- -->
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
-
-
-
- <tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="query*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- <tx:method name="select*" read-only="true" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="do*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="execute*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="*" read-only="true" propagation="REQUIRED" isolation="DEFAULT" />
- </tx:attributes>
- </tx:advice>
-
- <tx:advice id="logTxAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
- <tx:method name="query*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- <tx:method name="select*" read-only="true" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="*" propagation="REQUIRES_NEW"
- rollback-for="java.lang.Exception" />
- </tx:attributes>
- </tx:advice>
- <!-- "order"参数,这个参数是用来控制aop通知的优先级,值越小,优先级越高 ,混合事务配置时要制定该参数。
- execution:使用“execution(方法表达式)”匹配方法执行
- within:使用“within(类型表达式)”匹配指定类型内的方法执行
- -->
- <aop:config>
- <aop:pointcut id="defaultOperation"
- expression="execution(* *..*ServiceImpl.*(..))" />
- <aop:pointcut id="logServiceOperation"
- expression="@within(com.homent.service.LogService+" />
-
- <aop:advisor advice-ref="defaultTxAdvice" order="3" pointcut-ref="defaultOperation" />
- <aop:advisor advice-ref="logTxAdvice" order="2" pointcut-ref="logServiceOperation" />
- </aop:config>
-
-
- </beans>