我正在使用java servlet/jsp构建。我有一个类来处理数据库连接,但我不知道应该为每个请求创建每个实例或为所有请求创建一个实例。 例如:是否应为每个请求创建新实例?
方案1:
class HandleDB {
public static HandleDB getInstance(); // singleton pattern
public void initConnection();
public void releaseConnection();
}
然后,
//at the beginning of a request:
HandleDB.getInstance().initConnection();
// handle tasks
// at the end of request
HandleDB.getInstance().releaseConnection();
方案2:
class HandleDB {
public void initConnection();
public void releaseConnection();
}
//at the beginning of a request:
HandleDB db = new HandleDB();
db.initConnection();
// handle tasks
// at the end of request
db.releaseConnection();
db = null;
哪种情况应PRA使用ctice?
是的,我们使用Apache DBCP连接池,我会为你的建议。 – ipkiss