transmanager.java

来自「数据库连接池源码」· Java 代码 · 共 71 行

JAVA
71
字号
package llm.pool.relation;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;

/**
 * TransManager : 数据库事务管理
 */

public final class TransManager {
	
//  public static final int ORACLE = 0;
  public static final int SYBASE = 1;

  public TransManager() {
  }

  /**
   * 事务开始执行
   * @param conn Connection
   * @param dbType int
   * @throws SQLException
   */

  public static void beginTrans(Connection conn, int dbType) throws
      SQLException {
    if (dbType == SYBASE) {
      Statement stmt = conn.createStatement();
      stmt.execute("begin tran");
      stmt.close();
    }
    else
      conn.setAutoCommit(false);
  }

  /**
   * 事务完成后提交
   * @param conn Connection
   * @param dbType int
   * @throws SQLException
   */
  public static void commitTrans(Connection conn, int dbType) throws
      SQLException {
    if (dbType == SYBASE) {
      Statement stmt = conn.createStatement();
      stmt.execute("commit tran");
      stmt.close();
    }
    else
      conn.commit();
  }

  /**
   * 事务失败后回滚
   * @param conn Connection
   * @param dbType int
   * @throws SQLException
   */
  public static void rollbackTrans(Connection conn, int dbType) throws
      SQLException {
    if (dbType == SYBASE) {
      Statement stmt = conn.createStatement();
      stmt.execute("rollback tran");
      stmt.close();
    }
    else
      conn.rollback();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?