⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactioncontext.java

📁 《Developing Applications with Java and UML》一书的源代码。
💻 JAVA
字号:
package com.jacksonreed;import java.io.*;import java.sql.*;import java.text.*;import java.util.*;import javax.sql.*;/** * This class is a transaction context object which maintains the * state of the connection including the ability to close it as well * as support of transaction management * * @author Paul R. Reed, Jr., Jackson-Reed, Inc. <prreed@jacksonreed.com> */public class TransactionContext implements Serializable {    private Connection dbConnection = null;    private DataSource datasource   = null;    private CustomerValue custVal   = null;    private boolean unitOfWork = false;    /**     * Class constructor with no arguments    */    public TransactionContext() {      try {        log("TransactionContext: setup JDBC Driver");        // This is where we load the driver for sun and used        // with MS Access        //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");		// This is where we load the driver for hypersonicSQL        Class.forName("org.hsql.jdbcDriver");        // This is where we load the driver for Cloudscape        // JDBC Driver        //Class.forName("COM.cloudscape.core.JDBCDriver");        // This is where we load the driver for Cloudscape        // RmiJdbc Driver        //Class.forName("COM.cloudscape.core.RmiJdbcDriver");        log("TransactionContext: setup JDBC loaded ok");      }      catch (ClassNotFoundException e) {        System.out.println("class not found");        throw new TranSysException(e.getMessage());      }    }    public Connection getDBConnection() {          log("TransactionContext: in get connection");          if (dbConnection == null)              throw new TranSysException ("no database connection established yet");          return dbConnection;    }    public void beginTran(boolean supportTran) throws TranBeginException, TranSysException {      unitOfWork = supportTran;      getConnection();      try {          if (supportTran) {          log("TransactionContext: auto off");              dbConnection.setAutoCommit(false);          }          else {          log("TransactionContext: auto on");              dbConnection.setAutoCommit(true);          }      }      catch (SQLException e) {          throw new TranBeginException(e.getMessage());          }    }    public void commitTran()      throws TranCommitException, TranSysException, TranRollBackException {      log("TransactionContext: in commit");      if (unitOfWork) {        try {            dbConnection.commit();            log("TransactionContext: afterCommit");             closeConnection();            }        catch (SQLException e) {            rollBackTran();            throw new TranCommitException("Cant commit transaction");            }      }      closeConnection();    }     public boolean rollBackTran() throws TranRollBackException, TranSysException {      boolean returnValue = false;      try {          dbConnection.rollback();          log("TransactionContext: afterRollback");          closeConnection();          returnValue = true;      }      catch (SQLException e) {          throw new TranRollBackException("Cant rollback transaction");      }      return returnValue;    }    public void closeResultSet(ResultSet result) throws TranSysException {        try {            if (result != null) {                result.close();            }         }         catch (SQLException se) {            throw new TranSysException("SQL Exception while closing " +                                        "Result Set : \n" + se);        }    }    public void closeStatement(Statement stmt) throws TranSysException {        try {            if (stmt != null) {                stmt.close();            }        } catch (SQLException se) {            throw new TranSysException("SQL Exception while closing " +                                        "Statement : \n" + se);        }    }    public void closeConnection() throws TranSysException {        try {            if (dbConnection != null && !dbConnection.isClosed()) {                dbConnection.close();                log("TransactionContext: closing connection");            }        } catch (SQLException se) {            throw new TranSysException("SQLExcpetion while closing" +                                    " DB Connection : \n" + se);        }    }    public void getConnection() throws TranSysException {        try {        log("TransactionContext: before get connection");		// connection string for the HypersonicSQL Database implementation        dbConnection = DriverManager.getConnection("jdbc:HypersonicSQL:hsql://localhost", "sa", "");        // connection string for the MS Access Database implementation        //dbConnection = DriverManager.getConnection("jdbc:odbc:remulak-bea", "", "");        // connection string for the Cloudscape JDBC implementation        //dbConnection = DriverManager.getConnection("jdbc:cloudscape:data", "", "");        // connection string for the Cloudscape RmiJdbc implementation        //dbConnection = DriverManager.getConnection("jdbc:cloudscape:rmi://localhost:1099/c:/tomcat/jakarta-tomcat-3.2.1/webapps/RemulakWebApp/data","", "");        log("TransactionContext: after get connection");        } catch (SQLException se) {        log("TransactionContext: SQL Exception");            se.printStackTrace();            throw new TranSysException("SQLExcpetion while getting" +                                        " DB Connection : \n" + se);        }    }    private void log(String s) {        System.out.println(s);    }}

⌨️ 快捷键说明

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