初步实现声明式事物

 

  1.   
  2. import java.io.IOException;   
  3. import java.io.InputStream;   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.SQLException;   
  7. import java.util.Properties;   
  8.   
  9. public final class TransactionHelper {   
  10.        
  11.     //使用ThreadLocal持有当前线程的数据库连接   
  12.     private final static ThreadLocal<Connection> connection_holder = new ThreadLocal<Connection>();   
  13.        
  14.     //连接配置,来自connection.properties   
  15.     private final static Properties connectionProp = new Properties();   
  16.        
  17.     static{        
  18.         //加载配置文件   
  19.         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("connection.properties");   
  20.         try {   
  21.                
  22.             connectionProp.load(is);   
  23.             is.close();   
  24.             //加载驱动程序   
  25.             Class.forName(connectionProp.getProperty("driverClassName"));   
  26.         } catch (IOException e) {   
  27.              throw new RuntimeException(e.getMessage(),e);   
  28.         }catch(ClassNotFoundException e){   
  29.             throw new RuntimeException("驱动未找到",e);   
  30.         }   
  31.     }   
  32.        
  33.     //获取当前线程中的数据库连接   
  34.     private static Connection getCurrentConnection()   
  35.     {   
  36.         Connection conn = connection_holder.get();   
  37.         if(conn == null){   
  38.             conn =  createNotAutoCommitConnection();               
  39.             connection_holder.set(conn);   
  40.         }   
  41.         return conn;   
  42.     }   
  43.        
  44.     //执行SQL语句   
  45.     public static int executeNonQuery(String sql) throws SQLException{   
  46.            
  47.         Connection conn = getCurrentConnection();   
  48.             
  49.         return conn.createStatement().executeUpdate(sql);   
  50.   
  51.     }   
  52.        
  53.     //提交事务   
  54.     public static void commit(){   
  55.         Connection conn = getCurrentConnection();   
  56.         try {   
  57.             conn.commit();   
  58.             conn.close();   
  59.             connection_holder.set(null);   
  60.         } catch (SQLException e) {   
  61.             throw new RuntimeException(e.getMessage(),e);   
  62.         }   
  63.     }   
  64.        
  65.        
  66.     //回滚事务   
  67.     public static void rollback(){   
  68.         Connection conn = getCurrentConnection();   
  69.         try {   
  70.             conn.rollback();   
  71.             conn.close();   
  72.             connection_holder.set(null);   
  73.         } catch (SQLException e) {   
  74.             throw new RuntimeException(e.getMessage(),e);   
  75.         }   
  76.     }   
  77.        
  78.     //创建一个不自动Commit的数据库连接   
  79.     private static Connection createNotAutoCommitConnection() {   
  80.         try {   
  81.                
  82.             Connection conn = DriverManager.getConnection(connectionProp.getProperty("url")+";databaseName="+ connectionProp.getProperty("databaseName")   
  83.                     ,connectionProp.getProperty("username")   
  84.                     ,connectionProp.getProperty("password"));   
  85.             conn.setAutoCommit(false);   
  86.             return conn;   
  87.         } catch (SQLException e) {   
  88.              throw new RuntimeException(e.getMessage(),e);   
  89.         }   
  90.     }      
  91. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值