com.jolbox.bonecp
Class ConnectionHandle

java.lang.Object
  extended by com.jolbox.bonecp.ConnectionHandle
All Implemented Interfaces:
Connection, Wrapper

public class ConnectionHandle
extends Object
implements Connection

Connection handle wrapper around a JDBC connection.

Author:
wwadge

Field Summary
protected  boolean logicallyClosed
          If true, we've called close() on this connection.
protected  boolean possiblyBroken
          If true, this connection might have failed communicating with the database.
protected  TransactionRecoveryResult recoveryResult
          Map of translations + result from last recovery.
protected  boolean statementCachingEnabled
          Set to true if we have statement caching enabled.
protected  Thread threadUsingConnection
          Keep track of the thread.
protected  String url
          Connection url.
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
ConnectionHandle(Connection connection, IStatementCache preparedStatementCache, IStatementCache callableStatementCache, BoneCP pool)
          Private constructor used solely for unit testing.
ConnectionHandle(String url, String username, String password, BoneCP pool)
          Connection wrapper constructor
 
Method Summary
protected  void clearStatementCaches(boolean internalClose)
          Clears out the statement handles.
 void clearWarnings()
           
 void close()
          Release the connection if called.
 void commit()
           
 Array createArrayOf(String typeName, Object[] elements)
           
 Blob createBlob()
           
 Clob createClob()
           
 NClob createNClob()
           
 SQLXML createSQLXML()
           
 Statement createStatement()
           
 Statement createStatement(int resultSetType, int resultSetConcurrency)
           
 Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
           
 Struct createStruct(String typeName, Object[] attributes)
           
 boolean getAutoCommit()
           
 String getCatalog()
           
 Properties getClientInfo()
           
 String getClientInfo(String name)
           
 long getConnectionCreationTime()
          Deprecated. please use getConnectionCreationTimeInMs() instead.
 long getConnectionCreationTimeInMs()
          Returns the connectionCreationTime field.
 ConnectionHook getConnectionHook()
          Returns the configured connection hook object.
 long getConnectionLastReset()
          Deprecated. Please use getConnectionLastResetInMs() instead
 long getConnectionLastResetInMs()
           
 long getConnectionLastUsed()
          Deprecated. Use getConnectionLastUsedInMs() instead.
 long getConnectionLastUsedInMs()
           
 Object getDebugHandle()
          Returns a debug handle as previously set by an application
 int getHoldability()
           
 Connection getInternalConnection()
          Returns the internal connection as obtained via the JDBC driver.
 DatabaseMetaData getMetaData()
           
 ConnectionPartition getOriginatingPartition()
          Gets the partition this came from.
 BoneCP getPool()
          Returns a handle to the global pool from where this connection was obtained.
 Object getProxyTarget()
          This method will be intercepted by the proxy if it is enabled to return the internal target.
 Connection getRawConnection()
          Deprecated. 
 List<ReplayLog> getReplayLog()
          Returns transaction history log
 Thread getThreadUsingConnection()
          Returns the thread that is currently utilizing this connection.
 Thread getThreadWatch()
          Returns the thread watching over this connection.
 int getTransactionIsolation()
           
 Map<String,Class<?>> getTypeMap()
           
 SQLWarning getWarnings()
           
protected  void internalClose()
          Close off the connection.
 boolean isClosed()
          Returns true if this connection has been (logically) closed.
 boolean isConnectionAlive()
          Sends a test query to the underlying connection and return true if connection is alive.
 boolean isExpired()
          Returns true if the given connection has exceeded the maxConnectionAge.
protected  boolean isExpired(long currentTime)
          Returns true if the given connection has exceeded the maxConnectionAge.
protected  boolean isInReplayMode()
           
 boolean isLogStatementsEnabled()
          Returns true if logging of statements is enabled
 boolean isPossiblyBroken()
          Gets true if connection has triggered an exception at some point.
 boolean isReadOnly()
           
 boolean isValid(int timeout)
           
 boolean isWrapperFor(Class<?> iface)
           
