org.objectweb.easybeans.component.jdbcpool
Class JManagedConnection

java.lang.Object
  extended by org.objectweb.easybeans.component.jdbcpool.JManagedConnection
All Implemented Interfaces:
java.lang.Comparable, javax.sql.PooledConnection, javax.sql.XAConnection, javax.transaction.Synchronization, javax.transaction.xa.XAResource

public class JManagedConnection
extends java.lang.Object
implements java.lang.Comparable, javax.sql.XAConnection, javax.transaction.xa.XAResource, javax.transaction.Synchronization

This class represents the connection managed by the pool. This connection is a managed connection and is notified of the transaction events.

Author:
Philippe Durieux, Florent Benoit

Field Summary
private  long closeTime
          Time for closing this connection.
private  long deathTime
          Time of the death for this connection.
private  ConnectionManager ds
          Link to the connection manager.
private  java.util.Vector<javax.sql.ConnectionEventListener> eventListeners
          Event listeners (of PooledConnection).
private  int identifier
          Identifier of this connection.
private  java.sql.Connection implConn
          Connection returned to the user.
private static JLog logger
          Logger.
private static int objcount
          Counter of all managed connections created.
private  int open
          count of opening this connection.
private  java.sql.Connection physicalConnection
          Connection to the database.
private  java.util.Map<java.lang.String,JStatement> psList
          List of PreparedStatement in the pool.
private  int psOpenNb
          Current number of opened prepared statements.
private  int pstmtmax
          Maximum of prepared statements.
private  int reUsedPreparedStatements
          Prepared statements that were reused.
private  int timeout
          Transaction timeout value.
private  javax.transaction.Transaction tx
          Transaction the connection is involved with.
 
Fields inherited from interface javax.transaction.xa.XAResource
TMENDRSCAN, TMFAIL, TMJOIN, TMNOFLAGS, TMONEPHASE, TMRESUME, TMSTARTRSCAN, TMSUCCESS, TMSUSPEND, XA_OK, XA_RDONLY
 
Constructor Summary
JManagedConnection(java.sql.Connection physicalConnection, ConnectionManager ds)
          Builds a new managed connection on a JDBC connection.
 
Method Summary
 void addConnectionEventListener(javax.sql.ConnectionEventListener listener)
          Add an event listener.
 void afterCompletion(int status)
          synchronization implementation.
 void beforeCompletion()
          synchronization implementation.
 void close()
          Close the database connection.
 void commit(javax.transaction.xa.Xid xid, boolean onePhase)
          Commit the global transaction specified by xid.
 int compareTo(java.lang.Object o)
          Compares this object with another specified object.
 void end(javax.transaction.xa.Xid xid, int flags)
          Ends the work performed on behalf of a transaction branch.
 void forget(javax.transaction.xa.Xid xid)
          Tell the resource manager to forget about a heuristically completed transaction branch.
 java.sql.Connection getConnection()
          Create an object handle for a database connection.
 int getIdentifier()
           
 int getOpenCount()
           
 int getReUsedPreparedStatements()
           
 int getTransactionTimeout()
          Obtain the current transaction timeout value set for this XAResource instance.
 javax.transaction.Transaction getTx()
           
 javax.transaction.xa.XAResource getXAResource()
          Return an XA resource to the caller.
 void hold()
          Notify as opened.
 boolean inactive()
          Check if the connection has been unused for too long time.
 boolean isAged()
           
 boolean isClosed()
           
 boolean isOpen()
           
 boolean isSameRM(javax.transaction.xa.XAResource xares)
          Determine if the resource manager instance represented by the target object is the same as the resource manager instance represented by the parameter xares.
 void notifyClose()
          Notify a Close event on Connection.
 void notifyError(java.sql.SQLException ex)
          Notify an Error event on Connection.
 void notifyPsClose(JStatement ps)
          A PreparedStatement has been logically closed.
 int prepare(javax.transaction.xa.Xid xid)
          Ask the resource manager to prepare for a transaction commit of the transaction specified in xid.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Try to find a PreparedStatement in the pool.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
          Try to find a PreparedStatement in the pool for the given options.
 javax.transaction.xa.Xid[] recover(int flag)
          Obtain a list of prepared transaction branches from a resource manager.
 boolean release()
          notify as closed.
 void remove()
          remove this item, ignoring exception on close.
 void removeConnectionEventListener(javax.sql.ConnectionEventListener listener)
          Remove an event listener.
 void rollback(javax.transaction.xa.Xid xid)
          Inform the resource manager to roll back work done on behalf of a transaction branch.
 void setPstmtMax(int max)
          Dynamically change the prepared statement pool size.
 boolean setTransactionTimeout(int seconds)
          Set the current transaction timeout value for this XAResource instance.
 void setTx(javax.transaction.Transaction tx)
          Set the associated transaction.
 void start(javax.transaction.xa.Xid xid, int flags)
          Start work on behalf of a transaction branch specified in xid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static JLog logger
