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 protected
ProxyConnection(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 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 usinggetId()
boolean
equals(java.lang.Object obj)
Whether the underlying connections are equallong
getAge()
The age in millseconds since this connection was builtjava.util.Date
getBirthDate()
LikeConnectionInfoIF.getBirthTime()
but in Date formatlong
getBirthTime()
The time that this connection was created.java.sql.Connection
getConnection()
The real, delegate connection that we are usingprotected org.logicalcobwebs.proxool.ConnectionPool
getConnectionPool()
The ConnectionPool that this connection belongs toConnectionPoolDefinitionIF
getDefinition()
Get the definition that was used to create this connectionjava.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 connectionjava.lang.String
getLastSqlCall()
Get the most recent of all theConnectionInfoIF.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.int
getReasonCode()
Get the reason why this connection ismarked
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 expiryboolean
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 realvoid
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 stateboolean
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()
-
-
-
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:
equals
in 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:
getDefinition
in 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: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 interfaceProxyConnectionIF
- 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 interfaceProxyConnectionIF
- 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 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.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 interfaceProxyConnectionIF
- Throws:
java.sql.SQLException
- See Also:
ProxyConnectionIF.close()
-
open
protected void open()
-
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 isexpire
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 interfaceConnectionInfoIF
-
getStatus
public int getStatus()
Description copied from interface:ConnectionInfoIF
- Specified by:
getStatus
in interfaceConnectionInfoIF
-
setStatus
public boolean setStatus(int newStatus)
Description copied from interface:ProxyConnectionIF
Forces the new status regardless of the old state- Specified by:
setStatus
in 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: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 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:ConnectionInfoIF
A unique ID for this connection- Specified by:
getId
in interfaceConnectionInfoIF
-
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 interfaceConnectionInfoIF
- See Also:
ConnectionInfoIF.getBirthTime()
-
getBirthDate
public java.util.Date getBirthDate()
Description copied from interface:ConnectionInfoIF
LikeConnectionInfoIF.getBirthTime()
but in Date format- Specified by:
getBirthDate
in interfaceConnectionInfoIF
- 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 interfaceConnectionInfoIF
- 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 interfaceConnectionInfoIF
- See Also:
ConnectionInfoIF.getTimeLastStartActive()
-
setTimeLastStartActive
public void setTimeLastStartActive(long timeLastStartActive)
-
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 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:ConnectionInfoIF
The name of the thread that asked for this connection.- Specified by:
getRequester
in interfaceConnectionInfoIF
- See Also:
ConnectionInfoIF.getRequester()
-
setRequester
public void setRequester(java.lang.String requester)
- Specified by:
setRequester
in interfaceProxyConnectionIF
- See Also:
ConnectionInfoIF.getRequester()
-
isNull
public boolean isNull()
- Specified by:
isNull
in interfaceProxyConnectionIF
- Returns:
- true if the status is null
- See Also:
ProxyConnectionIF.isNull()
-
isAvailable
public boolean isAvailable()
- Specified by:
isAvailable
in interfaceProxyConnectionIF
- Returns:
- true if the status is available
- See Also:
ProxyConnectionIF.isAvailable()
-
isActive
public boolean isActive()
- Specified by:
isActive
in interfaceProxyConnectionIF
- Returns:
- true if the status is active
- See Also:
ProxyConnectionIF.isActive()
-
isOffline
public boolean isOffline()
- Specified by:
isOffline
in interfaceProxyConnectionIF
- 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 interfaceProxyConnectionIF
- 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 interfaceProxyConnectionIF
- 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 interfaceProxyConnectionIF
- 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 interfaceProxyConnectionIF
- Returns:
- connection
- See Also:
ProxyConnectionIF.getConnection()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.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 interfaceConnectionInfoIF
- 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 interfaceConnectionInfoIF
- 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 interfaceConnectionInfoIF
- Returns:
- delegateHashcode
- See Also:
ConnectionInfoIF.getDelegateHashcode()
-
compareTo
public int compareTo(java.lang.Object o)
Compares usinggetId()
- Specified by:
compareTo
in interfacejava.lang.Comparable
- Parameters:
o
- must be anotherConnectionInfoIF
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 ifConnectionPoolDefinitionIF.isTrace()
is enabled.- Specified by:
getSqlCalls
in interfaceConnectionInfoIF
- 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 theConnectionInfoIF.getSqlCalls()
- Specified by:
getLastSqlCall
in interfaceProxyConnectionIF
- Returns:
- the SQL (could be a batch of SQLs)
-
getReasonCode
public int getReasonCode()
Description copied from interface:ProxyConnectionIF
Get the reason why this connection ismarked
- Specified by:
getReasonCode
in 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)
-
-