@Transactional spring 配置事务 注意事项

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基于接口的代理将起作用。



< !-- JTA事务(非分布式事务), 事务配置的时候 ,不能指定dataSource属性(分布式事务,是有全局事务来管理数据库链接的)-->   
< !-- 标准的JDK基于接口的代理将起作用 -->  

[html] view plain copy
 
 
print?
  1. <!-- aop切面 -->    
  2.     <aop:aspectj-autoproxy proxy-target-class="false" />    
  3.     
  4. <!-- 基于类的代理将起作用 ,同时 cglib.jar必须在CLASSPATH中 -->    
  5. <!-- aop切面 -->    
  6.     <aop:aspectj-autoproxy proxy-target-class="true" />    
<!-- aop切面 -->  
    <aop:aspectj-autoproxy proxy-target-class="false" />  
  
<!-- 基于类的代理将起作用 ,同时 cglib.jar必须在CLASSPATH中 -->  
<!-- aop切面 -->  
    <aop:aspectj-autoproxy proxy-target-class="true" />  



注解@Transactional cglib与java动态代理最大区别是代理目标对象不用实现接口, 那么注解要是写到接口方法上,要是使用cglib代理,这是注解事物就失效了,为了保持兼容注解最好都写到实现类方法上。

5. Spring团队建议在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上 。在接口上使用 @Transactional 注解,只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的,这就意味着如果正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装。

6. @Transactional 的事务开启 ,或者是基于接口的 或者是基于类的代理被创建。所以在同一个类中一个方法调用另一个方法有事务的方法,事务是不会起作用的 。

[java] view plain copy
 
 
print?
  1. public interface PersonageTempService {    
  2.     //删除指定id的Personage    
  3.     public void del(Integer Personageid) ;    
  4.     
  5.     //删除指定id的Personage,flag    
  6.     public void del(Integer Personageid,boolean flag) ;    
  7.     }    
  8.     
  9.     public class PersonageTempServiceBean implements PersonageTempService {    
  10.         private JdbcTemplate jdbcTemplate;    
  11.     
  12.         public void del(Integer Personageid){    
  13.             try{    
  14.                 this.del(Personageid,true)    
  15.                 System.out.println("del success");    
  16.             }catch(Exception e){    
  17.                 System.out.println("del failed");    
  18.             }    
  19.         }    
  20.     
  21.         @Transactional    
  22.         //此时,事务根本就没有开启, 即数据库会默认提交该操作,即记录别删除掉    
  23.         public void del(Integer Personageid,boolean flag){    
  24.             if(flag == ture){    
  25.                 jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid}, new int[]{java.sql.Types.INTEGER});    
  26.                 throw new RuntimeException("运行期例外");    
  27.             }    
  28.         }    
  29.     }    
  30.     
  31.     public class PersonageTempServiceBeanTest{    
  32.         PersonageTempService ps = new PersonageTempServiceBean ();    
  33.         ps.del(5);    
  34.     }    
  35. }    
public interface PersonageTempService {  
    //删除指定id的Personage  
    public void del(Integer Personageid) ;  
  
    //删除指定id的Personage,flag  
    public void del(Integer Personageid,boolean flag) ;  
    }  
  
    public class PersonageTempServiceBean implements PersonageTempService {  
        private JdbcTemplate jdbcTemplate;  
  
        public void del(Integer Personageid){  
            try{  
                this.del(Personageid,true)  
                System.out.println("del success");  
            }catch(Exception e){  
                System.out.println("del failed");  
            }  
        }  
  
        @Transactional  
        //此时,事务根本就没有开启, 即数据库会默认提交该操作,即记录别删除掉  
        public void del(Integer Personageid,boolean flag){  
            if(flag == ture){  
                jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid}, new int[]{java.sql.Types.INTEGER});  
                throw new RuntimeException("运行期例外");  
            }  
        }  
    }  
  
    public class PersonageTempServiceBeanTest{  
        PersonageTempService ps = new PersonageTempServiceBean ();  
        ps.del(5);  
    }  
}  




 7. Spring使用声明式事务处理,默认情况下, 如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback ;如果发生的异常是checked异常,默认情况下数 据库操作还是会提 交的。

