简体   繁体   English

连接池:关闭还是不关闭连接?

[英]Connection Pooling: To Close or Not to Close Connections?

I'm using dbcp's BasicDataSource in my JSF Java application. 我在JSF Java应用程序中使用dbcp的BasicDataSource Since the basic convention is to close the connection after using it, I do so in catch - finally in my code. 由于基本约定是在使用后关闭连接,因此我在catch中-最终在我的代码中这样做。 However, the application grinds to a halt with the error, 但是,应用程序因错误而停止运行,

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)

So I decide to not close my connections; 因此, 我决定不关闭我的连接; my code almost runs okay, but then often stops with this error: 我的代码几乎可以正常运行,但是随后经常因以下错误而停止:

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

Below is my connection configuration: 以下是我的连接配置:

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;
    }
}

I should mention I've tried playing around with these settings, and also using defaults, but with the same results. 我应该提到我尝试使用这些设置,也使用默认设置,但结果相同。 What could I be doing wrong? 我可能做错了什么?

I found the solution to this after I realized that I was doing it all wrong. 在意识到自己做错了一切之后,我找到了解决方案。 So here is what I was able to come up with, that works perfectly with my design approach. 因此,这就是我能够想到的,与我的设计方法完美配合。

//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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM