I have this problem with c3p0 connection pooling, i am new with c3p0.
My desktop program use java with mssql 2008r2
the jdbc driver i use is jtds
and for the connection pooling i use c3p0
i have some problem when the program is running for some time, the program is stuck because it is waiting to get connection from the pool.
it seems that the connection pool is full, so that the sql statement cannot be executed.
i already close the connection every time i have finished execute the sql statement.
does anyone having this same problem with c3p0 ?
note :
i use ComboPooledDataSource and when i want to get some connection, i use the ComboPooledDataSource, getConnection() method. this getConnection() method from ComboPooledDataSource, does it get the idle connection?
how to make connection to idle? because i already close the connection everytime i get the connection.
Thanks.
here’s the code i use:
generally i have 2 class:
1.for the database connection (to get connection and the pooling)
2.for the database transaction (to execute query statement)
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;
}
}
Given the code you show, eventually your application is likely to leak Connections, leaving the Connection pool exhausted (that is, with all Connections irreversibly checked out). You need to consistently use the robust resource cleanup idiom. See e.g
http://old.nabble.com/Re:-My-connections-are-all-idle…-p27691635.html
Once you modify your code to reliably cose Connection, if you still see problems, c3p0 has settings to find and debug unreturned Connections. You can temporarily set an unreturnedConnectionTimeout and using
debugUnreturnedConnectionStackTraces to track down the leak. See
http://www.mchange.com/projects/c3p0/index.html#unreturnedConnectionTimeout
http://www.mchange.com/projects/c3p0/index.html#debugUnreturnedConnectionStackTraces
But first, just fix your resource cleanup code, and see if the problem goes away. Capturing debugUnreturnedConnectionStackTraces is a performance drag; as the name suggests, it should be used only temporarily, to debug.
I hope this helps!