/** * Flushes the non-cacheable {@link Statement} instances for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushOpenNonCacheableStatements() throws SQLException { synchronized(nonCacheable) { int count = nonCacheable.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " open non-cacheable Statement" + (count != 1 ? "s" : "")); while (!nonCacheable.isEmpty()) { try { ((Statement)nonCacheable.remove(0)).close(); } catch (SQLException sqlx) { logger.warn(pool.getName() + ": " + sqlx.getMessage(), sqlx); } } } } }
/** * Creates a new {@link CacheConnection} object, using the supplied {@link Connection}. * @param pool {@link ConnectionPool} to which this {@code CacheConnection} belongs * @param con {@link Connection} instance to which database calls should be delegated */ public CacheConnection(ConnectionPool pool, Connection con) { this.pool = pool; this.con = con; setCacheAll(true); ssReq = ssHit = psReq = psHit = csReq = csHit = 0; // Send log output to same logger as the pool uses. logger = LoggerFactory.getLogger(pool.getClass().getName() + "." + pool.getName()); logUtil = pool.getCustomLogger(); }
/** * Flushes the spare {@link Statement} caches for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushSpareStatements() throws SQLException { synchronized(ss) { int count = ss.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " cached Statement" + (count != 1 ? "s" : "")); while (!ss.isEmpty()) (ss.remove(0)).release(); } } }
/** * Flushes the open {@link PreparedStatement} cache for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushOpenPreparedStatements() throws SQLException { synchronized(psUsed) { int count = psUsed.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " open PreparedStatement" + (count != 1 ? "s" : "")); while (!psUsed.isEmpty()) (psUsed.remove(0)).release(); } } }
/** * Flushes the open {@link Statement} cache for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushOpenStatements() throws SQLException { synchronized(ssUsed) { int count = ssUsed.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " open Statement" + (count != 1 ? "s" : "")); while (!ssUsed.isEmpty()) (ssUsed.remove(0)).release(); } } }
/** * Flushes the open {@link CallableStatement} cache for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushOpenCallableStatements() throws SQLException { synchronized(csUsed) { int count = csUsed.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " open CallableStatement" + (count != 1 ? "s" : "")); while (!csUsed.isEmpty()) (csUsed.remove(0)).release(); } } }
/** * Flushes the spare {@link CallableStatement} cache for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushSpareCallableStatements() throws SQLException { synchronized(cs) { int count = cs.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " cached CallableStatement" + (count != 1 ? "s" : "")); for (List<CachedCallableStatement> list : cs.values()) { for (CachedCallableStatement ccs : list) ccs.release(); } cs.clear(); } } }
/** * Flushes the spare {@link PreparedStatement} cache for this connection. * @throws SQLException if thrown while attempting to flush statements */ protected void flushSparePreparedStatements() throws SQLException { synchronized(ps) { int count = ps.size(); if (count > 0) { log_debug(pool.getName() + ": Closing " + count + " cached PreparedStatement" + (count != 1 ? "s" : "")); for (List<CachedPreparedStatement> list : ps.values()) { for (CachedPreparedStatement cps : list) cps.release(); } ps.clear(); } } }
/** * Sets whether to use caching for {@link Statement} instances. * @param cache whether to cache {@code Statement} instances */ public void setCacheStatements(boolean cache) { // Release statements if required. if (cacheS && !cache) { try { flushSpareStatements(); } catch (SQLException sqlx) { log_warn(pool.getName() + ": " + sqlx.getMessage(), sqlx); } } this.cacheS = cache; }
/** * Sets whether to use caching for {@link PreparedStatement} instances. * @param cache whether to cache {@code PreparedStatement} instances */ public void setCachePreparedStatements(boolean cache) { // Release statements if required. if (cacheP && !cache) { try { flushSparePreparedStatements(); } catch (SQLException sqlx) { log_warn(pool.getName() + ": " + sqlx.getMessage(), sqlx); } } this.cacheP = cache; }
/** * Sets whether to use caching for {@link CallableStatement} instances. * @param cache whether to cache {@code CallableStatement} instances */ public void setCacheCallableStatements(boolean cache) { // Release statements if required. if (cacheC && !cache) { try { flushSpareCallableStatements(); } catch (SQLException sqlx) { log_warn(pool.getName() + ": " + sqlx.getMessage(), sqlx); } } this.cacheC = cache; }
/** * Registers this pool with the platform MBean server. * Once this method has been called, each pool instance is accessible as an * MBean using the {@code javax.management} API. */ public void registerMBean() { if (mbeanRegistered) return; try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); this.mbeanRegisteredName = "snaq.db:type=ConnectionPool,name=\"" + getName() + "\""; ObjectName name = new ObjectName(mbeanRegisteredName); mbs.registerMBean(JmxUtils.createObjectPoolMBean(this), name); mbeanRegistered = true; log_info("Registered MBean for JMX access"); } catch (Exception ex) { log_warn("Unable to register pool with MBean server", ex); } }
if (count > 0) log_debug(pool.getName() + ": Cleaning " + count + " cached Statement" + (count != 1 ? "s" : "")); synchronized(ssUsed) if (count > 0) log_debug(pool.getName() + ": Cleaning " + count + " cached PreparedStatement" + (count != 1 ? "s" : "")); synchronized(psUsed) if (count > 0) log_debug(pool.getName() + ": Cleaning " + count + " cached CallableStatement" + (count != 1 ? "s" : "")); synchronized(csUsed) log_warn(pool.getName() + ": " + sqlx.getMessage(), sqlx);
log_debug(pool.getName() + ": CallableStatement cache hit [" + sql + "," + ccs.getParametersString() + "] - " + showHitRate(csHit, csReq, "CS-")); ccs.setStatementListener(this); ccs.setOpen(); log_debug(pool.getName() + ": CallableStatement cache miss [" + sql + "," + ccs.getParametersString() + "] - " + showHitRate(csHit, csReq, "CS-"));
log_debug(pool.getName() + ": PreparedStatement cache hit [" + sql + "," + cps.getParametersString() + "] - " + showHitRate(psHit, psReq, "PS-")); cps.setStatementListener(this); cps.setOpen(); log_debug(pool.getName() + ": PreparedStatement cache miss [" + sql + "," + cps.getParametersString() + "] - " + showHitRate(psHit, psReq, "PS-"));