/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.eas.client.resourcepool; import java.sql.*; import java.util.*; import java.util.concurrent.Executor; /** * * @author mg */ public class BearDatabaseConnection implements Connection { protected final Map<String, PreparedStatement> stmts = new HashMap<>(); protected final Map<String, CallableStatement> calls = new HashMap<>(); protected ResourcePool<BearDatabaseConnection> connectionsPool; protected Connection delegate; protected int maxStatements = Integer.MAX_VALUE; protected int currentStatements; protected int currentCalls; public BearDatabaseConnection(int aMaxStatetments, Connection aDelegate, ResourcePool<BearDatabaseConnection> aPool) { super(); delegate = aDelegate; connectionsPool = aPool; maxStatements = Math.max(1, aMaxStatetments); } @Override public int getNetworkTimeout() throws SQLException { return delegate.getNetworkTimeout(); } @Override public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { delegate.setNetworkTimeout(executor, milliseconds); } @Override public void abort(Executor executor) throws SQLException { delegate.abort(executor); } @Override public String getSchema() throws SQLException { return delegate.getSchema(); } @Override public void setSchema(String schema) throws SQLException { delegate.setSchema(schema); } public void returnPreparedStatement(String aSqlClause, PreparedStatement aStatement) throws SQLException { stmts.put(aSqlClause, aStatement); } public void returnCallableStatement(String aSqlClause, CallableStatement aStatement) { calls.put(aSqlClause, aStatement); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return prepareStatement(sql); } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return prepareStatement(sql); } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return prepareStatement(sql); } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return prepareStatement(sql); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return prepareStatement(sql); } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { shrinkStatements(); PreparedStatement cachedStmt = stmts.remove(sql); if (cachedStmt != null) { return wrapPreparedStatement(sql, cachedStmt); } else { currentStatements++; return wrapPreparedStatement(sql, delegate.prepareStatement(sql)); } } protected BearCallableStatement wrapPrearedCall(String aSql, CallableStatement aCall) { return new BearCallableStatement(aSql, aCall, this); } protected BearPreparedStatement wrapPreparedStatement(String aSql, PreparedStatement aStatement) throws SQLException { return new BearPreparedStatement(aSql, aStatement, this); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return prepareCall(sql); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return prepareCall(sql); } @Override public CallableStatement prepareCall(String sql) throws SQLException { shrinkCalls(); CallableStatement cachedCall = calls.remove(sql); if (cachedCall != null) { return wrapPrearedCall(sql, cachedCall); } else { currentCalls++; return wrapPrearedCall(sql, delegate.prepareCall(sql)); } } @Override public void close() throws SQLException { connectionsPool.returnResource(this); } @Override public boolean isClosed() throws SQLException { return delegate == null || delegate.isClosed(); } /* * Delegating section */ @Override public Statement createStatement() throws SQLException { checkClosed(); return delegate.createStatement(); } @Override public String nativeSQL(String sql) throws SQLException { checkClosed(); return delegate.nativeSQL(sql); } @Override public void setAutoCommit(boolean autoCommit) throws SQLException { checkClosed(); delegate.setAutoCommit(autoCommit); } @Override public boolean getAutoCommit() throws SQLException { checkClosed(); return delegate.getAutoCommit(); } @Override public void commit() throws SQLException { checkClosed(); delegate.commit(); } @Override public void rollback() throws SQLException { checkClosed(); delegate.rollback(); } @Override public DatabaseMetaData getMetaData() throws SQLException { checkClosed(); return delegate.getMetaData(); } @Override public void setReadOnly(boolean readOnly) throws SQLException { checkClosed(); delegate.setReadOnly(readOnly); } @Override public boolean isReadOnly() throws SQLException { checkClosed(); return delegate.isReadOnly(); } @Override public void setCatalog(String catalog) throws SQLException { checkClosed(); delegate.setCatalog(catalog); } @Override public String getCatalog() throws SQLException { checkClosed(); return delegate.getCatalog(); } @Override public void setTransactionIsolation(int level) throws SQLException { delegate.setTransactionIsolation(level); checkClosed(); } @Override public int getTransactionIsolation() throws SQLException { checkClosed(); return delegate.getTransactionIsolation(); } @Override public SQLWarning getWarnings() throws SQLException { checkClosed(); return delegate.getWarnings(); } @Override public void clearWarnings() throws SQLException { checkClosed(); delegate.clearWarnings(); } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { checkClosed(); return delegate.createStatement(resultSetType, resultSetConcurrency); } @Override public Map<String, Class<?>> getTypeMap() throws SQLException { checkClosed(); return delegate.getTypeMap(); } @Override public void setTypeMap(Map<String, Class<?>> map) throws SQLException { checkClosed(); delegate.setTypeMap(map); } @Override public void setHoldability(int holdability) throws SQLException { checkClosed(); delegate.setHoldability(holdability); } @Override public int getHoldability() throws SQLException { checkClosed(); return delegate.getHoldability(); } @Override public Savepoint setSavepoint() throws SQLException { checkClosed(); return delegate.setSavepoint(); } @Override public Savepoint setSavepoint(String name) throws SQLException { checkClosed(); return delegate.setSavepoint(name); } @Override public void rollback(Savepoint savepoint) throws SQLException { checkClosed(); delegate.rollback(savepoint); } @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { checkClosed(); delegate.releaseSavepoint(savepoint); } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { checkClosed(); return delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); } @Override public Clob createClob() throws SQLException { checkClosed(); return delegate.createClob(); } @Override public Blob createBlob() throws SQLException { checkClosed(); return delegate.createBlob(); } @Override public NClob createNClob() throws SQLException { checkClosed(); return delegate.createNClob(); } @Override public SQLXML createSQLXML() throws SQLException { checkClosed(); return delegate.createSQLXML(); } @Override public boolean isValid(int timeout) throws SQLException { checkClosed(); return delegate.isValid(timeout); } @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { try { checkClosed(); delegate.setClientInfo(name, value); } catch (SQLException ex) { Map<String, ClientInfoStatus> props = new HashMap<>(); props.put(name, ClientInfoStatus.REASON_UNKNOWN); throw new SQLClientInfoException(props, ex); } } @Override public void setClientInfo(Properties properties) throws SQLClientInfoException { try { checkClosed(); delegate.setClientInfo(properties); } catch (SQLException ex) { Map<String, ClientInfoStatus> props = new HashMap<>(); for (Object key : properties.keySet()) { props.put(String.valueOf(key), ClientInfoStatus.REASON_UNKNOWN); } throw new SQLClientInfoException(props, ex); } } @Override public String getClientInfo(String name) throws SQLException { checkClosed(); return delegate.getClientInfo(name); } @Override public Properties getClientInfo() throws SQLException { checkClosed(); return delegate.getClientInfo(); } @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { checkClosed(); return delegate.createArrayOf(typeName, elements); } @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { checkClosed(); return delegate.createStruct(typeName, attributes); } @Override public <T> T unwrap(Class<T> iface) throws SQLException { checkClosed(); if (isWrapperFor(iface)) { return (T) delegate; } else { return null; } } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { checkClosed(); return iface.isAssignableFrom(delegate.getClass()); } private void checkClosed() throws SQLException { if (delegate == null) { throw new SQLException("Connection already closed."); } } protected void shrinkStatements() throws SQLException { while (!stmts.isEmpty() && currentStatements >= maxStatements) { String toRemove = stmts.keySet().iterator().next(); PreparedStatement stmt = stmts.remove(toRemove); stmt.close(); currentStatements--; } } protected void shrinkCalls() throws SQLException { while (!calls.isEmpty() && currentCalls >= maxStatements) { String toRemove = calls.keySet().iterator().next(); CallableStatement call = calls.remove(toRemove); call.close(); currentCalls--; } } public void shutdown() throws SQLException { if (delegate != null) { for (CallableStatement call : calls.values()) { call.close(); } calls.clear(); for (PreparedStatement stmt : stmts.values()) { stmt.close(); } stmts.clear(); shutdownDelegate(); } } protected void shutdownDelegate() throws SQLException { delegate.close(); } }