2016-11-29 5 views
0

내 JSF Java 응용 프로그램에서 dbcp의 BasicDataSource을 사용하고 있습니다. 기본 컨벤션은 그것을 사용하고 나서 연결을 닫는 것이기 때문에 나는 코드에서 그렇게한다. 그러나 응용 프로그램이 그래서연결 풀링 : 연결을 닫거나 연결을 닫지 않으려면?

java.sql.SQLException: Connection is null. 
at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.checkOpen(DelegatingConnection.java:611) 
at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.createStatement(DelegatingConnection.java:258) 

내가 내 연결을 닫지하기로 결정 오류로 중단 갈기; 내 코드는 거의 괜찮 실행,하지만 종종이 오류와 함께 중지 : 아래

Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections 

내 연결 구성입니다 :

public class StageDB { 
    public StageDB() {} 
    public static Connection getConnection() { 
     BasicDataSource ds = new BasicDataSource(); 
     ds.setDriverClassName(JDBC_DRIVER); 
     ds.setUsername(USER); 
     ds.setPassword(PASS); 
     ds.setUrl(DB_URL); 
     ds.setTimeBetweenEvictionRunsMillis(20*1000); 
     ds.setMinIdle(0); 
     ds.setMaxIdle(10); 
     ds.setMaxOpenPreparedStatements(100); 
     conn = ds.getConnection(); 
     return conn; 
    } 
} 

나는 이러한 설정으로 주위 재생, 또한 사용 해봤 언급해야한다 기본값이지만 동일한 결과가 나타납니다. 나는 무엇을 잘못 할 수 있 었는가?

+3

'ds.close()'는 무엇인가요? 데이터 소스를 만든 다음 즉시 닫습니다. – Kayaman

+0

이것은 dbcp의 버그처럼 보입니다. 예, 더 이상 필요없는 conn.close()를 호출해야합니다. 백엔드가 오라클이라면 오라클의 UCP를 사용해 보셨습니까? –

+0

@Kayaman, 코드는 DAO에서 사용하는 데이터 소스 클래스에 있습니다. conn (ds.getConnection() 참조)을 반환합니다. –

답변

0

내가 잘못했음을 알게 된 후 해결책을 찾았습니다. 그래서 여기에 내가 생각해 낼 수 있었던 것이 있습니다. 그것은 제 디자인 접근법과 완벽하게 작동합니다.

//1. Create pooled connections datasource class using the Singleton. 

/*************************** 
* This is the pooled datasource class 
****************************/ 
public class PrimaryDS { 
    private PrimaryDS primaryDS; 

    public PrimaryDS() { 
     ds = new BasicDataSource(); 
     ds.setDriverClassName(JDBC_DRIVER); 
     ds.setUsername(USER); 
     ds.setPassword(PASS); 
     ds.setUrl(DB_URL); 
    } 

    public static PrimaryDS getInstance() { 
     primaryDS = primaryDS == null ? new PrimaryDS() : primaryDS; 
     return primaryDS; 
    } 

    public BasicDataSource getDataSource() { 
     return ds; 
    } 

    public void setDataSource(BasicDataSource ds) { 
     this.ds = ds; 
    } 
} 

//2. DAOs make call to the datasource. Initialize DS in the DAO's constructor. 

/*************************** 
* This is in the DAO's constructor 
****************************/ 
ds = PrimaryDS.getInstance().getDataSource(); 

//3. DAOs have the database manupulation methods. In each of these methods, 
//create connection object from ds, and ensure it's closed in the catch - finally. 

/*************************** 
* This is inside one of the DAO methods 
****************************/ 
try { 
    conn = ds.getConnection(); 
    stmt = null; 
    rs = null; 
    PreparedStatement pstmt = conn.prepareStatement(ACTIVE_ACCOUNTS_SQL); 
    pstmt.setByte(1,status); 
    ResultSet rs = pstmt.executeQuery(); 
    // TODO loop through rs 
} catch (SQLException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     if(rs != null) rs.close(); 
     if(stmt != null) stmt.close(); 
     if(conn != null) conn.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}