2

私は c3p0 接続プーリングでこの問題を抱えています。私は c3p0 が初めてです。

私のデスクトップ プログラムは、mssql 2008r2 で Java を使用します。使用する jdbc ドライバーは jtds で、接続プールには c3p0 を使用します。

プログラムがしばらく実行されているときに問題が発生しました。プールから接続を取得するのを待っているため、プログラムがスタックしています。

接続プールがいっぱいで、SQL ステートメントを実行できないようです。SQLステートメントの実行が完了するたびに、すでに接続を閉じています。

c3p0 で同じ問題を抱えている人はいますか?

注:私は ComboPooledDataSource を使用し、接続を取得したい場合は、ComboPooledDataSource、getConnection() メソッドを使用します。ComboPooledDataSource からのこの getConnection() メソッドは、アイドル状態の接続を取得しますか?

アイドルに接続するには?接続を取得するたびに、すでに接続を閉じているためです。

ありがとう。

ここに私が使用するコードがあります:一般的に私は2つのクラスを持っています:1.データベース接続用(接続とプーリングを取得するため)2.データベーストランザクション用(クエリステートメントを実行するため)

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

}

4

1 に答える 1

2

あなたが示すコードを考えると、最終的にアプリケーションは接続をリークする可能性があり、接続プールが使い果たされたままになります (つまり、すべての接続が不可逆的にチェックアウトされます)。堅牢なリソース クリーンアップ イディオムを一貫して使用する必要があります。例を参照してください

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

コードを変更して Connection を確実に生成しても問題が解決しない場合は、c3p0 には、返されない Connection を見つけてデバッグするための設定があります。unreturnedConnectionTimeout を一時的に設定し、debugUnreturnedConnectionStackTraces を使用してリークを追跡できます。見る

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

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

ただし、まず、リソース クリーンアップ コードを修正して、問題が解決するかどうかを確認してください。debugUnreturnedConnectionStackTraces のキャプチャはパフォーマンスの低下です。名前が示すように、デバッグのために一時的にのみ使用する必要があります。

これが役立つことを願っています!

于 2012-10-09T14:01:38.910 に答える