public static SQLException generateSQLException(String reason, Throwable t) { // slow, but this is for very exceptional cases only try { // try JDK6/7 style constructor if (sqlExceptionClass == null){ // yes there's a chance this can race but nothing bad happens except for a performance hit... sqlExceptionClass = (Class<Throwable>) Class.forName(exceptionClass); } return (SQLException)sqlExceptionClass.getConstructor(String.class, Throwable.class).newInstance(reason, t); } catch (Exception e) { // fallback to JDK5 return new SQLException(PoolUtil.stringifyException(t)); } }
@Override public void onQueryExecuteTimeLimitExceeded(ConnectionHandle handle, Statement statement, String sql, Map<Object, Object> logParams, long timeElapsedInNs) { double timeMs = timeElapsedInNs / 1000d; String query = PoolUtil.fillLogParams(sql, logParams); LOGGER.warn("Query execute time limit exceeded ({}ms) - query: {}", timeMs, query); } });
@Override protected Connection getConnectionInternal() throws SQLException { ConnectionHandle result = pollConnection(); // we still didn't find an empty one, wait forever (or as per config) until our partition is free if (result == null) { int partition = (int) (Thread.currentThread().getId() % this.pool.partitionCount); ConnectionPartition connectionPartition = this.pool.partitions[partition]; try { result = connectionPartition.getFreeConnections().poll(this.pool.connectionTimeoutInMs, TimeUnit.MILLISECONDS); if (result == null){ if (this.pool.nullOnConnectionTimeout){ return null; } // 08001 = The application requester is unable to establish the connection. throw new SQLException("Timed out waiting for a free available connection.", "08001"); } } catch (InterruptedException e) { if (this.pool.nullOnConnectionTimeout){ return null; } throw PoolUtil.generateSQLException(e.getMessage(), e); } } return result; }
config.getConnectionHook().onAcquireFail(e, acquireConfig); throw PoolUtil.generateSQLException(String.format(ERROR_TEST_CONNECTION, config.getJdbcUrl(), config.getUsername(), PoolUtil.stringifyException(e)), e);
/** * {@inheritDoc} * * @see java.sql.PreparedStatement#setTime(int, java.sql.Time, * java.util.Calendar) */ // @Override public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { checkClosed(); try { this.internalPreparedStatement.setTime(parameterIndex, x, cal); if (this.logStatementsEnabled){ this.logParams.put(parameterIndex, PoolUtil.safePrint(x, ", cal=", cal)); } } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } }
/** Helper method * @param obj item to print * @return String for pretty printing. */ protected static String prettyPrint(Object obj){ StringBuilder sb = new StringBuilder(); if (obj == null){ sb.append("NULL"); } else if (obj instanceof Blob){ sb.append(formatLogParam((Blob)obj)); } else if (obj instanceof Clob){ sb.append(formatLogParam((Clob)obj)); } else if (obj instanceof Ref){ sb.append(formatLogParam((Ref)obj)); } else if (obj instanceof Array){ sb.append(formatLogParam((Array)obj)); } else if (obj instanceof String){ sb.append("'" + obj.toString()+"'"); } else { sb.append(obj.toString()); } return sb.toString(); }
result.append(prettyPrint(it.next())); } else { result.append('?');
config.getConnectionHook().onAcquireFail(e, acquireConfig); throw PoolUtil.generateSQLException(String.format(ERROR_TEST_CONNECTION, config.getJdbcUrl(), config.getUsername(), PoolUtil.stringifyException(e)), e);
/** * {@inheritDoc} * * @see java.sql.PreparedStatement#setNull(int, int, java.lang.String) */ // @Override public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { checkClosed(); try { this.internalPreparedStatement.setNull(parameterIndex, sqlType, typeName); if (this.logStatementsEnabled){ this.logParams.put(parameterIndex, PoolUtil.safePrint("[SQL NULL of type ", sqlType, ", type = ", typeName, "]")); } } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } }
@Deprecated public void onQueryExecuteTimeLimitExceeded(String sql, Map<Object, Object> logParams){ StringBuilder sb = new StringBuilder("Query execute time limit exceeded. Query: "); sb.append(PoolUtil.fillLogParams(sql, logParams)); logger.warn(sb.toString()); }
/** * @throws SQLException * */ private void maybeInit() throws SQLException { try { if (this.getDriverClass() != null) { this.loadClass(this.getDriverClass()); } } catch (final ClassNotFoundException e) { throw new SQLException(PoolUtil.stringifyException(e)); } BoneCPDataSource.logger.debug(this.toString()); this.pool = new FinalWrapper<BoneCP>(new BoneCP(this)); }
/** * {@inheritDoc} * * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) */ public Connection getConnection(String username, String password) throws SQLException { try { return this.multiDataSource.get(new UsernamePassword(username, password)).getConnection(); } catch (ExecutionException e) { throw PoolUtil.generateSQLException("Unable to obtain connection", e); } }
/** * {@inheritDoc} * * @see java.sql.PreparedStatement#setDate(int, java.sql.Date, * java.util.Calendar) */ // @Override public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { checkClosed(); try { this.internalPreparedStatement.setDate(parameterIndex, x, cal); if (this.logStatementsEnabled){ this.logParams.put(parameterIndex, PoolUtil.safePrint(x, ", cal=", cal)); } } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } }
try{ if (this.logStatementsEnabled && logger.isDebugEnabled()){ logger.debug(PoolUtil.fillLogParams(this.batchSQL.toString(), this.logParams));
/** * {@inheritDoc} * * @see javax.sql.DataSource#getConnection() */ public Connection getConnection() throws SQLException { FinalWrapper<BoneCP> wrapper = this.pool; if (wrapper == null) { synchronized (this) { if (this.pool == null) { try{ if (this.getDriverClass() != null){ loadClass(this.getDriverClass()); } logger.debug(this.toString()); this.pool = new FinalWrapper<BoneCP>(new BoneCP(this)); } catch (ClassNotFoundException e) { throw new SQLException(PoolUtil.stringifyException(e)); } } wrapper = this.pool; } } return wrapper.value.getConnection(); }
throw PoolUtil.generateSQLException("ConnectionHandle is gone!", new IllegalStateException()); throw PoolUtil.generateSQLException(failedThrowable.getMessage(), failedThrowable);
/** * {@inheritDoc} * * @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp, * java.util.Calendar) */ // @Override public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { checkClosed(); try { this.internalPreparedStatement.setTimestamp(parameterIndex, x, cal); if (this.logStatementsEnabled){ this.logParams.put(parameterIndex, PoolUtil.safePrint(x, ", cal=", cal)); } } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } }
/** * {@inheritDoc} * * @see java.sql.Statement#executeUpdate(java.lang.String, int[]) */ // @Override public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { int result = 0; checkClosed(); try{ if (this.logStatementsEnabled && logger.isDebugEnabled()){ logger.debug(PoolUtil.fillLogParams(sql, this.logParams), columnIndexes); } long queryStartTime = queryTimerStart(); if (this.connectionHook != null){ this.connectionHook.onBeforeStatementExecute(this.connectionHandle, this, sql, this.logParams); } result = this.internalStatement.executeUpdate(sql, columnIndexes); if (this.connectionHook != null){ this.connectionHook.onAfterStatementExecute(this.connectionHandle, this, sql, this.logParams); } queryTimerEnd(sql, queryStartTime); } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } return result; }
/** * {@inheritDoc} * * @see java.sql.CallableStatement#setNull(java.lang.String, int) */ public void setNull(String parameterName, int sqlType) throws SQLException { checkClosed(); try { this.internalCallableStatement.setNull(parameterName, sqlType); if (this.logStatementsEnabled){ this.logParams.put(parameterName, PoolUtil.safePrint("[SQL NULL type ", sqlType, "]")); } } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } }
/** * {@inheritDoc} * * @see java.sql.Statement#execute(java.lang.String, int) */ // @Override public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { boolean result = false; checkClosed(); try{ if (this.logStatementsEnabled && logger.isDebugEnabled()){ logger.debug(PoolUtil.fillLogParams(sql, this.logParams)); } long queryStartTime = queryTimerStart(); if (this.connectionHook != null){ this.connectionHook.onBeforeStatementExecute(this.connectionHandle, this, sql, this.logParams); } result = this.internalStatement.execute(sql, autoGeneratedKeys); if (this.connectionHook != null){ this.connectionHook.onAfterStatementExecute(this.connectionHandle, this, sql, this.logParams); } queryTimerEnd(sql, queryStartTime); } catch (SQLException e) { throw this.connectionHandle.markPossiblyBroken(e); } return result; }