java servlet/jsp를 사용하여 빌드하고 있습니다. 데이터베이스 연결을 처리하는 클래스가 있지만 각 요청마다 각 인스턴스를 만들거나 모든 요청에 대해 하나의 인스턴스를 만들어야하는지 알 수 없습니다.각 요청마다 새 인스턴스를 작성해야합니까?
시나리오 1 : 예를 들어 다음
class HandleDB {
public static HandleDB getInstance(); // singleton pattern
public void initConnection();
public void releaseConnection();
}
, 2
//at the beginning of a request:
HandleDB.getInstance().initConnection();
// handle tasks
// at the end of request
HandleDB.getInstance().releaseConnection();
시나리오 :
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;
프라 사용해야 시나리오 ctice?
예, 우리는 연결 풀에 Apache DBCP를 사용하며 귀하의 제안을 기다리고 있습니다. – ipkiss