c3p0连接池完全eventhough连接已关闭

时间:2012-10-09 13:29:41

标签: java jdbc c3p0

我对c3p0连接池有这个问题,我是c3p0的新手。

我的桌面程序使用java和mssql 2008r2 我使用的jdbc驱动程序是jtds 对于连接池我使用c3p0

程序运行一段时间后出现问题,程序因为等待从池中连接而卡住了。

似乎连接池已满,因此无法执行sql语句。 我每次完成执行sql语句后都已关闭连接。

是否有人对c3p0有同样的问题?

注意: 我使用ComboPooledDataSource,当我想获得一些连接时,我使用ComboPooledDataSource,getConnection()方法。来自ComboPooledDataSource的这个getConnection()方法,它是否获得空闲连接?

如何连接空闲?因为每次我得到连接时我都已经关闭了连接。

感谢。

这是我使用的代码: 一般我有2个类: 1.对于数据库连接(获取连接和池) 2.for数据库事务(执行查询语句)

public final class DBConnection {
private static DatabaseProperties dbProp;
private static ComboPooledDataSource ds;

private DBConnection(){}

private static void create(){
    DatabaseProperties dp = getDatabaseProperties();
    boolean success = true;

    do{
        try{
            ds = new ComboPooledDataSource();
            ds.setDriverClass("net.sourceforge.jtds.jdbc.Driver");
            ds.setJdbcUrl("jdbc:jtds:sqlserver://"+ dp.getIpaddr()+":"+dp.getPort()+ "/"+dp.getDbname(););
            ds.setUser(dp.getUsername());
            ds.setPassword(dp.getPassword());

            ds.setMinPoolSize(3);
            ds.setAcquireIncrement(1);
            ds.setMaxPoolSize(20);
        } catch (Exception ex) {
            LoggerController.log(Level.SEVERE,"Database error on creating connection",ex,LoggerController.DATABASE);
            success = false;
        }
    }while(!success);
}
public static ComboPooledDataSource getDataSource(){
    if(ds == null)create();

    return ds;
}

public static Connection getConnection(){
    Connection con = null;
    try {
        con = DBConnection.getDataSource().getConnection();
    } catch (SQLException ex) {
        LoggerController.log(Level.SEVERE,"Database error on getting connection",ex,LoggerController.DATABASE);
    }

    return con;
}

public static void close(){
    ds.close();
}

}

public class DBTrans {
private DBTrans(){}

public static DataTable executeQuery(String query) throws SQLException{
    DataTable dt = null;
    Connection con = null;
    try {
        con = DBConnection.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        dt = new DataTable(rs);
    } catch (SQLException ex) {
        throw new SQLException("QUERY= ["+query+"]\n"+ex.getMessage());
    }
    finally{
        if(con!=null){
            con.close();
        }
    }
    return dt;
}

public static int executeUpdate(String query) throws SQLException{
    int sql = 0;
    Connection con = null;
    try {
        con = DBConnection.getConnection();
        Statement stmt = con.createStatement();
        sql = stmt.executeUpdate(query);
        con.close();
    } catch (SQLException ex) {
        throw new SQLException("QUERY=["+query+"]\n"+ex.getMessage());
    }
    finally{
        if(con!=null){
            con.close();
        }
    }
    return sql;
}

}

1 个答案:

答案 0 :(得分:2)

根据您显示的代码,最终您的应用程序可能会泄漏Connections,导致连接池耗尽(即,所有Connections都被不可逆转地检出)。您需要始终如一地使用强大的资源清理习惯用法。见例如

http://old.nabble.com/Re:-My-connections-are-all-idle...-p27691635.html

修改代码以可靠地连接Connection后,如果仍然遇到问题,c3p0可以设置查找和调试未返回的连接。您可以临时设置unreturnedConnectionTimeout并使用 debugUnreturnedConnectionStackTraces跟踪泄漏。参见

http://www.mchange.com/projects/c3p0/index.html#unreturnedConnectionTimeout

http://www.mchange.com/projects/c3p0/index.html#debugUnreturnedConnectionStackTraces

但首先,只需修复资源清理代码,然后查看问题是否消失。捕获debugUnreturnedConnectionStackTraces是一种性能拖累;顾名思义,它应该只是临时使用,以便进行调试。

我希望这有帮助!

相关问题