org.tentackle.db
Class ManagedConnection

java.lang.Object
  extended by org.tentackle.db.ManagedConnection

public class ManagedConnection
extends Object

A jdbc connection managed by the ConnectionManager.
The connection provides some additional features such as a prepared statement cache and translates SQLExceptions to DbRuntimeExceptions. The ConnectionManager is responsible to attach and detach the connection to a Db.

Author:
harald

Constructor Summary
ManagedConnection(ConnectionManager manager, Connection connection)
          Creates a managed connection.
 
Method Summary
 void attachDb(Db db)
          Attaches a connection to a Db.
 void close()
          Closes the connection.
 void closeAllPreparedStatements(boolean onlyMarked)
          Closes all prepared statements.
 void commit()
          Performs a commit.
 void countForClearWarnings()
          Increments a counter and empties the warnings on the connection and all prepared statements if a trigger value is reached.
 Statement createStatement(Db db)
          Creates a one-shot statement.
 Statement createStatement(Db db, int resultSetType)
          Creates a one-shot statement.
 Statement createStatement(Db db, int resultSetType, int resultSetConcurrency)
          Creates a one-shot statement.
 void detachDb(Db db)
          Detaches a connection.
 boolean getAutoCommit()
          Gets the autocommit value.
 Connection getConnection()
          Gets the low level connection.
 Db getDb()
          Gets tha attached db.
 long getDetachedSince()
          Gets the epochal time when this connection was detached.
 long getEstablishedSince()
          Gets the epochal time when this connection was established.
 long getExpireAt()
          Gets the epochal time when this connection should be closed, if unused.
 int getIndex()
          Gets the connection index.
 ConnectionManager getManager()
          Gets the connection manager
 int getMaxCountForClearWarnings()
          Gets the current setting for clearWarnings() trigger.
 PreparedStatementWrapper getPreparedStatement(int stmtId)
          Gets a prepared statement.
 boolean isAttached()
          Checks whether a Db is attached to this connection.
 boolean isClosed()
          Gets the connection's closed state.
 boolean isDead()
          Returns whether connection is marked dead
 void logAndClearWarnings()
          Reads all warnings, logs them and clear.
 void rollback()
          Performs a rollback.
 void setAutoCommit(boolean autoCommit)
          Sets the autocommit feature.
 void setDead(boolean dead)
          Marks a connection being dead.
 void setExpireAt(long expireAt)
          Sets the epochal time when this connection should be closed, if unused.
 void setIndex(int index)
          Sets the connection index.
 void setMaxCountForClearWarnings(int max)
          Sets the countForClearWarnings trigger.
 String toString()
          Gets the string representation of this connection.
 boolean verifyConnection()
          Checks whether connection is still valid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ManagedConnection

public ManagedConnection(ConnectionManager manager,
                         Connection connection)
Creates a managed connection.

Parameters:
manager - the connection manager
connection - the low level JDBC connection
Method Detail

getManager

public ConnectionManager getManager()
Gets the connection manager

Returns:
the manager

getConnection

public Connection getConnection()
Gets the low level connection.

Returns:
the physical connection

getEstablishedSince

public long getEstablishedSince()
Gets the epochal time when this connection was established.

Returns:
the time establishedSince in ms

setExpireAt

public void setExpireAt(long expireAt)
Sets the epochal time when this connection should be closed, if unused.

Parameters:
expireAt - the time in [ms], 0 = forever

getExpireAt

public long getExpireAt()
Gets the epochal time when this connection should be closed, if unused.

Returns:
the time in [ms], 0 = forever

getDetachedSince

public long getDetachedSince()
Gets the epochal time when this connection was detached.

Note that newly created conntections get their detach time initialized from the current system time.

Returns:
the epochal time of last detach, 0 if attached

setIndex

public void setIndex(int index)
Sets the connection index. Connection managers use that to manage connection lists.

Parameters:
index - the connection index

getIndex

public int getIndex()
Gets the connection index.

Returns:
the connection index.

setDead

public void setDead(boolean dead)
Marks a connection being dead.