Logger.


physicalConnection

private java.sql.Connection physicalConnection
Connection to the database.


implConn

private java.sql.Connection implConn
Connection returned to the user.


pstmtmax

private int pstmtmax
Maximum of prepared statements.


psOpenNb

private int psOpenNb
Current number of opened prepared statements.


eventListeners

private java.util.Vector<javax.sql.ConnectionEventListener> eventListeners
Event listeners (of PooledConnection).


open

private int open
count of opening this connection. >0 if open.


timeout

private int timeout
Transaction timeout value.


tx

private javax.transaction.Transaction tx
Transaction the connection is involved with.


objcount

private static int objcount
Counter of all managed connections created.


identifier

private final int identifier
Identifier of this connection.


reUsedPreparedStatements

private int reUsedPreparedStatements
Prepared statements that were reused.


psList

private java.util.Map<java.lang.String,JStatement> psList
List of PreparedStatement in the pool.


ds

private ConnectionManager ds
Link to the connection manager.


deathTime

private long deathTime
Time of the death for this connection.


closeTime

private long closeTime
Time for closing this connection.

Constructor Detail

JManagedConnection

public JManagedConnection(java.sql.Connection physicalConnection,
                          ConnectionManager ds)
Builds a new managed connection on a JDBC connection.

Parameters:
physicalConnection - the physical JDBC Connection.
ds - the connection manager
Method Detail

getIdentifier

public int getIdentifier()
Returns:
The identifier of this JManagedConnection

setPstmtMax

public void setPstmtMax(int max)
Dynamically change the prepared statement pool size.

Parameters:
max - the maximum of prepared statement.

commit

public void commit(javax.transaction.xa.Xid xid,
                   boolean onePhase)
            throws javax.transaction.xa.XAException
Commit the global transaction specified by xid.

Specified by:
commit in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
onePhase - true if one phase commit
Throws:
javax.transaction.xa.XAException - XA protocol error

end

public void end(javax.transaction.xa.Xid xid,
                int flags)
         throws javax.transaction.xa.XAException
Ends the work performed on behalf of a transaction branch.

Specified by:
end in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
flags - currently unused
Throws:
javax.transaction.xa.XAException - XA protocol error

forget

public void forget(javax.transaction.xa.Xid xid)
            throws javax.transaction.xa.XAException
Tell the resource manager to forget about a heuristically completed transaction branch.

Specified by:
forget in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
Throws:
javax.transaction.xa.XAException - XA protocol error

getTransactionTimeout

public int getTransactionTimeout()
                          throws javax.transaction.xa.XAException
Obtain the current transaction timeout value set for this XAResource instance.

Specified by:
getTransactionTimeout in interface javax.transaction.xa.XAResource
Returns:
the current transaction timeout in seconds
Throws:
javax.transaction.xa.XAException - XA protocol error

isSameRM

public boolean isSameRM(javax.transaction.xa.XAResource xares)
                 throws javax.transaction.xa.XAException
Determine if the resource manager instance represented by the target object is the same as the resource manager instance represented by the parameter xares.

Specified by:
isSameRM in interface javax.transaction.xa.XAResource
Parameters:
xares - An XAResource object
Returns:
True if same RM instance, otherwise false.
Throws:
javax.transaction.xa.XAException - XA protocol error

prepare

public int prepare(javax.transaction.xa.Xid xid)
            throws javax.transaction.xa.XAException
Ask the resource manager to prepare for a transaction commit of the transaction specified in xid.

Specified by:
prepare in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
Returns:
always OK
Throws:
javax.transaction.xa.XAException - XA protocol error

recover

public javax.transaction.xa.Xid[] recover(int flag)
                                   throws javax.transaction.xa.XAException
Obtain a list of prepared transaction branches from a resource manager.

Specified by:
recover in interface javax.transaction.xa.XAResource
Parameters:
flag - unused parameter.
Returns:
an array of transaction Xids
Throws:
javax.transaction.xa.XAException - XA protocol error

rollback

