JDBC MySql连接池实践,以避免耗尽连接池

Dan*_*lay 10 java mysql connection-pooling jdbc glassfish

我在GlassFish上有一个Java-JSF Web应用程序,我想在其中使用连接池.因此,我创建了一个applicationscoped bean,它Connection为其他bean的实例提供服务:

public class DatabaseBean {

    private DataSource myDataSource;

    public DatabaseBean() {
        try {
            Context ctx = new InitialContext();
            ecwinsDataSource = (DataSource) ctx.lookup("jdbc/myDataSource");
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        Connection connection = myDataSource.getConnection();
        System.out.println("Succesfully connected: " + connection);
        //Sample: Succesfully connected: com.sun.gjc.spi.jdbc40.ConnectionHolder40@7fb213a5
        return connection;
    }
}
Run Code Online (Sandbox Code Playgroud)

这样连接池的填充速度非常快; 在通过"db-related"视图进行一些导航后,应用程序将停止并显示以下内容:

RAR5117:无法从连接池[mysql_testPool]获取/创建连接.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.RAR5114:分配连接时出错:[分配连接时出错.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.] java.sql.SQLException:分配连接时出错.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.

我正在关闭每种方法中的连接和其他资源.应用程序通过独立连接运行一切正常.

我究竟做错了什么?任何提示或建议将不胜感激.

Bal*_*usC 20

该异常表示泄漏数据库连接的应用程序代码的典型情况.您需要确保根据正常的JDBC习惯用法在同一个方法块中的块中获取关闭它们(Connection,Statement ResultSet)try-with-resources.

public void create(Entity entity) throws SQLException {
    try (
        Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(SQL_CREATE);
    ) { 
        statement.setSomeObject(1, entity.getSomeProperty());
        // ...
        statement.executeUpdate();
    }
}
Run Code Online (Sandbox Code Playgroud)

或者当你不在Java 7上时,在一个try-finally块中.关闭它们finally将保证在例外情况下它们也会关闭.

public void create(Entity entity) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try { 
        connection = dataSource.getConnection();
        statement = connection.prepareStatement(SQL_CREATE);
        statement.setSomeObject(1, entity.getSomeProperty());
        // ...
        statement.executeUpdate();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

是的,即使使用连接池,您仍需要自己关闭连接.在初学者中,他们认为它会自动处理结束是一个常见的错误.这是不正确的.连接池返回一个包装的连接,它在close()中执行类似下面的操作:

public void close() throws SQLException {
    if (this.connection is still eligible for reuse) {
        do not close this.connection, but just return it to pool for reuse;
    } else {
        actually invoke this.connection.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

不关闭它们会导致连接没有被释放回池中以便重复使用,因此它将一次又一次地获取一个新连接,直到数据库用尽连接,这将导致应用程序崩溃.

也可以看看: