public void begin(boolean autocommit) throws DBTransException {
try { if(!this.isdatasource)
{
log("DB:before get connection");
conn = DriverManager.getConnection(strUrl, strUser, strPass);
log("DB:after get connection");
}
else
{
String jndiname = bundle.getString("jndiname");
log("DB:before datasource get connection, jndiname:" + jndiname);

Context ctx = new InitialContext();
    //Context envCtx = (Context) ctx.lookup("java:comp/env");
    datasource = (DataSource) ctx.lookup(jndiname);
    
    conn = datasource.getConnection();
    
    log("DB:after datasource get connection");
}

} catch (Exception se) {
log("DB:SQL Exception");
se.printStackTrace();
throw new DBTransException(DBTransException.ERR_GET_CONN, bundle);
} this.autocommit = autocommit;

try {
if (autocommit) {
log("DB:auto commit on");
conn.setAutoCommit(true);
} else {
log("DB:auto commit off");
conn.setAutoCommit(false);
}

} catch (SQLException e) {
throw new DBTransException(DBTransException.ERR_SET_AUTOCOMMIT, bundle);
}
}
看我上面的代码,每次开始事务都要查找ctx.lookup(jndiname);寻找连接池,这样是不是不对啊?是不是只需要查找一次,就把DataSource保存起来,每次datasource.getConnection();就行了?
该怎样做了?

解决方案 »

  1.   

    写一个专门取getConnection()的方法
    还有一个关闭的closeConnection()
      

  2.   

    我全贴出来,package uooho.db;
    import java.sql.*;
    import javax.naming.*;
    import javax.sql.*;
    import java.util.*;/**
     * Database transaction class
     * @author xi sha sha
     *
     */
    public class DBTransaction { private DataSource datasource = null;
    private Connection conn = null;
    private ResourceBundle bundle = null;

    private String strDriver = "";
    private String strUrl = "";
    private String strUser = "";
    private String strPass = "";
    private boolean debug = false;
    private boolean autocommit = false;
    private boolean isdatasource = false;
    /**
     * construct db connaction
     * @throws DBTransException 
     */
    public DBTransaction() throws DBTransException{
    try {
    bundle = ResourceBundle.getBundle("db");

    this.strDriver = bundle.getString("dbDriver");
    this.strUrl = bundle.getString("dbUrl");
    this.strUser = bundle.getString("dbUser");
    this.strPass = bundle.getString("dbPass");

    if(bundle.getString("debug").equals("yes"))
    this.debug = true;

    if(bundle.getString("isDataSource").equals("yes"))
    this.isdatasource = true;

    } catch (Exception e) {
    e.printStackTrace();
    }

    try {

    log("DB:loading Driver");
    Class.forName(this.strDriver);
    log("DB:loading Driver done");

    } catch (ClassNotFoundException e) {
    throw new DBTransException(DBTransException.ERR_DRIVER, bundle);
    }
    }
    /**
     * begin transaction,auto commit is true
     * @throws DBTransException
     */
    public void begin() throws DBTransException {
    begin(true);
    }
    /**
     * begin transaction
     * @param autocommit true auto commit ,false no auto
     * @throws DBTransException 
     */
    public void begin(boolean autocommit) throws DBTransException {
    try { if(!this.isdatasource)
    {
    log("DB:before get connection");
    conn = DriverManager.getConnection(strUrl, strUser, strPass);
    log("DB:after get connection");
    }
    else
    {
    String jndiname = bundle.getString("jndiname");
    log("DB:before datasource get connection, jndiname:" + jndiname);

    Context ctx = new InitialContext();
        //Context envCtx = (Context) ctx.lookup("java:comp/env");
        datasource = (DataSource) ctx.lookup(jndiname);
        
        conn = datasource.getConnection();
        
        log("DB:after datasource get connection");
    }

    } catch (Exception se) {
    log("DB:SQL Exception");
    se.printStackTrace();
    throw new DBTransException(DBTransException.ERR_GET_CONN, bundle);
    } this.autocommit = autocommit;

    try {
    if (autocommit) {
    log("DB:auto commit on");
    conn.setAutoCommit(true);
    } else {
    log("DB:auto commit off");
    conn.setAutoCommit(false);
    }

    } catch (SQLException e) {
    throw new DBTransException(DBTransException.ERR_SET_AUTOCOMMIT, bundle);
    }
    }
    /**
     * commit transaction
     * @throws DBTransException 
     */
    public void commit() throws DBTransException{
    if(!this.autocommit)
    {
    log("DB:in commit");
    try {
    conn.commit();
    log("DB:after commit");
    } catch (SQLException e) {
    rollback();
    throw new DBTransException(DBTransException.ERR_COMMIT_TRAN, bundle);
    }
    finally{
    close();
    }
    }
    close();
    }
    /**
     * rollback data
     * @throws DBTransException 
     */
    public void rollback() throws DBTransException{
    try {
    conn.rollback();
    close();
    log("DB:after rollback");
    } catch (SQLException e) {
    throw new DBTransException(DBTransException.ERR_ROLLBACK, bundle);
    }

    }
    /**
     * close connection
     * @throws DBTransException 
     */
    public void close() throws DBTransException{
    try {
    if(null != conn && !conn.isClosed())
    {
    conn.close();
    log("DB:after conn close");
    }
    } catch (SQLException e) {
    throw new DBTransException(DBTransException.ERR_CLOSE_CONN, bundle);
    }
    }
    /**
     * get Connection object
     * @return
     */
    public Connection getConnection() {
    log("DB:get conn is " + (conn != null?"OK":"NULL"));
    return conn;
    }
    /**
     * print log
     * @param mess
     */
    private void log(String mess){
    if(this.debug)
    System.out.println(mess);
    }
    }看我上面的代码,每次开始事务都要查找ctx.lookup(jndiname);寻找连接池,这样是不是不对啊?是不是只需要查找一次,就把DataSource保存起来,每次datasource.getConnection();就行了? 
    该怎样做了?
    每次都要DBTransaction tran = new DBTransaction(); 这样对不对?
    但是DataSource也每次重新查找好像不合理。
      

  3.   

    只访问一次jndi,然后缓存起来。
    可以放入静态代码块中static{ ... }
    工具类最好是全部静态方法或者是单例类
      

  4.   


    static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err.println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }可以参照
      

  5.   

    视情况而定。如果是web application,通常会用spring来管理。