2018-03-01 web用户项目(一):批量删除 ---- 事物处理

//批量删除:批量删除,要么全部失败,要么全部成功-----事务
    //解决数据耦合 ----- ThreadLocal类
    public void deleteBatch(String[] ids) {
        // TODO Auto-generated method stub
        
        
        try {
            
            //开启事务 ----- set(conn)(ThreadLocal类)
            TransactionUtil.startTransaction();
            
            for (String id : ids) {
                
                //不能用cdao.deleteOneById(id)
                cdao.deleteOneByIdInTransaction(id);
            }
//          //提交事务
//          conn.commit();
        
            TransactionUtil.commit();
        } catch (Exception e) {
            //有异常就回滚
            TransactionUtil.rollback();
            
        }finally {
            //释放资源
            TransactionUtil.relase();
            
        }
        
        
    }


//把得到连接及事务有关的方法写到此类中
public class TransactionUtil {
    
    // 内部是维护了 一个 map , 这个map 的key 始终 都是 当前 的线程 
    private static ThreadLocal tl = new ThreadLocal();
    
    private static DataSource ds = JdbcUtils.getDataSource();
    
    public static DataSource getDataSource(){
        return ds;
    }
    
    //  这里, 获得一个 connection  对象 
    public static Connection getConnection(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                //从数据连接池 中 取 一个连接 出来 
                conn = ds.getConnection();
                
                //将 取出来  connection 放到 tl中去
                tl.set(conn);
            }
            return conn;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 开启 事务  
    // 结果 就是 返回了 一个 connection对象, 并且 将 返回的 connection放到了 threadlocal中 , 
    public static void startTransaction(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void rollback(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.rollback();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void commit(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void relase(){
        try {
            Connection conn = tl.get();
            if(conn!=null){
                conn.close();
                tl.remove();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

你可能感兴趣的:(2018-03-01 web用户项目(一):批量删除 ---- 事物处理)