protected  SQLException markPossiblyBroken(SQLException e)
          Given an exception, flag the connection (or database) as being potentially broken.
 String nativeSQL(String sql)
           
protected  Connection obtainInternalConnection()
          Obtains a database connection, retrying if necessary.
 CallableStatement prepareCall(String sql)
           
 CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
           
 CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
           
 PreparedStatement prepareStatement(String sql)
           
 PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
           
 PreparedStatement prepareStatement(String sql, int[] columnIndexes)
           
 PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
           
 PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
           
 PreparedStatement prepareStatement(String sql, String[] columnNames)
           
 void releaseSavepoint(Savepoint savepoint)
           
protected  void renewConnection()
          Renews this connection, i.e.
 void rollback()
           
 void rollback(Savepoint savepoint)
           
 void sendInitSQL()
          Sends any configured SQL init statement.
 void setAutoCommit(boolean autoCommit)
           
 void setCatalog(String catalog)
           
 void setClientInfo(Properties properties)
           
 void setClientInfo(String name, String value)
           
protected  void setConnectionLastResetInMs(long connectionLastReset)
           
protected  void setConnectionLastUsedInMs(long connectionLastUsed)
           
 void setDebugHandle(Object debugHandle)
          Sets a debugHandle, an object that is not used by the connection pool at all but may be set by an application to track this particular connection handle for any purpose it deems fit.
 void setHoldability(int holdability)
           
protected  void setInReplayMode(boolean inReplayMode)
           
 void setInternalConnection(Connection rawConnection)
          Sets the internal connection to use.
 void setLogStatementsEnabled(boolean logStatementsEnabled)
          Enable or disable logging of this connection.
protected  void setOriginatingPartition(ConnectionPartition originatingPartition)
          Sets Originating partition
 void setReadOnly(boolean readOnly)
           
protected  void setReplayLog(List<ReplayLog> replayLog)
          Sets the transaction history log
 Savepoint setSavepoint()
           
 Savepoint setSavepoint(String name)
           
protected  void setThreadWatch(Thread threadWatch)
          Sets the thread watching over this connection.
 void setTransactionIsolation(int level)
           
 void setTypeMap(Map<String,Class<?>> map)
           
<T> T
unwrap(Class<T> iface)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

possiblyBroken

protected boolean possiblyBroken
If true, this connection might have failed communicating with the database. We assume that exceptions should be rare here i.e. the normal case is assumed to succeed.


logicallyClosed

protected boolean logicallyClosed
If true, we've called close() on this connection.


statementCachingEnabled

protected boolean statementCachingEnabled
Set to true if we have statement caching enabled.


recoveryResult

protected TransactionRecoveryResult recoveryResult
Map of translations + result from last recovery.


url

protected String url
Connection url.


threadUsingConnection

protected Thread threadUsingConnection
Keep track of the thread.

Constructor Detail

ConnectionHandle

public ConnectionHandle(String url,
                        String username,
                        String password,
                        BoneCP pool)
                 throws SQLException
Connection wrapper constructor

Parameters:
url - JDBC connection string
username - user to use
password - password for db
pool - pool handle.
Throws:
SQLException - on error

ConnectionHandle

public ConnectionHandle(Connection connection,
                        IStatementCache preparedStatementCache,
                        IStatementCache callableStatementCache,
                        BoneCP pool)
Private constructor used solely for unit testing.

Parameters:
connection -
preparedStatementCache -
callableStatementCache -
pool -
Method Detail

obtainInternalConnection

protected Connection obtainInternalConnection()
                                       throws SQLException
Obtains a database connection, retrying if necessary.

Returns:
A DB connection.
Throws:
SQLException

sendInitSQL

public void sendInitSQL()
                 throws SQLException
Sends any configured SQL init statement.

Throws:
SQLException - on error

markPossiblyBroken

protected SQLException markPossiblyBroken(SQLException e)
Given an exception, flag the connection (or database) as being potentially broken. If the exception is a data-specific exception, do nothing except throw it back to the application.

Parameters:
e - SQLException e
Returns:
SQLException for further processing

clearWarnings

public void clearWarnings()
                   throws SQLException
