Package org.logicalcobwebs.proxool
Class ProxyConnection
- java.lang.Object
-
- 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.40 $, $Date: 2007/05/15 23:17:21 $
- Author:
- bill, $Author: billhorsman $ (current maintainer)
-
-
Field Summary
-
Fields inherited from interface org.logicalcobwebs.proxool.ConnectionInfoIF
MARK_FOR_EXPIRY, MARK_FOR_USE, STATUS_ACTIVE, STATUS_AVAILABLE, STATUS_NULL, STATUS_OFFLINE
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedProxyConnection(java.sql.Connection connection, long id, java.lang.String delegateUrl, org.logicalcobwebs.proxool.ConnectionPool connectionPool, ConnectionPoolDefinitionIF definition, int status)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddOpenStatement(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.voidaddSqlCall(java.lang.String sqlCall)voidclose()Doesn't really close the connection, just puts it back in the pool.intcompareTo(java.lang.Object o)Compares usinggetId()booleanequals(java.lang.Object obj)Whether the underlying connections are equallonggetAge()The age in millseconds since this connection was builtjava.util.DategetBirthDate()LikeConnectionInfoIF.getBirthTime()but in Date formatlonggetBirthTime()The time that this connection was created.java.sql.ConnectiongetConnection()The real, delegate connection that we are usingprotected org.logicalcobwebs.proxool.ConnectionPoolgetConnectionPool()The ConnectionPool that this connection belongs toConnectionPoolDefinitionIFgetDefinition()Get the definition that was used to create this connectionjava.lang.StringgetDelegateHashcode()The hashcode (in hex) of the delegate connection object.java.lang.StringgetDelegateUrl()The URL that this connection is using (the definition might have changed since this connection was built).longgetId()A unique ID for this connectionjava.lang.StringgetLastSqlCall()Get the most recent of all theConnectionInfoIF.getSqlCalls()intgetMark()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.StringgetProxyHashcode()The hashcode (in hex) of the ProxyConnection object.intgetReasonCode()Get the reason why this connection ismarkedjava.lang.StringgetReasonForMark()Why this connection is marked (for instance, if a thread has marked it for expiry then it's nice to know why)java.lang.StringgetRequester()The name of the thread that asked for this connection.java.lang.String[]getSqlCalls()A log of the last SQL used on this connection.intgetStatus()The status of the connection.longgetTimeLastStartActive()When this connection was last given out.longgetTimeLastStopActive()When this connection was last given back (or zero if it is still active).booleanisActive()booleanisAvailable()booleanisClosed()Whether this connection is available.booleanisMarkedForExpiry()Whether this connection is due for expirybooleanisNull()booleanisOffline()booleanisReallyClosed()Find out if the delegated connection is close.voidmarkForExpiry(java.lang.String reason)Mark this connection for expiry (destruction) as soon as it stops being active.protected voidopen()This gets called /just/ before a connection is served.voidreallyClose()Close the connection for realvoidregisterClosedStatement(java.sql.Statement statement)Notify that a statement has been closed and won't need closing when the connection is returned to the poo.voidsetBirthTime(long birthTime)voidsetId(long id)protected voidsetNeedToReset(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).voidsetRequester(java.lang.String requester)booleansetStatus(int newStatus)Forces the new status regardless of the old statebooleansetStatus(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.voidsetTimeLastStartActive(long timeLastStartActive)voidsetTimeLastStopActive(long timeLastStopActive)java.lang.StringtoString()
-
-
-
Constructor Detail
-
ProxyConnection
protected ProxyConnection(java.sql.Connection connection, long id, java.lang.String delegateUrl, org.logicalcobwebs.proxool.ConnectionPool connectionPool, ConnectionPoolDefinitionIF definition, int status) throws java.sql.SQLException- Parameters:
connection- the real connection that is usedid- unique IDdelegateUrl-connectionPool- the pool it is a member ofdefinition- 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, orConnectionInfoIF.STATUS_OFFLINE- Throws:
java.sql.SQLException
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
Whether the underlying connections are equal- Overrides:
equalsin classjava.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 org.logicalcobwebs.proxool.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:
getDefinitionin interfaceProxyConnectionIF- 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:ProxyConnectionIFNotify that a statement has been closed and won't need closing when the connection is returned to the poo.- Specified by:
registerClosedStatementin interfaceProxyConnectionIF- Parameters:
statement- the statement that has just been closed- See Also:
ProxyConnectionIF.registerClosedStatement(java.sql.Statement)
-
reallyClose
public void reallyClose() throws java.sql.SQLExceptionClose the connection for real- Specified by:
reallyClosein interfaceProxyConnectionIF- Throws:
java.sql.SQLException- if anything goes wrong
-
isReallyClosed
public boolean isReallyClosed() throws java.sql.SQLExceptionDescription copied from interface:ProxyConnectionIFFind 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:
isReallyClosedin interfaceProxyConnectionIF- 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.SQLExceptionDescription copied from interface:ProxyConnectionIFDoesn't really close the connection, just puts it back in the pool. And tries to reset all the methods that need resetting.- Specified by:
closein interfaceProxyConnectionIF- Throws:
java.sql.SQLException- See Also:
ProxyConnectionIF.close()
-
open
protected void open()
-
getMark
public int getMark()
Description copied from interface:ConnectionInfoIFSometimes 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 isexpirethe 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:
getMarkin interfaceConnectionInfoIF
-
getStatus
public int getStatus()
Description copied from interface:ConnectionInfoIF- Specified by:
getStatusin interfaceConnectionInfoIF
-
setStatus
public boolean setStatus(int newStatus)
Description copied from interface:ProxyConnectionIFForces the new status regardless of the old state- Specified by:
setStatusin interfaceProxyConnectionIF- 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:ProxyConnectionIFChanges 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:
setStatusin interfaceProxyConnectionIF- 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:ConnectionInfoIFA unique ID for this connection- Specified by:
getIdin interfaceConnectionInfoIF
-
setId
public void setId(long id)
-
getBirthTime
public long getBirthTime()
Description copied from interface:ConnectionInfoIFThe time that this connection was created. The number of milliseconds since midnight, January 1, 1970 UTC.- Specified by:
getBirthTimein interfaceConnectionInfoIF- See Also:
ConnectionInfoIF.getBirthTime()
-
getBirthDate
public java.util.Date getBirthDate()
Description copied from interface:ConnectionInfoIFLikeConnectionInfoIF.getBirthTime()but in Date format- Specified by:
getBirthDatein interfaceConnectionInfoIF- Returns:
- birthDate
- See Also:
ConnectionInfoIF.getBirthDate()
-
getAge
public long getAge()
Description copied from interface:ConnectionInfoIFThe age in millseconds since this connection was built- Specified by:
getAgein interfaceConnectionInfoIF- See Also:
ConnectionInfoIF.getAge()
-
setBirthTime
public void setBirthTime(long birthTime)
- See Also:
ConnectionInfoIF.getBirthTime()
-
getTimeLastStartActive
public long getTimeLastStartActive()
Description copied from interface:ConnectionInfoIFWhen this connection was last given out. The number of milliseconds since midnight, January 1, 1970 UTC.- Specified by:
getTimeLastStartActivein interfaceConnectionInfoIF- See Also:
ConnectionInfoIF.getTimeLastStartActive()
-
setTimeLastStartActive
public void setTimeLastStartActive(long timeLastStartActive)
-
getTimeLastStopActive
public long getTimeLastStopActive()
Description copied from interface:ConnectionInfoIFWhen 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:
getTimeLastStopActivein interfaceConnectionInfoIF- See Also:
ConnectionInfoIF.getTimeLastStopActive()
-
setTimeLastStopActive
public void setTimeLastStopActive(long timeLastStopActive)
- See Also:
ConnectionInfoIF.getTimeLastStopActive()
-
getRequester
public java.lang.String getRequester()
Description copied from interface:ConnectionInfoIFThe name of the thread that asked for this connection.- Specified by:
getRequesterin interfaceConnectionInfoIF- See Also:
ConnectionInfoIF.getRequester()
-
setRequester
public void setRequester(java.lang.String requester)
- Specified by:
setRequesterin interfaceProxyConnectionIF- See Also:
ConnectionInfoIF.getRequester()
-
isNull
public boolean isNull()
- Specified by:
isNullin interfaceProxyConnectionIF- Returns:
- true if the status is null
- See Also:
ProxyConnectionIF.isNull()
-
isAvailable
public boolean isAvailable()
- Specified by:
isAvailablein interfaceProxyConnectionIF- Returns:
- true if the status is available
- See Also:
ProxyConnectionIF.isAvailable()
-
isActive
public boolean isActive()
- Specified by:
isActivein interfaceProxyConnectionIF- Returns:
- true if the status is active
- See Also:
ProxyConnectionIF.isActive()
-
isOffline
public boolean isOffline()
- Specified by:
isOfflinein interfaceProxyConnectionIF- Returns:
- true if the status is offline
- See Also:
ProxyConnectionIF.isOffline()
-
markForExpiry
public void markForExpiry(java.lang.String reason)
Description copied from interface:ProxyConnectionIFMark this connection for expiry (destruction) as soon as it stops being active.- Specified by:
markForExpiryin interfaceProxyConnectionIF- Parameters:
reason- why we are marking this connection- See Also:
ProxyConnectionIF.markForExpiry(java.lang.String)
-
isMarkedForExpiry
public boolean isMarkedForExpiry()
Description copied from interface:ProxyConnectionIFWhether this connection is due for expiry- Specified by:
isMarkedForExpiryin interfaceProxyConnectionIF- Returns:
- true if it is due for expiry
- See Also:
ProxyConnectionIF.isMarkedForExpiry()
-
getReasonForMark
public java.lang.String getReasonForMark()
Description copied from interface:ProxyConnectionIFWhy this connection is marked (for instance, if a thread has marked it for expiry then it's nice to know why)- Specified by:
getReasonForMarkin interfaceProxyConnectionIF- Returns:
- reasonForMark
- See Also:
ProxyConnectionIF.getReasonForMark()
-
getConnection
public java.sql.Connection getConnection()
Description copied from interface:ProxyConnectionIFThe real, delegate connection that we are using- Specified by:
getConnectionin interfaceProxyConnectionIF- Returns:
- connection
- See Also:
ProxyConnectionIF.getConnection()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
getDelegateUrl
public java.lang.String getDelegateUrl()
Description copied from interface:ConnectionInfoIFThe URL that this connection is using (the definition might have changed since this connection was built).- Specified by:
getDelegateUrlin interfaceConnectionInfoIF- Returns:
- delegateUrl
- See Also:
ConnectionInfoIF.getDelegateUrl()
-
getProxyHashcode
public java.lang.String getProxyHashcode()
Description copied from interface:ConnectionInfoIFThe hashcode (in hex) of the ProxyConnection object. This uniquely identifies this proxy connection.- Specified by:
getProxyHashcodein interfaceConnectionInfoIF- Returns:
- proxyHashcode
- See Also:
ConnectionInfoIF.getProxyHashcode()
-
getDelegateHashcode
public java.lang.String getDelegateHashcode()
Description copied from interface:ConnectionInfoIFThe hashcode (in hex) of the delegate connection object. This uniquely identifies the underlying connection.- Specified by:
getDelegateHashcodein interfaceConnectionInfoIF- Returns:
- delegateHashcode
- See Also:
ConnectionInfoIF.getDelegateHashcode()
-
compareTo
public int compareTo(java.lang.Object o)
Compares usinggetId()- Specified by:
compareToin interfacejava.lang.Comparable- Parameters:
o- must be anotherConnectionInfoIFimplementation- Returns:
- the comparison
- See Also:
Comparable.compareTo(Object)
-
getSqlCalls
public java.lang.String[] getSqlCalls()
Description copied from interface:ConnectionInfoIFA log of the last SQL used on this connection. Only populated ifConnectionPoolDefinitionIF.isTrace()is enabled.- Specified by:
getSqlCallsin interfaceConnectionInfoIF- Returns:
- the most recent SQL to be used
-
getLastSqlCall
public java.lang.String getLastSqlCall()
Description copied from interface:ProxyConnectionIFGet the most recent of all theConnectionInfoIF.getSqlCalls()- Specified by:
getLastSqlCallin interfaceProxyConnectionIF- Returns:
- the SQL (could be a batch of SQLs)
-
getReasonCode
public int getReasonCode()
Description copied from interface:ProxyConnectionIFGet the reason why this connection ismarked- Specified by:
getReasonCodein interfaceProxyConnectionIF- Returns:
ConnectionListenerIF.MAXIMUM_ACTIVE_TIME_EXPIRED,ConnectionListenerIF.HOUSE_KEEPER_TEST_FAIL,ConnectionListenerIF.FATAL_SQL_EXCEPTION_DETECTED,ConnectionListenerIF.MANUAL_EXPIRY,ConnectionListenerIF.MAXIMUM_CONNECTION_LIFETIME_EXCEEDED,ConnectionListenerIF.RESET_FAIL,ConnectionListenerIF.SHUTDOWN, orConnectionListenerIF.VALIDATION_FAIL
-
addSqlCall
public void addSqlCall(java.lang.String sqlCall)
-
-