[java] view plain copy
 
 
print?
  1. public interface PersonageService {    
  2.     //删除指定id的Personage    
  3.     public void del(Integer Personageid) ;    
  4.     
  5.     //获取Personage    
  6.     public Personage getPersonage(Integer Personageid);    
  7.     }    
  8.     
  9.     //PersonageServiceBean 实现了PersonageService 接口,则基于接口的还是基于类的代理 都可以实现事务    
  10.     @Transactional public class PersonageServiceBean implements PersonageService {    
  11.     private JdbcTemplate jdbcTemplate;    
  12.     
  13.     //发生了unchecked异常,事务回滚, @Transactional    
  14.     public void del(Integer Personageid){    
  15.         jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid},    
  16.         new int[]{java.sql.Types.INTEGER});    
  17.         throw new RuntimeException("运行期例外");    
  18.     }    
  19. }    
  20.    
  21.   
  22.   
  23. public interface PersonageService {    
  24.     //删除指定id的Personage    
  25.     public void delete(Integer Personageid) throws Exception;    
  26.     
  27.     //获取Personage    
  28.     public Personage getPersonage(Integer Personageid);    
  29.     }    
  30.     
  31.     @Transactional    
  32.     public class PersonageServiceBean implements PersonageService {    
  33.     
  34.     //发生了checked异常,事务不回滚,即数据库记录仍能被删除,    
  35.     //checked的例外,需要我们在外部用try/catch语法对调用该方法的地方进行包含 @Transactional    
  36.     public void delete(Integer Personageid) throws Exception{    
  37.         jdbcTemplate.update("delete from Personage where id=?", new Object[]{Personageid},    
  38.         new int[]{java.sql.Types.INTEGER});    
  39.         throw new Exception("运行期例外");    
  40.     }    
  41. }    
public interface PersonageService {  
    //删除指定id的Personage  
    public void del(Integer Personageid) ;  
  
    //获取Personage  
    public Personage getPersonage(Integer Personageid);  
    }  
  
    //PersonageServiceBean 实现了PersonageService 接口,则基于接口的还是基于类的代理 都可以实现事务  
    @Transactional public class PersonageServiceBean implements PersonageService {  
    private JdbcTemplate jdbcTemplate;  
  
    //发生了unchecked异常,事务回滚, @Transactional  
    public void del(Integer Personageid){  
        jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid},  
        new int[]{java.sql.Types.INTEGER});  
        throw new RuntimeException("运行期例外");  
    }  
}  
 


public interface PersonageService {  
    //删除指定id的Personage  
    public void delete(Integer Personageid) throws Exception;  
  
    //获取Personage  
    public Personage getPersonage(Integer Personageid);  
    }  
  
    @Transactional  
    public class PersonageServiceBean implements PersonageService {  
  
    //发生了checked异常,事务不回滚,即数据库记录仍能被删除,  
    //checked的例外,需要我们在外部用try/catch语法对调用该方法的地方进行包含 @Transactional  
    public void delete(Integer Personageid) throws Exception{  
        jdbcTemplate.update("delete from Personage where id=?", new Object[]{Personageid},  
        new int[]{java.sql.Types.INTEGER});  
        throw new Exception("运行期例外");  
    }  
}  




但是,对于checked这种例外,默认情况下它是不会进行事务回滚的,但是 如果我们需要它进行事务回滚,这时候可以在delete方法上通过@Transaction这个注解来修改它的行为。

[java] view plain copy
 
 
print?
  1. @Transactional    
  2. public class PersonServiceBean implements PersonService {    
  3.     
  4.     @Transactional(rollbackFor=Exception.class)    
  5.     //rollbackFor这属性指定了,既使你出现了checked这种例外,那么它也会对事务进行回滚    
  6.     public void delete(Integer personid) throws Exception{    
  7.         jdbcTemplate.update("delete from person where id=?", new Object[]{personid},    
  8.         new int[]{java.sql.Types.INTEGER});    
  9.         throw new Exception("运行期例外");    
  10.     }    
  11. }    
@Transactional  
public class PersonServiceBean implements PersonService {  
  
    @Transactional(rollbackFor=Exception.class)  
    //rollbackFor这属性指定了,既使你出现了checked这种例外,那么它也会对事务进行回滚  
    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方法执行前就不会开启事务 .


[java] view plain copy
 
 
print?
    1. @Transactional    
    2. public class PersonServiceBean implements PersonService {    
    3.     @Transactional(propagation=Propagation.NOT_SUPPORTED)    
    4.     //则此方法 就不会开启事务了    
    5.     public Person getPerson(Integer personid)    
    6.     {    
    7.     }    
    8. }    
原文地址:https://www.cnblogs.com/shenzhichipingguo/p/9187092.html