java - Why this error - Cannot get a connection, pool error Timeout waiting for idle object -
i having webapp on production, runs fine 7-8 days website down , logs reproduce error:
java.sql.sqlexception: cannot connection, pool error timeout waiting idle object
and operations database starts failing. have read across several other questions , blog not able find definite solution. using connection pool, , not sure problem is.
is because of code have written or pool configuration? providing code here 1 of method gets data database , pool configuration. please have , let me know if doing wrong.
public cartitem getcustomercartitem(int customerid, int productid, int productofcityid) { connection con = connectionpool.getinstance().getconnection(); preparedstatement st = null; resultset rs = null; cartitem ci = null; cityproduct cityproduct = productservice.getcityproduct(productid, productofcityid); if (cityproduct == null) { return null; } string query = "select ci.* customer_cart_item_mapping cim inner join cart_item ci on ci.id = cim.cartitemid " + "cim.customerid = ? , ci.productofcityid = ?"; try { st = con.preparecall(query); st.setint(1, customerid); st.setint(2, productofcityid); rs = st.executequery(); if (rs.first()) { ci = new cartitem(); ci.setid(rs.getint("id")); ci.setcityproduct(cityproduct); ci.setquantity(rs.getint("quantity")); ci.setcreateddate(rs.getdate("createddate")); ci.setupdateddate(rs.getdate("updateddate")); } } catch (sqlexception ex) { logger lgr = logger.getlogger(cartitemdaoimpl.class.getname()); lgr.log(level.severe, ex.getmessage(), ex); } { dbutil.close(con, st, rs); } return ci; }
below dbutil
class used close connections:
public class dbutil {
public static void close(connection c, statement s, resultset r) { try { if (r != null) { r.close(); } if (s != null) { s.close(); } if (c != null) { connectionpool.getinstance().freeconnection(c); } } catch (sqlexception ex) { logger.getlogger(dbutil.class.getname()).log(level.severe, null, ex); } }
}
following connectionpool
class
public class connectionpool { private static connectionpool pool=null; private static datasource datasource = null; public synchronized static connectionpool getinstance(){ if (pool == null){ pool = new connectionpool(); } return pool; } private connectionpool(){ try{ initialcontext ic = new initialcontext(); datasource = (datasource) ic.lookup("java:/comp/env/jdbc/prod_db"); } catch(namingexception e){ system.out.println(e); } } public connection getconnection(){ try{ return datasource.getconnection(); } catch (sqlexception sqle){ system.err.println(sqle); return null; } } public void freeconnection(connection c){ try{ c.close(); } catch (sqlexception sqle){ system.err.println(sqle); } }
}
below connection pool configuration
<context antijarlocking="true" path="/"> <resource auth="container" connectionproperties="useencoding=true;" driverclassname="com.mysql.jdbc.driver" initialsize="2" logabandoned="true" maxidle="40" maxtotal="70" maxwaitmillis="1000" minevictableidletimemillis="600000" minidle="2" name="jdbc/winni_prime_db" password="password" removeabandoned="true" removeabandonedtimeout="90" testwhileidle="true" timebetweenevictionrunsmillis="90000" type="javax.sql.datasource" url="jdbc:mysql://localhost:3306/prod_db" username="sqladmin" validationquery="select 1"/>
this problem has been persistent last several months. solution restart tomcat every night. please take @ code , give me expert opinion doing wrong. using mysql
you can potentially have lick here:
if (cityproduct == null) { return null; }
you got connection before code, can exit function without closing connection.
Comments
Post a Comment