public class ManagedConnection extends Object
| Modifier and Type | Field and Description |
|---|---|
static boolean |
checkStatementResultSetParameters
Optionally enable statement checks for resultSetType and resultSetConcurrency.
|
static boolean |
collectStatistics
Flag to turn on statement statistics.
|
static long |
logMinAttachMillis
Minimum milliseconds a connection remains attached before being logged.
0 to disable, -1 to log every detach. |
static long |
logStatisticsMillis
Optional periodical log of statistics in milliseconds.
|
static Pattern |
mdcFilter
Optional filter to log only statements for certain MappedDiagnosticContexts.
If set, statements are only collected for logging if the MDC is valid and the pattern matches the toString-value of the MDC. |
| Constructor and Description |
|---|
ManagedConnection(ConnectionManager manager,
Backend backend,
Connection connection)
Creates a managed connection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addResultSet(ResultSetWrapper rs)
Adds a pending result set.
|
void |
addRunningStatement(StatementWrapper statement)
Remembers a statement as running.
|
void |
attachSession(Db db)
Attaches a connection to a session.
|
void |
cancelRunningStatements()
Cancels the(all) currently running statement(s).
May be invoked from any thread. |
void |
close()
Closes the connection.
Closing an already closed connection is allowed. |
void |
closePreparedStatements(boolean onlyMarkedReady)
Closes 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.
|
PreparedStatementWrapper |
createPreparedStatement(StatementKey statementKey,
String sql,
int resultSetType,
int resultSetConcurrency)
Creates a prepared statement.
|
StatementWrapper |
createStatement(int resultSetType,
int resultSetConcurrency)
Creates a non-prepared statement.
One-shot statements (i.e. |
void |
detachSession(Db db)
Detaches a session from a connection.
|
protected void |
finalize() |
long |
getAttachedSince()
Gets the epochal time when this connection was attached.
|
boolean |
getAutoCommit()
Gets the autocommit value.
|
String |
getBackendId()
Gets the backend id.
|
Connection |
getConnection()
Gets the low level connection.
|
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.
|
long |
getInstanceNumber()
Gets the unique instance number.
|
static Collection<ManagedConnection> |
getManagedConnections()
Gets a list of all managed open connections.
|
ConnectionManager |
getManager()
Gets the connection manager
|
int |
getMaxCountForClearWarnings()
Gets the current setting for clearWarnings() trigger.
|
String |
getName()
Gets the connection name.
|
Collection<ResultSetWrapper> |
getOpenResultSets()
Gets all open result sets for this connection.
|
PreparedStatementWrapper |
getPreparedStatement(StatementKey statementKey,
boolean alwaysPrepare,
int resultSetType,
int resultSetConcurrency,
Supplier<String> sqlSupplier)
Gets a prepared statement.
The statement will be reused if already prepared. |
Db |
getSession()
Gets tha attached session.
|
List<StatementHistory> |
getStatementHistory()
Gets the statement history.
|
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
|
protected boolean |
isMDCValid()
Checks whether the mapped diagnostic context is valid for statistics logging.
|
boolean |
isTxRunning()
Returns whether a transaction is running.
|
void |
logAndClearWarnings()
Reads all warnings, logs and clears them.
|
void |
removePreparedStatement(PreparedStatementWrapper st)
Removes a closed prepared statement.
|
void |
removeResultSet(ResultSetWrapper rs)
Removes a pending result set.
|
void |
removeRunningStatement(StatementWrapper statement)
Removes a statement from the running statements.
|
void |
rollback(boolean withLog)
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 |
toDiagnosticString()
Creates a diagnostic string.
|
String |
toString() |
void |
unmarkPreparedStatements()
Unmark all prepared statements that are marked ready.
|
boolean |
verifyConnection()
Checks whether connection is still valid.
|
public static long logMinAttachMillis
For analysis and debugging purposes only.
public static boolean collectStatistics
public static long logStatisticsMillis
public static Pattern mdcFilter
public static boolean checkStatementResultSetParameters
public ManagedConnection(ConnectionManager manager, Backend backend, Connection connection)
manager - the connection managerbackend - the backendconnection - the low level JDBC connectionpublic static Collection<ManagedConnection> getManagedConnections()
public ConnectionManager getManager()
public Connection getConnection()
public long getEstablishedSince()
public void setExpireAt(long expireAt)
expireAt - the time in [ms], 0 = foreverpublic long getExpireAt()
public long getAttachedSince()
public long getDetachedSince()
Note that newly created conntections get their detach time initialized from the current system time.
public void setIndex(int index)
index - the connection indexpublic int getIndex()
public void setDead(boolean 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).
dead - true if marked dead, false not deadpublic boolean isDead()
public boolean verifyConnection()
Implemented via a "SELECT 1" query. If the check fails the connection is marked dead.
public void attachSession(Db db)
db - the logical Db to attachpublic boolean isAttached()
public boolean isTxRunning()
public void detachSession(Db db)
db - the db to detachpublic Db getSession()
public long getInstanceNumber()
public String getBackendId()
public String getName()
protected boolean isMDCValid()
mdcFilterpublic List<StatementHistory> getStatementHistory()
public void addResultSet(ResultSetWrapper rs)
rs - the result setpublic void removeResultSet(ResultSetWrapper rs)
rs - the result setpublic Collection<ResultSetWrapper> getOpenResultSets()
public String toDiagnosticString()
public void setAutoCommit(boolean autoCommit)
autoCommit - true to enable autocomit, false to disable.public boolean getAutoCommit()
public void commit()
public void rollback(boolean withLog)
withLog - true if log via INFOpublic void addRunningStatement(StatementWrapper statement)
statement - the statementpublic void removeRunningStatement(StatementWrapper statement)
statement - the statementpublic void cancelRunningStatements()
public void unmarkPreparedStatements()
public void logAndClearWarnings()
public void setMaxCountForClearWarnings(int max)
max - the maxcount, 0 = app must eat the warningspublic int getMaxCountForClearWarnings()
public void countForClearWarnings()
public void close()
public boolean isClosed()
protected void finalize()
throws Throwable
public void closePreparedStatements(boolean onlyMarkedReady)
onlyMarkedReady - true if close only pending statements, false if allpublic void removePreparedStatement(PreparedStatementWrapper st)
st - the statementpublic StatementWrapper createStatement(int resultSetType, int resultSetConcurrency)
resultSetType - a result set type; one of
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency - a concurrency type; one of
ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLEStatement object that will generate
ResultSet objects with the given type and
concurrencypublic PreparedStatementWrapper createPreparedStatement(StatementKey statementKey, String sql, int resultSetType, int resultSetConcurrency)
statementKey - the statement key, null if one-shotsql - the sql coderesultSetType - the result set typeresultSetConcurrency - the result set concurrencypublic PreparedStatementWrapper getPreparedStatement(StatementKey statementKey, boolean alwaysPrepare, int resultSetType, int resultSetConcurrency, Supplier<String> sqlSupplier)
Notice: this method will be invoked from within a synchronized block from a Db-instance, so there's no need for special multi-threading handling.
statementKey - the unique statement keyalwaysPrepare - true if always do a physical prepareresultSetType - one of ResultSet.TYPE_...resultSetConcurrency - one of ResultSet.CONCUR_...sqlSupplier - the SQL supplierCopyright © 2016 Krake Softwaretechnik. All rights reserved.