/** * Overwrite to configure new created connections before use. * * @return created connection or null if not possible * @throws Exception */ protected DbConnection createConnection() throws Exception { try { InternalDbConnection con = getProvider().createConnection(); if (con == null) return null; con.setPool(this); pool.add(con); if (tracePoolSize.value()) log().d("Create DB Connection",pool.size()); con.setUsed(true); //getDialect().initializeConnection(con, this); return new DbConnectionProxy(this, con); } catch (Exception e) { // special behavior for e.g. mysql, retry to get a connection after gc() // Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections if (e.getMessage() != null && e.getMessage().indexOf("Too many connections") > -1) { printStackTrace(); } throw e; } }
public DbConnectionProxy(DbPool pool, DbConnection instance) { if (traceCaller.value()) { this.pool = pool; pool.getStackTraces().put(MSystem.getObjectId(this), new ConnectionTrace(this)); // instance.setUsedTrace(createStackTrace); } this.instance = instance; log().t(id,"created",instance.getInstanceId()); }
/** * {@inheritDoc} * * Look into the pool for an unused DbProvider. If no one find, create one. */ @Override public DbConnection getConnection() throws Exception { log().t(getName(),"getConnection"); boolean foundClosed = false; try { synchronized (pool) { for (InternalDbConnection con : pool) { if (con.isClosed() || con.checkTimedOut()) { foundClosed = true; } else if (!con.isUsed()) { con.setUsed(true); return new DbConnectionProxy(this, con); } } return createConnection(); } } finally { if (foundClosed) cleanup(false); } }
@Override protected void finalize() throws Throwable { log().t(id,"finalized",instance.getInstanceId()); if (instance != null) { log().i(id,"final closed", instance.getInstanceId() ); ConnectionTrace trace = pool.getStackTraces().get(MSystem.getObjectId(this)); if (trace != null) trace.log(log()); setUsed(false); } if (traceCaller.value()) pool.getStackTraces().remove(MSystem.getObjectId(this)); super.finalize(); }