org.logicalcobwebs.proxool
Class ProxyConnection

java.lang.Object
  extended by org.logicalcobwebs.proxool.ProxyConnection
All Implemented Interfaces:
java.lang.Comparable, ConnectionInfoIF, ProxyConnectionIF

public class ProxyConnection
extends java.lang.Object
implements ProxyConnectionIF

Manages a connection. This is wrapped up inside a...

Since:
Proxool 0.10
Version:
$Revision: 1.36 $, $Date: 2005/10/07 08:18:24 $
Author:
bill, $Author: billhorsman $ (current maintainer)

Field Summary
private  java.util.Date birthDate
           
private  java.sql.Connection connection
           
private  ConnectionPool connectionPool
           
private  ConnectionPoolDefinitionIF definition
           
private  java.lang.String delegateUrl
           
private  long id
           
private  java.text.DecimalFormat idFormat
           
private static Log LOG
           
private  int mark
           
private  boolean needToReset
          Whether we have invoked a method that requires us to reset
private  java.util.Set openStatements
           
private  java.lang.String reasonForMark
           
private  java.lang.String requester
           
private  java.util.List sqlCalls
           
private  int status
           
(package private) static int STATUS_FORCE
           
private  WriterPreferenceReadWriteLock statusReadWriteLock
           
private  long timeLastStartActive
           
private  long timeLastStopActive
           
 
Fields inherited from interface org.logicalcobwebs.proxool.ConnectionInfoIF
MARK_FOR_EXPIRY, MARK_FOR_USE, STATUS_ACTIVE, STATUS_AVAILABLE, STATUS_NULL, STATUS_OFFLINE
 
Constructor Summary
protected ProxyConnection(java.sql.Connection connection, long id, java.lang.String delegateUrl, ConnectionPool connectionPool, ConnectionPoolDefinitionIF definition, 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 addSqlCall(java.lang.String sqlCall)
           
 void close()
          Doesn't really close the connection, just puts it back in the pool.
 int compareTo(java.lang.Object o)
          Compares using 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 ConnectionInfoIF.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 this connection belongs to
 ConnectionPoolDefinitionIF getDefinition()
          Get the definition 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
 java.lang.String getLastSqlCall()
          Get the most recent of all the ConnectionInfoIF.getSqlCalls()
 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.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.
 java.lang.String[] getSqlCalls()
          A log of the last SQL used on 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).
 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.
protected  void open()
          This gets called /just/ before a connection is served.
 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

STATUS_FORCE

static final int STATUS_FORCE
See Also:
Constant Field Values

statusReadWriteLock

private WriterPreferenceReadWriteLock statusReadWriteLock

LOG

private static final Log LOG

connection

private java.sql.Connection connection

delegateUrl

private java.lang.String delegateUrl

mark

private int mark

reasonForMark

private java.lang.String reasonForMark

status

private int status

id

private long id

birthDate

private java.util.Date birthDate

timeLastStartActive

private long timeLastStartActive

timeLastStopActive

private long timeLastStopActive

connectionPool

private ConnectionPool connectionPool

definition

private ConnectionPoolDefinitionIF definition

requester

private java.lang.String requester

openStatements

private java.util.Set openStatements

idFormat

private java.text.DecimalFormat idFormat

sqlCalls

private java.util.List sqlCalls

needToReset

private boolean needToReset
Whether we have invoked a method that requires us to reset

Constructor Detail

ProxyConnection

protected ProxyConnection(java.sql.Connection connection,
                          long id,
                          java.lang.String delegateUrl,
                          ConnectionPool connectionPool,
                          ConnectionPoolDefinitionIF definition,
                          int status)
                   throws java.sql.SQLException
Parameters:
connection - the real connection that is used
id - unique ID
delegateUrl -
connectionPool - the pool it is a member of
definition - the definition that was used to build it (could possibly be different from the one held in the connectionPool)
status - ConnectionInfoIF.STATUS_ACTIVE, ConnectionInfoIF.STATUS_AVAILABLE, STATUS_FORCE, ConnectionInfoIF.STATUS_NULL, or ConnectionInfoIF.STATUS_OFFLINE
Throws:
java.sql.SQLException
Method Detail

equals

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

Overrides:
equals in class java.lang.Object
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

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 this connection belongs to

Returns:
connectionPool

getDefinition

public ConnectionPoolDefinitionIF getDefinition()
Get the definition that was used to create this connection

Specified by:
getDefinition in interface ProxyConnectionIF
Returns:
definition

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:
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()

open

protected void open()
This gets called /just/ before a connection is served. You can use it to reset some of the attributes. The lifecycle is: open() then 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()
Overrides:
toString in class java.lang.Object
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 getId()

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

getSqlCalls

public java.lang.String[] getSqlCalls()
Description copied from interface: ConnectionInfoIF
A log of the last SQL used on this connection. Only populated if ConnectionPoolDefinitionIF.isTrace() is enabled.

Specified by:
getSqlCalls in interface ConnectionInfoIF
Returns:
the most recent SQL to be used

getLastSqlCall

public java.lang.String getLastSqlCall()
Description copied from interface: ProxyConnectionIF
Get the most recent of all the ConnectionInfoIF.getSqlCalls()

Specified by:
getLastSqlCall in interface ProxyConnectionIF
Returns:
the SQL (could be a batch of SQLs)

addSqlCall

public void addSqlCall(java.lang.String sqlCall)