TC java数据库访问标准格式(with transaction)

java 代码
 
  1.  /** 
  2.   * 

     

     
  3.   * Get database connection from the db connection factory. It will set the connection to auto commit as required. 
  4.   * Note: autoCommit will be closed only for update statements, but not for query statements. 
  5.   * 

     

     
  6.   * @return A database connection. 
  7.   * @throws ChatContactPersistenceException 
  8.   *             If can't get connection or fails to set the auto commit value. 
  9.   */  
  10.  private Connection createConnection(boolean autoCommit) throws ChatContactPersistenceException {  
  11.      try {  
  12.          // create a DB connection  
  13.          Connection conn = connectionFactory.createConnection(connectionName);  
  14.   
  15.          // Begin transaction.  
  16.          conn.setAutoCommit(autoCommit);  
  17.   
  18.          return conn;  
  19.      } catch (DBConnectionException dbce) {  
  20.          throw new ChatContactPersistenceException("Can't get the connection from database.", dbce);  
  21.      } catch (SQLException sqle) {  
  22.          throw new ChatContactPersistenceException("Error while setting auto commit", sqle);  
  23.      }  
  24.  }  
  25.   
  26.  /** 
  27.   * 

     

     
  28.   * Release the connection. If not success, rollback the transaction. 
  29.   * 

     

     
  30.   * @param conn 
  31.   *            the connection. 
  32.   * @param needRollBack 
  33.   *            whether rolling back is needed. 
  34.   */  
  35.  private void releaseConnection(Connection conn, boolean needRollBack) {  
  36.      try {  
  37.          if (needRollBack && (conn != null) && !conn.isClosed()) {  
  38.              conn.rollback();  
  39.          }  
  40.      } catch (SQLException e) {  
  41.          // ignore it  
  42.      }  
  43.   
  44.      try {  
  45.          if ((conn != null) && !conn.isClosed()) {  
  46.              conn.close();  
  47.          }  
  48.      } catch (SQLException e) {  
  49.          // ignore it  
  50.      }  
  51.  }  
  52.   
  53.  /** 
  54.   * 

     

     
  55.   * Release the statement and result set. 
  56.   * 

     

     
  57.   * @param stmt 
  58.   *            the statement to release 
  59.   * @param result 
  60.   *            the result to release 
  61.   */  
  62.  private void releaseStatement(Statement stmt, ResultSet result) {  
  63.      try {  
  64.          if (result != null) {  
  65.              result.close();  
  66.          }  
  67.          if (stmt != null) {  
  68.              stmt.close();  
  69.          }  
  70.      } catch (SQLException e) {  
  71.          // ignore it  
  72.      }  
  73.  }  
  74.   
  75. public void addBuddy(long userId, long buddyUserId) throws Exception{  
  76.      Connection conn = null;  
  77.      boolean success = false;  
  78.      PreparedStatement stmt = null;  
  79.   
  80.      try {  
  81.          // create a DB connection  
  82.          conn = createConnection(false);  
  83.   
  84.          // Insert this buddy if not existed.  
  85.          stmt = conn.prepareStatement(SQL_INSERT_BUDDYUSER);  
  86.          stmt.setLong(1, userId);  
  87.          stmt.setLong(2, buddyUserId);  
  88.          stmt.executeUpdate();  
  89.   
  90.          // commit the transaction  
  91.          conn.commit();  
  92.          success = true;  
  93.      } catch (SQLException sqle) {  
  94.          throw new ChatContactPersistenceException("Error while processing sql statement", sqle);  
  95.      } finally {  
  96.          releaseStatement(stmt, null);  
  97.          releaseConnection(conn, !success);  
  98.      }  
  99.  }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值