运用ThreadLocal解决jdbc事务管理
JdbcUtils.java
public class JdbcUtils { private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); private static DataSource ds; static { ds = new ComboPooledDataSource(); } public static DataSource getDataSource() { return ds; } // 当前线程绑定一个开启事务的连接 // ThreadLocal是一个map集合 具有get() set() remove()方法 key是线程名称 public static Connection getConnection() throws SQLException { Connection conn = tl.get(); if (conn == null) { conn = ds.getConnection(); tl.set(conn); } return conn; } public static void startTransaction() { try { Connection conn = getConnection(); //调用 if(conn != null) { conn.setAutoCommit(false); } } catch (Exception e) { throw new RuntimeException(); } } public static void rollback() { try { Connection conn = getConnection(); //调用 if(conn != null) { conn.rollback(); conn.commit(); } } catch (Exception e) { throw new RuntimeException(); } } public static void commit() { try { Connection conn = getConnection(); //调用 if(conn != null) { conn.commit(); } } catch (Exception e) { throw new RuntimeException(); } } public static void close() { try { Connection conn = getConnection(); //调用 if(conn != null) { try { conn.close(); } finally { tl.remove(); } } } catch (Exception e) { throw new RuntimeException(); } } }
AccountService.java
public class AccountService { // ThreadLocal 解决方案 public void transfer(int sourceid,int destid,double money) throws SQLException { try { JdbcUtils.startTransaction(); AccountDao dao = new AccountDao(); Account a = dao.find(sourceid); //select Account b = dao.find(destid); //select a.setMoney(a.getMoney()-100); b.setMoney(b.getMoney()+100); dao.update(a); //update dao.update(b); //update
JdbcUtils.commit(); } catch (Exception e) { JdbcUtils.rollback();
throw new RuntimeException(); } finally { JdbcUtils.close(); } }