Marking connections dead allows connection managers like MpxConnectionManager to re-open connections before being attached next time. Notice that not all connection managers honour the dead-flag (makes only sense in servers, anyway).

Whenever the DefaultErrorHandler detects a communication link error, it marks a connection dead.

Parameters:
dead - true if marked dead, false not dead

isDead

public boolean isDead()
Returns whether connection is marked dead

Returns:
true if dead

verifyConnection

public boolean verifyConnection()
Checks whether connection is still valid.

Implemented via a "SELECT 1" query. If the check fails the connection is marked dead.

Returns:
true if connection still valid, false if invalid

attachDb

public void attachDb(Db db)
Attaches a connection to a Db. Connections must be attached before it can be used by statements or starting a tx. The method must only be invoked by a connection manager!

Parameters:
db - the logical Db to attach

isAttached

public boolean isAttached()
Checks whether a Db is attached to this connection.

Returns:
true if attached, else false.

detachDb

public void detachDb(Db db)
Detaches a connection. Connections must be detached before they can be used by another Db. The method must only be invoked by a connection manager!

Parameters:
db - the db to detach

getDb

public Db getDb()
Gets tha attached db.

Returns:
the db, null if not attached

toString

public String toString()
Gets the string representation of this connection.

Overrides:
toString in class Object
Returns:
a string representation of the object.

setAutoCommit

public void setAutoCommit(boolean autoCommit)
Sets the autocommit feature.

Parameters:
autoCommit - true to enable autocomit, false to disable.

getAutoCommit

public boolean getAutoCommit()
Gets the autocommit value.

Returns:
the autocommit value.

commit

public void commit()
Performs a commit.


rollback

public void rollback()
Performs a rollback.


logAndClearWarnings

public void logAndClearWarnings()
Reads all warnings, logs them and clear.


setMaxCountForClearWarnings

public void setMaxCountForClearWarnings(int max)
Sets the countForClearWarnings trigger.

Parameters:
max - the maxcount, 0 = app must eat the warnings

getMaxCountForClearWarnings

public int getMaxCountForClearWarnings()
Gets the current setting for clearWarnings() trigger.

Returns:
the maxcount, 0 = app must eat the warnings

countForClearWarnings

public void countForClearWarnings()
Increments a counter and empties the warnings on the connection and all prepared statements if a trigger value is reached. This is necessary for apps that don't clearWarnings() on their own, otherwise filling up memory might occur.


close

public void close()
Closes the connection.


isClosed

public boolean isClosed()
Gets the connection's closed state.

Returns:
true if connection is closed

closeAllPreparedStatements

public void closeAllPreparedStatements(boolean onlyMarked)
Closes all prepared statements.

Parameters:
onlyMarked - true if close only pending statements

createStatement

public Statement createStatement(Db db,
                                 int resultSetType,
                                 int resultSetConcurrency)
Creates a one-shot statement. One-shot statements (i.e. non-prepared statements) must attach the db as soon as they are instantiated. The db is detached after executeUpdate or after executeQuery when its result set is closed.

Parameters:
db - the Db to use for this one-shot
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
Returns:
a new Statement object that will generate ResultSet objects with the given type and concurrency

createStatement

public Statement createStatement(Db db,
                                 int resultSetType)
Creates a one-shot statement.

Parameters:
db - the Db to use for this one-shot
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
Returns:
a new Statement object that will generate ResultSet objects with the given type and concurrency CONCUR_READ_ONLY

createStatement

public Statement createStatement(Db db)
Creates a one-shot statement.

Parameters:
db - the Db to use for this one-shot
Returns:
a new Statement object that will generate ResultSet objects with type TYPE_FORWARD_ONLY and concurrency CONCUR_READ_ONLY

getPreparedStatement

public PreparedStatementWrapper getPreparedStatement(int stmtId)
Gets a prepared statement. The statement will be reused if already prepared. Else it will be prepared according to the statement definition in PooledPreparedStatement.

Parameters:
stmtId - is the global statement id (> 0)
Returns:
the prepared statement for this connection


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de