public void rollback(javax.transaction.xa.Xid xid)
              throws javax.transaction.xa.XAException
Inform the resource manager to roll back work done on behalf of a transaction branch.

Specified by:
rollback in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
Throws:
javax.transaction.xa.XAException - XA protocol error

setTransactionTimeout

public boolean setTransactionTimeout(int seconds)
                              throws javax.transaction.xa.XAException
Set the current transaction timeout value for this XAResource instance.

Specified by:
setTransactionTimeout in interface javax.transaction.xa.XAResource
Parameters:
seconds - timeout value, in seconds.
Returns:
always true
Throws:
javax.transaction.xa.XAException - XA protocol error

start

public void start(javax.transaction.xa.Xid xid,
                  int flags)
           throws javax.transaction.xa.XAException
Start work on behalf of a transaction branch specified in xid.

Specified by:
start in interface javax.transaction.xa.XAResource
Parameters:
xid - transaction xid
flags - unused parameter
Throws:
javax.transaction.xa.XAException - XA protocol error

getXAResource

public javax.transaction.xa.XAResource getXAResource()
                                              throws java.sql.SQLException
Return an XA resource to the caller.

Specified by:
getXAResource in interface javax.sql.XAConnection
Returns:
The XAResource
Throws:
java.sql.SQLException - - if a database-access error occurs

compareTo

public int compareTo(java.lang.Object o)
Compares this object with another specified object.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the object to compare
Returns:
a value detecting if these objects are matching or not.

getReUsedPreparedStatements

public int getReUsedPreparedStatements()
Returns:
value of reused prepared statement.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Create an object handle for a database connection.

Specified by:
getConnection in interface javax.sql.PooledConnection
Returns:
connection used by this managed connection
Throws:
java.sql.SQLException - - if a database-access error occurs

close

public void close()
           throws java.sql.SQLException
Close the database connection.

Specified by:
close in interface javax.sql.PooledConnection
Throws:
java.sql.SQLException - - if a database-access error occurs

addConnectionEventListener

public void addConnectionEventListener(javax.sql.ConnectionEventListener listener)
Add an event listener.

Specified by:
addConnectionEventListener in interface javax.sql.PooledConnection
Parameters:
listener - event listener

removeConnectionEventListener

public void removeConnectionEventListener(javax.sql.ConnectionEventListener listener)
Remove an event listener.

Specified by:
removeConnectionEventListener in interface javax.sql.PooledConnection
Parameters:
listener - event listener

beforeCompletion

public void beforeCompletion()
synchronization implementation.

Specified by:
beforeCompletion in interface javax.transaction.Synchronization

afterCompletion

public void afterCompletion(int status)
synchronization implementation.

Specified by:
afterCompletion in interface javax.transaction.Synchronization

isAged

public boolean isAged()
Returns:
true if connection max age has expired

isOpen

public boolean isOpen()
Returns:
true if connection is still open

getOpenCount

public int getOpenCount()
Returns:
open count

inactive

public boolean inactive()
Check if the connection has been unused for too long time. This occurs usually when the caller forgot to call close().

Returns:
true if open time has been reached, and not involved in a tx.

isClosed

public boolean isClosed()
Returns:
true if connection is closed

hold

public void hold()
Notify as opened.


release

public boolean release()
notify as closed.

Returns:
true if normal close.

setTx

public void setTx(javax.transaction.Transaction tx)
Set the associated transaction.

Parameters:
tx - Transaction

getTx

public javax.transaction.Transaction getTx()
Returns:
the Transaction

remove

public void remove()
remove this item, ignoring exception on close.


prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int resultSetType,
                                                   int resultSetConcurrency)
                                            throws java.sql.SQLException
Try to find a PreparedStatement in the pool for the given options.

Parameters:
sql - the sql of the prepared statement
resultSetType - the type of resultset
resultSetConcurrency - the concurrency of this resultset
Returns:
a preparestatement object
Throws:
java.sql.SQLException - if an errors occurs on the database.

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Try to find a PreparedStatement in the pool.

Parameters:
sql - the given sql query.
Returns:
a given prepared statement.
Throws:
java.sql.SQLException - if an error in the database occurs.

notifyPsClose

public void notifyPsClose(JStatement ps)
A PreparedStatement has been logically closed.

Parameters:
ps - a prepared statement.

notifyClose

public void notifyClose()
Notify a Close event on Connection.


notifyError

public void notifyError(java.sql.SQLException ex)
Notify an Error event on Connection.

Parameters:
ex - the given exception