@Transactional 注解的使用和注意java
标准的JDK基于接口的代理将起做用-->proxy-target-class="false"/>sql
基于类的代理将起做用 ,同时 cglib.jar必须在CLASSPATH中 proxy-target-class="true"/>数据库
非JTA事务(即非分布式事务), 事务配置的时候 ,须要指定dataSource属性(非分布式事务,事务是在数据库建立的连接上开启。)bash
JTA事务(非分布式事务), 事务配置的时候 ,不能指定dataSource属性(分布式事务,是有全局事务来管理数据库连接的)分布式
注解@Transactional cglib与java动态代理最大区别是代理目标对象不用实现接口,那么注解要是写到接口方法上,要是使用cglib代理,这时注解事物就失效了,为了保持兼容注解最好都写到实现类方法上。this
public interface PersonService {
//删除指定id的person
public void delete(Integer personid) ;
//删除指定id的person,flag
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
//此时,事务根本就没有开启, 即数据库会默认提交该操做,即记录被删除掉 public void delete(Integer personid,boolean flag){
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);
}
复制代码
public interface PersonService {
//删除指定id的person
public void delete(Integer personid) ;
//获取person
public Person getPerson(Integer personid);
}
//PersonServiceBean 实现了PersonService 接口,则基于接口的仍是基于类的代理 均可以实现事务
@Transactional public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate;
//发生了unchecked异常,事务回滚, @Transactional
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 {
//删除指定id的person
public void delete(Integer personid) throws Exception;
//获取person
public Person getPerson(Integer personid);
}
@Transactional
public class PersonServiceBean implements PersonService {
//发生了checked异常,事务不回滚,即数据库记录仍能被删除,
//checked的例外,须要咱们在外部用try/catch语法对调用该方法的地方进行包含 @Transactional
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这个注解来修改它的行为。spa
@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方法执行前就不会开启事务.code
@Transactional
public class PersonServiceBean implements PersonService {
@Transactional(propagation=Propagation.NOT_SUPPORTED)
//则此方法 就不会开启事务了
public Person getPerson(Integer personid)
{
}
}
复制代码
note:咱们没必要为平凡悲叹,由于平凡也是一种美丽对象