/**
     * 1.   根据DataSource去创建事务管理器
     *      构造方法 , 参数1. DataSource
     */
    DataSourceTransactionManager txManager = new DataSourceTransactionManager(getDataSource());
    /**
     * 2.   创建事务模版对象, 并将事务管理器传递给模版
     */
    TransactionTemplate ttlate = new TransactionTemplate(txManager);

    /**
     * 3.   通过事务模版 , 执行事务, 并指定事务的回调函数
     */
    Boolean flag = ttlate.execute(new TransactionCallback<Boolean>() {

        /**
         *  我们要处理的JDBC操作, 可以放到这个方法中,  在这个方法里的所有JDBC操作, 将视为一个事务
         *  
         *  这个方法, 如何认定事务是否应该提交 :
         *  
         *      当方法中出现异常, 则表示事务执行失败 ,
         *      如果异常进行了处理, 则事务执行成功!
         */
        @Override
        public Boolean doInTransaction(TransactionStatus status) {
            try{
            
                getJdbcTemplate().update("update book set bookname=? where bookid=?", "西游记",10002);
                    if(1==2){
                        throw new RuntimeException("停电了, 哈哈哈");
                    }
                getJdbcTemplate().update("update book set bookname=? where bookid=?", "红楼梦",10003);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                //处理了异常 , 没事了
                //加入回滚标记 ( 本次事务不提交  )
                status.setRollbackOnly();
                return false;

            }
        }
    });
    System.out.println("事务执行的结果:"+flag);

 

声明式事务

相较于编程式的事务 ,有利有弊 !

优点:  

    是一种aop的编程思想, 给一段代码添加事务, 无需修改原代码

缺点:

    因为采用了注解, 注解的最小范围只能给类的成员 , 也就是说, 声明时的事务 ,最小的处理范围是一个方法 !

使用步骤:

1.  向容器中添加事务的管理对象
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>

2.  开启事务的扫描 , 指定事务管理器

<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

3.  在具体要被执行事务的方法上, 添加注解@Transactional即可
@Transactional也可以添加到类上,表示对该类的所有方法使用事务

 

@Transactional注解的属性

@Transactional注解标记有以下属性,在使用时
可以根据需要做特殊设定。
propagation: 设置事务传播
isolation : 设置事务隔离级别
readOnly : 设置为只读,还是可读写
rollbackFor : 设置遇到哪些异常必须回滚
noRollbackFor : 设置遇到哪些异常不回滚

 

看一个Demo:

User类:

package cn.wxg.bean;

public class User {

    private int id;
    private String name;
    private String password;
    public User() {
        super();
    }
    public int getId() {
        return id;
    }
    public User(int id, String name, String password) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [>;
    }
    
}
User.java

相关文章:

  • 2021-07-22
  • 2018-07-18
  • 2021-05-29
  • 2021-07-06
  • 2021-06-26
  • 2021-05-03
猜你喜欢
  • 2020-01-14
  • 2021-07-04
  • 2021-12-04
  • 2021-09-14
相关资源
相似解决方案