Specified by:
clearWarnings in interface Connection
Throws:
SQLException

close

public void close()
           throws SQLException
Release the connection if called.

Specified by:
close in interface Connection
Throws:
SQLException - Never really thrown

internalClose

protected void internalClose()
                      throws SQLException
Close off the connection.

Throws:
SQLException

commit

public void commit()
            throws SQLException
Specified by:
commit in interface Connection
Throws:
SQLException

getClientInfo

public Properties getClientInfo()
                         throws SQLException
Specified by:
getClientInfo in interface Connection
Throws:
SQLException

getClientInfo

public String getClientInfo(String name)
                     throws SQLException
Specified by:
getClientInfo in interface Connection
Throws:
SQLException

isValid

public boolean isValid(int timeout)
                throws SQLException
Specified by:
isValid in interface Connection
Throws:
SQLException

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Specified by:
isWrapperFor in interface Wrapper
Throws:
SQLException

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Specified by:
unwrap in interface Wrapper
Throws:
SQLException

setClientInfo

public void setClientInfo(Properties properties)
                   throws SQLClientInfoException
Specified by:
setClientInfo in interface Connection
Throws:
SQLClientInfoException

setClientInfo

public void setClientInfo(String name,
                          String value)
                   throws SQLClientInfoException
Specified by:
setClientInfo in interface Connection
Throws:
SQLClientInfoException

createStruct

public Struct createStruct(String typeName,
                           Object[] attributes)
                    throws SQLException
Specified by:
createStruct in interface Connection
Throws:
SQLException

createArrayOf

public Array createArrayOf(String typeName,
                           Object[] elements)
                    throws SQLException
Specified by:
createArrayOf in interface Connection
Throws:
SQLException

createBlob

public Blob createBlob()
                throws SQLException
Specified by:
createBlob in interface Connection
Throws:
SQLException

createClob

public Clob createClob()
                throws SQLException
Specified by:
createClob in interface Connection
Throws:
SQLException

createNClob

public NClob createNClob()
                  throws SQLException
Specified by:
createNClob in interface Connection
Throws:
SQLException

createSQLXML

public SQLXML createSQLXML()
                    throws SQLException
Specified by:
createSQLXML in interface Connection
Throws:
SQLException

createStatement

public Statement createStatement()
                          throws SQLException
Specified by:
createStatement in interface Connection
Throws:
SQLException

createStatement

public Statement createStatement(int resultSetType,
                                 int resultSetConcurrency)
                          throws SQLException
Specified by:
createStatement in interface Connection
Throws:
SQLException

createStatement

public Statement createStatement(int resultSetType,
                                 int resultSetConcurrency,
                                 int resultSetHoldability)
                          throws SQLException
Specified by:
createStatement in interface Connection
Throws:
SQLException

getAutoCommit

public boolean getAutoCommit()
                      throws SQLException
Specified by:
getAutoCommit in interface Connection
Throws:
SQLException

getCatalog

public String getCatalog()
                  throws SQLException
Specified by:
getCatalog in interface Connection
Throws:
SQLException

getHoldability

public int getHoldability()
                   throws SQLException
Specified by:
getHoldability in interface Connection
Throws:
SQLException

getMetaData

public DatabaseMetaData getMetaData()
                             throws SQLException
Specified by:
getMetaData in interface Connection
Throws:
SQLException

getTransactionIsolation

public int getTransactionIsolation()
                            throws SQLException
Specified by:
getTransactionIsolation in interface Connection
Throws:
SQLException

getTypeMap

public Map<String,Class<?>> getTypeMap()
                                throws SQLException
Specified by:
getTypeMap in interface Connection
Throws:
SQLException

getWarnings

public SQLWarning getWarnings()
                       throws SQLException
Specified by:
getWarnings in interface Connection
Throws:
SQLException

isClosed

public boolean isClosed()
Returns true if this connection has been (logically) closed.

Specified by:
isClosed in interface Connection
Returns:
the logicallyClosed setting.

isReadOnly

public boolean isReadOnly()
                   throws SQLException
Specified by:
isReadOnly in interface Connection
Throws:
SQLException

nativeSQL

public String nativeSQL(String sql)
                 throws SQLException
Specified by:
nativeSQL in interface Connection
Throws:
SQLException

prepareCall

public CallableStatement prepareCall(String sql)
                              throws SQLException
Specified by:
prepareCall in interface Connection
Throws:
SQLException

prepareCall

public CallableStatement prepareCall(String sql,
                                     int resultSetType,
                                     int resultSetConcurrency)
                              throws SQLException
Specified by:
prepareCall in interface Connection
Throws:
SQLException

prepareCall

public CallableStatement prepareCall(String sql,
                                     int resultSetType,
                                     int resultSetConcurrency,
                                     int resultSetHoldability)
                              throws SQLException
Specified by:
prepareCall in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          int autoGeneratedKeys)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          int[] columnIndexes)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          String[] columnNames)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          int resultSetType,
                                          int resultSetConcurrency)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          int resultSetType,
                                          int resultSetConcurrency,
                                          int resultSetHoldability)
                                   throws SQLException
Specified by:
prepareStatement in interface Connection
Throws:
SQLException

releaseSavepoint

public void releaseSavepoint(Savepoint savepoint)
                      throws SQLException
Specified by:
releaseSavepoint in interface Connection
Throws:
SQLException

rollback

public void rollback()
              throws SQLException
Specified by:
rollback in interface Connection
Throws:
SQLException

rollback

public void rollback(Savepoint savepoint)
              throws SQLException
Specified by:
rollback in interface Connection
Throws:
SQLException

setAutoCommit

public void setAutoCommit(boolean autoCommit)
                   throws SQLException
Specified by:
setAutoCommit in interface Connection
Throws:
SQLException

setCatalog

public void setCatalog(String catalog)
                throws SQLException
Specified by:
setCatalog in interface Connection
Throws:
SQLException

setHoldability

public void setHoldability(int holdability)
                    throws SQLException
Specified by:
setHoldability in interface Connection
Throws:
SQLException

setReadOnly

public void setReadOnly(boolean readOnly)
                 throws SQLException
Specified by:
setReadOnly in interface Connection
Throws:
SQLException

setSavepoint

public Savepoint setSavepoint()
                       throws SQLException
Specified by:
setSavepoint in interface Connection
Throws:
SQLException

setSavepoint

public Savepoint setSavepoint(String name)
                       throws SQLException
Specified by:
setSavepoint in interface Connection
Throws:
SQLException

setTransactionIsolation

public void setTransactionIsolation(int level)
                             throws SQLException
Specified by:
setTransactionIsolation in interface Connection
Throws:
SQLException

setTypeMap

public void setTypeMap(Map<String,Class<?>> map)
                throws SQLException
Specified by:
setTypeMap in interface Connection
Throws:
SQLException

getConnectionLastUsedInMs

public long getConnectionLastUsedInMs()
Returns:
the connectionLastUsed

getConnectionLastUsed

@Deprecated
public long getConnectionLastUsed()
Deprecated. Use getConnectionLastUsedInMs() instead.

Deprecated. Use getConnectionLastUsedInMs() instead.

Returns:
the connectionLastUsed

setConnectionLastUsedInMs

protected void setConnectionLastUsedInMs(long connectionLastUsed)
Parameters:
connectionLastUsed - the connectionLastUsed to set

getConnectionLastResetInMs

public long getConnectionLastResetInMs()
Returns:
the connectionLastReset

getConnectionLastReset

@Deprecated
public long getConnectionLastReset()
Deprecated. Please use getConnectionLastResetInMs() instead

Deprecated. Use getConnectionLastResetInMs() instead.

Returns:
the connectionLastReset

setConnectionLastResetInMs

protected void setConnectionLastResetInMs(long connectionLastReset)
Parameters:
connectionLastReset - the connectionLastReset to set

isPossiblyBroken

public boolean isPossiblyBroken()
Gets true if connection has triggered an exception at some point.

Returns:
true if the connection has triggered an error

getOriginatingPartition

public ConnectionPartition getOriginatingPartition()
Gets the partition this came from.

Returns:
the partition this came from

setOriginatingPartition

protected void setOriginatingPartition(ConnectionPartition originatingPartition)
Sets Originating partition

Parameters:
originatingPartition - to set

renewConnection

protected void renewConnection()
Renews this connection, i.e. Sets this connection to be logically open (although it was never really physically closed)


clearStatementCaches

protected void clearStatementCaches(boolean internalClose)
Clears out the statement handles.

Parameters:
internalClose - if true, close the inner statement handle too.

getDebugHandle

public Object getDebugHandle()
Returns a debug handle as previously set by an application

Returns:
DebugHandle

setDebugHandle

public void setDebugHandle(Object debugHandle)
Sets a debugHandle, an object that is not used by the connection pool at all but may be set by an application to track this particular connection handle for any purpose it deems fit.

Parameters:
debugHandle - any object.

getRawConnection

@Deprecated
public Connection getRawConnection()
Deprecated. 

Deprecated. Please use getInternalConnection() instead.

Returns:
the raw connection

getInternalConnection

public Connection getInternalConnection()
Returns the internal connection as obtained via the JDBC driver.

Returns:
the raw connection

getConnectionHook

public ConnectionHook getConnectionHook()
Returns the configured connection hook object.

Returns:
the connectionHook that was set in the config

isLogStatementsEnabled

public boolean isLogStatementsEnabled()
Returns true if logging of statements is enabled

Returns:
logStatementsEnabled status

setLogStatementsEnabled

public void setLogStatementsEnabled(boolean logStatementsEnabled)
Enable or disable logging of this connection.

Parameters:
logStatementsEnabled - true to enable logging, false to disable.

isInReplayMode

protected boolean isInReplayMode()
Returns:
the inReplayMode

setInReplayMode

protected void setInReplayMode(boolean inReplayMode)
Parameters:
inReplayMode - the inReplayMode to set

isConnectionAlive

public boolean isConnectionAlive()
Sends a test query to the underlying connection and return true if connection is alive.

Returns:
True if connection is valid, false otherwise.

setInternalConnection

public void setInternalConnection(Connection rawConnection)
Sets the internal connection to use. Be careful how to use this method, normally you should never need it! This is here for odd use cases only!

Parameters:
rawConnection - to set

getPool

public BoneCP getPool()
Returns a handle to the global pool from where this connection was obtained.

Returns:
BoneCP handle

getReplayLog

public List<ReplayLog> getReplayLog()
Returns transaction history log

Returns:
replay list

setReplayLog

protected void setReplayLog(List<ReplayLog> replayLog)
Sets the transaction history log

Parameters:
replayLog - to set.

getProxyTarget

public Object getProxyTarget()
This method will be intercepted by the proxy if it is enabled to return the internal target.

Returns:
the target.

getThreadUsingConnection

public Thread getThreadUsingConnection()
Returns the thread that is currently utilizing this connection.

Returns:
the threadUsingConnection

getConnectionCreationTime

@Deprecated
public long getConnectionCreationTime()
Deprecated. please use getConnectionCreationTimeInMs() instead.

Deprecated. Use getConnectionCreationTimeInMs() instead.

Returns:
connectionCreationTime

getConnectionCreationTimeInMs

public long getConnectionCreationTimeInMs()
Returns the connectionCreationTime field.

Returns:
connectionCreationTime

isExpired

public boolean isExpired()
Returns true if the given connection has exceeded the maxConnectionAge.

Returns:
true if the connection has expired.

isExpired

protected boolean isExpired(long currentTime)
Returns true if the given connection has exceeded the maxConnectionAge.

Parameters:
currentTime - current time to use.
Returns:
true if the connection has expired.

setThreadWatch

protected void setThreadWatch(Thread threadWatch)
Sets the thread watching over this connection.

Parameters:
threadWatch - the threadWatch to set

getThreadWatch

public Thread getThreadWatch()
Returns the thread watching over this connection.

Returns:
threadWatch


Copyright © 2009-2011 JolBox. All Rights Reserved.