`

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.  }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics