org.logicalcobwebs.proxool
Class ProxyConnection

java.lang.Object
  extended byorg.logicalcobwebs.proxool.AbstractProxyConnection
      extended byorg.logicalcobwebs.proxool.ProxyConnection
All Implemented Interfaces:
Callback, java.lang.Comparable, ConnectionInfoIF, InvocationHandler, MethodInterceptor, ProxyConnectionIF

class ProxyConnection
extends AbstractProxyConnection
implements InvocationHandler, MethodInterceptor

Delegates to a normal Coonection for everything but the close() method (when it puts itself back into the pool instead).

Version:
$Revision: 1.29 $, $Date: 2003/12/12 19:29:47 $
Author:
billhorsman, $Author: billhorsman $ (current maintainer)

Field Summary
private static java.lang.String CLOSE_METHOD
           
private static java.lang.String EQUALS_METHOD
           
private static java.lang.String FINALIZE_METHOD
           
private static java.lang.String GET_META_DATA_METHOD
           
private static java.lang.String IS_CLOSED_METHOD
           
private static Log LOG
           
(package private) static int STATUS_FORCE
           
 
Fields inherited from interface org.logicalcobwebs.proxool.ConnectionInfoIF
MARK_FOR_EXPIRY, MARK_FOR_USE, STATUS_ACTIVE, STATUS_AVAILABLE, STATUS_NULL, STATUS_OFFLINE
 
Constructor Summary
ProxyConnection(java.sql.Connection connection, long id, java.lang.String delegateUrl, ConnectionPool connectionPool, int status)
           
 
Method Summary
protected  void addOpenStatement(java.sql.Statement statement)
          By calling this we can keep track of any statements that are left open when this connection is returned to the pool.
 void close()
          Doesn't really close the connection, just puts it back in the pool.
 int compareTo(java.lang.Object o)
          Compares using AbstractProxyConnection.getId()
 boolean equals(java.lang.Object obj)
          Whether the underlying connections are equal
 long getAge()
          The age in millseconds since this connection was built
 java.util.Date getBirthDate()
          Like AbstractProxyConnection.getBirthTime() but in Date format
 long getBirthTime()
          The time that this connection was created.
 java.sql.Connection getConnection()
          The real, delegate connection that we are using
protected  ConnectionPool getConnectionPool()
          The ConnectionPool that was used to create this connection
 java.lang.String getDelegateHashcode()
          The hashcode (in hex) of the delegate connection object.
 java.lang.String getDelegateUrl()
          The URL that this connection is using (the definition might have changed since this connection was built).
 long getId()
          A unique ID for this connection
 int getMark()
          Sometimes we want do something to a connection but can't because it is still active and we don't want to disrupt its use.
 java.sql.DatabaseMetaData getMetaData()
           
 java.lang.String getProxyHashcode()
          The hashcode (in hex) of the ProxyConnection object.
 java.lang.String getReasonForMark()
          Why this connection is marked (for instance, if a thread has marked it for expiry then it's nice to know why)
 java.lang.String getRequester()
          The name of the thread that asked for this connection.
 int getStatus()
          The status of the connection.
 long getTimeLastStartActive()
          When this connection was last given out.
 long getTimeLastStopActive()
          When this connection was last given back (or zero if it is still active).
 java.lang.Object intercept(java.lang.Object obj, java.lang.reflect.Method method, java.lang.Object[] args, MethodProxy proxy)
          All generated proxied methods call this method instead of the original method.
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
           
 boolean isActive()
           
 boolean isAvailable()
           
 boolean isClosed()
          Whether this connection is available.
 boolean isMarkedForExpiry()
          Whether this connection is due for expiry
 boolean isNull()
           
 boolean isOffline()
           
 boolean isReallyClosed()
          Find out if the delegated connection is close.
 void markForExpiry(java.lang.String reason)
          Mark this connection for expiry (destruction) as soon as it stops being active.
 void reallyClose()
          Close the connection for real
 void registerClosedStatement(java.sql.Statement statement)
          Notify that a statement has been closed and won't need closing when the connection is returned to the poo.
 void setBirthTime(long birthTime)
           
 void setId(long id)
           
protected  void setNeedToReset(boolean needToReset)
          The subclass should call this to indicate that a change has been made to the connection that might mean it needs to be reset (like setting autoCommit to false or something).
 void setRequester(java.lang.String requester)
           
 boolean setStatus(int newStatus)
          Forces the new status regardless of the old state
 boolean setStatus(int oldStatus, int newStatus)
          Changes the status and lets the ConnectionPool know so that it can keep count of how many connections are at each status.
 void setTimeLastStartActive(long timeLastStartActive)
           
 void setTimeLastStopActive(long timeLastStopActive)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

private static final Log LOG

CLOSE_METHOD

private static final java.lang.String CLOSE_METHOD
See Also:
Constant Field Values

IS_CLOSED_METHOD

private static final java.lang.String IS_CLOSED_METHOD
See Also:
Constant Field Values

EQUALS_METHOD

private static final java.lang.String EQUALS_METHOD
See Also:
Constant Field Values

GET_META_DATA_METHOD

private static final java.lang.String GET_META_DATA_METHOD
See Also:
Constant Field Values

FINALIZE_METHOD

private static final java.lang.String FINALIZE_METHOD
See Also:
Constant Field Values

STATUS_FORCE

static final int STATUS_FORCE
See Also:
Constant Field Values
Constructor Detail

ProxyConnection

public ProxyConnection(java.sql.Connection connection,
                       long id,
                       java.lang.String delegateUrl,
                       ConnectionPool connectionPool,
                       int status)
                throws java.sql.SQLException
Method Detail

intercept

public java.lang.Object intercept(java.lang.Object obj,
                                  java.lang.reflect.Method method,
                                  java.lang.Object[] args,
                                  MethodProxy proxy)
                           throws java.lang.Throwable
Description copied from interface: MethodInterceptor
All generated proxied methods call this method instead of the original method. The original method may either be invoked by normal reflection using the Method object, or by using the MethodProxy (faster).

Specified by:
intercept in interface MethodInterceptor
Parameters:
obj - "this", the enhanced object
method - intercepted Method
args - argument array; primitive types are wrapped
proxy - used to invoke super (non-intercepted method); may be called as many times as needed
Returns:
any value compatible with the signature of the proxied method. Method returning void will ignore this value.
Throws:
java.lang.Throwable - any exception may be thrown; if so, super method will not be invoked
See Also:
MethodProxy

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Specified by:
invoke in interface InvocationHandler
Throws:
java.lang.Throwable
See Also:
java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object)

equals

public boolean equals(java.lang.Object obj)
Whether the underlying connections are equal

Parameters:
obj - the object (probably another connection) that we are being compared to
Returns:
whether they are the same

isClosed

public boolean isClosed()
Whether this connection is available. (When you close the connection it doesn't really close, it just becomes available for others to use).

Returns:
true if the connection is not active

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Throws:
java.sql.SQLException

setNeedToReset

protected void setNeedToReset(boolean needToReset)
The subclass should call this to indicate that a change has been made to the connection that might mean it needs to be reset (like setting autoCommit to false or something). We don't reset unless this has been called to avoid the overhead of unnecessary resetting.

Parameters:
needToReset - true if the connection might need resetting.

getConnectionPool

protected ConnectionPool getConnectionPool()
The ConnectionPool that was used to create this connection

Returns:
connectionPool

addOpenStatement

protected void addOpenStatement(java.sql.Statement statement)
By calling this we can keep track of any statements that are left open when this connection is returned to the pool.

Parameters:
statement - the statement that we have just opened/created.
See Also:
AbstractProxyConnection.registerClosedStatement(java.sql.Statement)

registerClosedStatement

public void registerClosedStatement(java.sql.Statement statement)
Description copied from interface: ProxyConnectionIF
Notify that a statement has been closed and won't need closing when the connection is returned to the poo.

Specified by:
registerClosedStatement in interface ProxyConnectionIF
Parameters:
statement - the statement that has just been closed
See Also:
ProxyConnectionIF.registerClosedStatement(java.sql.Statement)

reallyClose

public void reallyClose()
                 throws java.sql.SQLException
Close the connection for real

Specified by:
reallyClose in interface ProxyConnectionIF
Throws:
java.sql.SQLException - if anything goes wrong

isReallyClosed

public boolean isReallyClosed()
                       throws java.sql.SQLException
Description copied from interface: ProxyConnectionIF
Find out if the delegated connection is close. Just calling isClosed() on the proxied connection will only indicate whether it is in the pool or not.

Specified by:
isReallyClosed in interface ProxyConnectionIF
Returns:
true if the connection is really closed, or if the connection is null
Throws:
java.sql.SQLException - if anything went wrong
See Also:
ProxyConnectionIF.isReallyClosed()

close

public void close()
           throws java.sql.SQLException
Description copied from interface: ProxyConnectionIF
Doesn't really close the connection, just puts it back in the pool. And tries to reset all the methods that need resetting.

Specified by:
close in interface ProxyConnectionIF
Throws:
java.sql.SQLException
See Also:
ProxyConnectionIF.close()

getMark

public int getMark()
Description copied from interface: ConnectionInfoIF
Sometimes we want do something to a connection but can't because it is still active and we don't want to disrupt its use. So we mark it instead and when it stops being active we can perform the necessary operation. The only thing we do at the moment is expire the connection (if it is too old for instance). And this will happen if the housekeeper decides it should but the connection is still active.

Specified by:
getMark in interface ConnectionInfoIF

getStatus

public int getStatus()
Description copied from interface: ConnectionInfoIF
The status of the connection. Can be either: null, available, active or offline.

Specified by:
getStatus in interface ConnectionInfoIF

setStatus

public boolean setStatus(int newStatus)
Description copied from interface: ProxyConnectionIF
Forces the new status regardless of the old state

Specified by:
setStatus in interface ProxyConnectionIF
Parameters:
newStatus - the status to change to
Returns:
true if status changed successfully, or false if no change made (should always return true)
See Also:
ProxyConnectionIF.setStatus(int)

setStatus

public boolean setStatus(int oldStatus,
                         int newStatus)
Description copied from interface: ProxyConnectionIF
Changes the status and lets the ConnectionPool know so that it can keep count of how many connections are at each status. This method obtains a write lock.

Specified by:
setStatus in interface ProxyConnectionIF
Parameters:
oldStatus - the expected existing status. if the existing status is not this value then no change is made and false is returned.
newStatus - the status to change to
Returns:
true if status changed successfully, or false if no change made (because of unexpected existing status).
See Also:
ProxyConnectionIF.setStatus(int, int)

getId

public long getId()
Description copied from interface: ConnectionInfoIF
A unique ID for this connection

Specified by:
getId in interface ConnectionInfoIF

setId

public void setId(long id)

getBirthTime

public long getBirthTime()
Description copied from interface: ConnectionInfoIF
The time that this connection was created. The number of milliseconds since midnight, January 1, 1970 UTC.

Specified by:
getBirthTime in interface ConnectionInfoIF
See Also:
ConnectionInfoIF.getBirthTime()

getBirthDate

public java.util.Date getBirthDate()
Description copied from interface: ConnectionInfoIF
Like ConnectionInfoIF.getBirthTime() but in Date format

Specified by:
getBirthDate in interface ConnectionInfoIF
Returns:
birthDate
See Also:
ConnectionInfoIF.getBirthDate()

getAge

public long getAge()
Description copied from interface: ConnectionInfoIF
The age in millseconds since this connection was built

Specified by:
getAge in interface ConnectionInfoIF
See Also:
ConnectionInfoIF.getAge()

setBirthTime

public void setBirthTime(long birthTime)
See Also:
ConnectionInfoIF.getBirthTime()

getTimeLastStartActive

public long getTimeLastStartActive()
Description copied from interface: ConnectionInfoIF
When this connection was last given out. The number of milliseconds since midnight, January 1, 1970 UTC.

Specified by:
getTimeLastStartActive in interface ConnectionInfoIF
See Also:
ConnectionInfoIF.getTimeLastStartActive()

setTimeLastStartActive

public void setTimeLastStartActive(long timeLastStartActive)
See Also:
ConnectionInfoIF.getTimeLastStartActive()

getTimeLastStopActive

public long getTimeLastStopActive()
Description copied from interface: ConnectionInfoIF
When this connection was last given back (or zero if it is still active). The number of milliseconds since midnight, January 1, 1970 UTC.

Specified by:
getTimeLastStopActive in interface ConnectionInfoIF
See Also:
ConnectionInfoIF.getTimeLastStopActive()

setTimeLastStopActive

public void setTimeLastStopActive(long timeLastStopActive)
See Also:
ConnectionInfoIF.getTimeLastStopActive()

getRequester

public java.lang.String getRequester()
Description copied from interface: ConnectionInfoIF
The name of the thread that asked for this connection.

Specified by:
getRequester in interface ConnectionInfoIF
See Also:
ConnectionInfoIF.getRequester()

setRequester

public void setRequester(java.lang.String requester)
Specified by:
setRequester in interface ProxyConnectionIF
See Also:
ConnectionInfoIF.getRequester()

isNull

public boolean isNull()
Specified by:
isNull in interface ProxyConnectionIF
Returns:
true if the status is null
See Also:
ProxyConnectionIF.isNull()

isAvailable

public boolean isAvailable()
Specified by:
isAvailable in interface ProxyConnectionIF
Returns:
true if the status is available
See Also:
ProxyConnectionIF.isAvailable()

isActive

public boolean isActive()
Specified by:
isActive in interface ProxyConnectionIF
Returns:
true if the status is active
See Also:
ProxyConnectionIF.isActive()

isOffline

public boolean isOffline()
Specified by:
isOffline in interface ProxyConnectionIF
Returns:
true if the status is offline
See Also:
ProxyConnectionIF.isOffline()

markForExpiry

public void markForExpiry(java.lang.String reason)
Description copied from interface: ProxyConnectionIF
Mark this connection for expiry (destruction) as soon as it stops being active.

Specified by:
markForExpiry in interface ProxyConnectionIF
Parameters:
reason - why we are marking this connection
See Also:
ProxyConnectionIF.markForExpiry(java.lang.String)

isMarkedForExpiry

public boolean isMarkedForExpiry()
Description copied from interface: ProxyConnectionIF
Whether this connection is due for expiry

Specified by:
isMarkedForExpiry in interface ProxyConnectionIF
Returns:
true if it is due for expiry
See Also:
ProxyConnectionIF.isMarkedForExpiry()

getReasonForMark

public java.lang.String getReasonForMark()
Description copied from interface: ProxyConnectionIF
Why this connection is marked (for instance, if a thread has marked it for expiry then it's nice to know why)

Specified by:
getReasonForMark in interface ProxyConnectionIF
Returns:
reasonForMark
See Also:
ProxyConnectionIF.getReasonForMark()

getConnection

public java.sql.Connection getConnection()
Description copied from interface: ProxyConnectionIF
The real, delegate connection that we are using

Specified by:
getConnection in interface ProxyConnectionIF
Returns:
connection
See Also:
ProxyConnectionIF.getConnection()

toString

public java.lang.String toString()
See Also:
Object.toString()

getDelegateUrl

public java.lang.String getDelegateUrl()
Description copied from interface: ConnectionInfoIF
The URL that this connection is using (the definition might have changed since this connection was built).

Specified by:
getDelegateUrl in interface ConnectionInfoIF
Returns:
delegateUrl
See Also:
ConnectionInfoIF.getDelegateUrl()

getProxyHashcode

public java.lang.String getProxyHashcode()
Description copied from interface: ConnectionInfoIF
The hashcode (in hex) of the ProxyConnection object. This uniquely identifies this proxy connection.

Specified by:
getProxyHashcode in interface ConnectionInfoIF
Returns:
proxyHashcode
See Also:
ConnectionInfoIF.getProxyHashcode()

getDelegateHashcode

public java.lang.String getDelegateHashcode()
Description copied from interface: ConnectionInfoIF
The hashcode (in hex) of the delegate connection object. This uniquely identifies the underlying connection.

Specified by:
getDelegateHashcode in interface ConnectionInfoIF
Returns:
delegateHashcode
See Also:
ConnectionInfoIF.getDelegateHashcode()

compareTo

public int compareTo(java.lang.Object o)
Compares using AbstractProxyConnection.getId()

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - must be another ConnectionInfoIF implementation
Returns:
the comparison
See Also:
Comparable.compareTo(Object)