Package org.apache.derby.jdbc
Class EmbedPooledConnection
- java.lang.Object
-
- org.apache.derby.jdbc.EmbedPooledConnection
-
- All Implemented Interfaces:
javax.sql.PooledConnection,BrokeredConnectionControl
- Direct Known Subclasses:
EmbedXAConnection
class EmbedPooledConnection extends java.lang.Object implements javax.sql.PooledConnection, BrokeredConnectionControl
A PooledConnection object is a connection object that provides hooks for connection pool management.This is Derby's implementation of a PooledConnection for use in the following environments:
- JDBC 4.2 - Java SE 8
- JDBC 4.1 - Java SE 7
- JDBC 4.0 - Java SE 6
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringconnStringthe connection string(package private) BrokeredConnectioncurrentConnectionHandle(package private) BasicEmbeddedDataSource40dataSource(package private) intdefaultIsolationLevelprivate booleandefaultReadOnlyprivate inteventIteratorsThe number of iterators going through the list of connection event listeners at the current time.private java.util.ArrayList<javax.sql.ConnectionEventListener>eventListenerThe list ofConnectionEventListeners.protected booleanisActiveprivate java.lang.Stringpassword(package private) EmbedConnectionrealConnectionprivate booleanrequestPasswordTrue if the password was passed in on the connection request, false if it came from the data source property.private java.util.concurrent.CopyOnWriteArrayList<javax.sql.StatementEventListener>statementEventListenersList of statement event listeners.private java.lang.Stringusername
-
Constructor Summary
Constructors Constructor Description EmbedPooledConnection(BasicEmbeddedDataSource40 ds, java.lang.String u, java.lang.String p, boolean requestPassword)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddConnectionEventListener(javax.sql.ConnectionEventListener listener)Add an event listener.voidaddStatementEventListener(javax.sql.StatementEventListener listener)Registers aStatementEventListenerwith thisPooledConnectionobject.(package private) voidcheckActive()voidcheckAutoCommit(boolean autoCommit)Allow control over setting auto commit mode.voidcheckClose()Check if the brokered connection can be closed.voidcheckCommit()Allow control over calling commit.intcheckHoldCursors(int holdability, boolean downgrade)Are held cursors allowed.voidcheckRollback()Allow control over calling rollback.voidcheckSavepoint()Allow control over creating a Savepoint (JDBC 3.0)voidclose()Close the Pooled connection.private voidcloseCurrentConnectionHandle()In this case the Listeners are *not* notified.booleanclosingConnection()Close called on BrokeredConnection.private voidfireConnectionEventListeners(java.sql.SQLException exception)Fire all theConnectionEventListeners registered.java.sql.ConnectiongetConnection()Create an object handle for a database connection.LanguageConnectionContextgetLanguageConnection()private static LanguageConnectionContextgetLanguageConnectionContext(EmbedConnection conn)Gets the LanguageConnectionContext for this connection.(package private) java.sql.ConnectiongetNewCurrentConnectionHandle()(package private) java.lang.StringgetPassword()EngineConnectiongetRealConnection()Return the real JDBC connection for the brokered connection.(package private) java.lang.StringgetUsername()booleanisActive()getter function for isActivebooleanisInGlobalTransaction()Is this a global transactionbooleanisIsolationLevelSetUsingSQLorJDBC()Returns true if isolation level has been set using either JDBC api or SQLvoidnotifyError(java.sql.SQLException exception)voidnotifyException(java.sql.SQLException sqle)Notify the control class that a SQLException was thrown during a call on one of the brokered connection's methods.voidonStatementClose(java.sql.PreparedStatement statement)Raise the statementClosed event for all the listeners when the corresponding events occursvoidonStatementErrorOccurred(java.sql.PreparedStatement statement, java.sql.SQLException sqle)Raise the statementErrorOccurred event for all the listeners when the corresponding events occurs(package private) voidopenRealConnection()voidremoveConnectionEventListener(javax.sql.ConnectionEventListener listener)Remove an event listener.voidremoveStatementEventListener(javax.sql.StatementEventListener listener)Removes the specifiedStatementEventListenerfrom the list of components that will be notified when the driver detects that aPreparedStatementhas been closed or is invalid.voidresetIsolationLevelFlag()Reset the isolation level flag used to keep state in BrokeredConnection.(package private) voidresetRealConnection()java.lang.StringtoString()Get the string representation of this pooled connection.java.sql.CallableStatementwrapStatement(java.sql.CallableStatement cs, java.lang.String sql)Call the setBrokeredConnectionControl method inside the EmbedCallableStatement class to set the BrokeredConnectionControl variable to this instance of EmbedPooledConnection This will then be used to call the onStatementErrorOccurred and onStatementClose events when the corresponding events occur on the CallableStatementjava.sql.PreparedStatementwrapStatement(java.sql.PreparedStatement ps, java.lang.String sql, java.lang.Object generatedKeys)Call the setBrokeredConnectionControl method inside the EmbedPreparedStatement class to set the BrokeredConnectionControl variable to this instance of EmbedPooledConnection This will then be used to call the onStatementErrorOccurred and onStatementClose events when the corresponding events occur on the PreparedStatementjava.sql.StatementwrapStatement(java.sql.Statement s)No need to wrap statements for PooledConnections.
-
-
-
Field Detail
-
connString
private java.lang.String connString
the connection string
-
eventListener
private java.util.ArrayList<javax.sql.ConnectionEventListener> eventListener
The list ofConnectionEventListeners. It is initiallynulland will be initialized lazily when the first listener is added.
-
statementEventListeners
private final java.util.concurrent.CopyOnWriteArrayList<javax.sql.StatementEventListener> statementEventListeners
List of statement event listeners. The list is copied on each write, ensuring that it can be safely iterated over even if other threads or the listeners fired in the same thread add or remove listeners.
-
eventIterators
private int eventIterators
The number of iterators going through the list of connection event listeners at the current time. Only one thread may be iterating over the list at any time (because of synchronization), but a single thread may have multiple iterators if for instance an event listener performs database calls that trigger a new event.
-
realConnection
EmbedConnection realConnection
-
defaultIsolationLevel
int defaultIsolationLevel
-
defaultReadOnly
private boolean defaultReadOnly
-
currentConnectionHandle
BrokeredConnection currentConnectionHandle
-
dataSource
final BasicEmbeddedDataSource40 dataSource
-
username
private final java.lang.String username
-
password
private final java.lang.String password
-
requestPassword
private final boolean requestPassword
True if the password was passed in on the connection request, false if it came from the data source property.
-
isActive
protected boolean isActive
-
-
Constructor Detail
-
EmbedPooledConnection
EmbedPooledConnection(BasicEmbeddedDataSource40 ds, java.lang.String u, java.lang.String p, boolean requestPassword) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
-
Method Detail
-
isActive
public boolean isActive()
getter function for isActive- Returns:
- boolean is isActive is true
-
getUsername
java.lang.String getUsername()
-
getPassword
java.lang.String getPassword()
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionCreate an object handle for a database connection.- Specified by:
getConnectionin interfacejavax.sql.PooledConnection- Returns:
- a Connection object
- Throws:
java.sql.SQLException- - if a database-access error occurs.
-
openRealConnection
final void openRealConnection() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getNewCurrentConnectionHandle
final java.sql.Connection getNewCurrentConnectionHandle() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
closeCurrentConnectionHandle
private void closeCurrentConnectionHandle() throws java.sql.SQLExceptionIn this case the Listeners are *not* notified. JDBC 3.0 spec section 11.4- Throws:
java.sql.SQLException
-
resetRealConnection
void resetRealConnection() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLExceptionClose the Pooled connection.- Specified by:
closein interfacejavax.sql.PooledConnection- Throws:
java.sql.SQLException- - if a database-access error occurs.
-
addConnectionEventListener
public final void addConnectionEventListener(javax.sql.ConnectionEventListener listener)
Add an event listener.- Specified by:
addConnectionEventListenerin interfacejavax.sql.PooledConnection
-
removeConnectionEventListener
public final void removeConnectionEventListener(javax.sql.ConnectionEventListener listener)
Remove an event listener.- Specified by:
removeConnectionEventListenerin interfacejavax.sql.PooledConnection
-
getRealConnection
public EngineConnection getRealConnection() throws java.sql.SQLException
Description copied from interface:BrokeredConnectionControlReturn the real JDBC connection for the brokered connection.- Specified by:
getRealConnectionin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
getLanguageConnection
public LanguageConnectionContext getLanguageConnection() throws java.sql.SQLException
- Returns:
- The underlying language connection.
- Throws:
java.sql.SQLException
-
notifyError
public void notifyError(java.sql.SQLException exception)
-
fireConnectionEventListeners
private void fireConnectionEventListeners(java.sql.SQLException exception)
Fire all theConnectionEventListeners registered. Callers must synchronize onthisto prevent others from modifying the list of listeners.- Parameters:
exception- the exception that caused the event, ornullif it is a close event
-
checkActive
final void checkActive() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
isIsolationLevelSetUsingSQLorJDBC
public boolean isIsolationLevelSetUsingSQLorJDBC() throws java.sql.SQLExceptionReturns true if isolation level has been set using either JDBC api or SQL- Specified by:
isIsolationLevelSetUsingSQLorJDBCin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
resetIsolationLevelFlag
public void resetIsolationLevelFlag() throws java.sql.SQLExceptionReset the isolation level flag used to keep state in BrokeredConnection. It will get set to true when isolation level is set using JDBC/SQL. It will get reset to false at the start and the end of a global transaction.- Specified by:
resetIsolationLevelFlagin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
isInGlobalTransaction
public boolean isInGlobalTransaction()
Description copied from interface:BrokeredConnectionControlIs this a global transaction- Specified by:
isInGlobalTransactionin interfaceBrokeredConnectionControl- Returns:
- true if this is a global XA transaction
- See Also:
BrokeredConnectionControl.isInGlobalTransaction()
-
notifyException
public void notifyException(java.sql.SQLException sqle)
Notify the control class that a SQLException was thrown during a call on one of the brokered connection's methods.- Specified by:
notifyExceptionin interfaceBrokeredConnectionControl
-
checkAutoCommit
public void checkAutoCommit(boolean autoCommit) throws java.sql.SQLExceptionAllow control over setting auto commit mode.- Specified by:
checkAutoCommitin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
checkHoldCursors
public int checkHoldCursors(int holdability, boolean downgrade) throws java.sql.SQLExceptionAre held cursors allowed.- Specified by:
checkHoldCursorsin interfaceBrokeredConnectionControldowngrade- true to downgrade the holdability, false to throw an exception.- Throws:
java.sql.SQLException
-
checkSavepoint
public void checkSavepoint() throws java.sql.SQLExceptionAllow control over creating a Savepoint (JDBC 3.0)- Specified by:
checkSavepointin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
checkRollback
public void checkRollback() throws java.sql.SQLExceptionAllow control over calling rollback.- Specified by:
checkRollbackin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
checkCommit
public void checkCommit() throws java.sql.SQLExceptionAllow control over calling commit.- Specified by:
checkCommitin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
checkClose
public void checkClose() throws java.sql.SQLExceptionDescription copied from interface:BrokeredConnectionControlCheck if the brokered connection can be closed.- Specified by:
checkClosein interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException- if it is not allowed to call close on the brokered connection- See Also:
BrokeredConnectionControl.checkClose()
-
closingConnection
public boolean closingConnection() throws java.sql.SQLExceptionClose called on BrokeredConnection. If this call returns true then getRealConnection().close() will be called. Notify listners that connection is closed. Don't close the underlying real connection as it is pooled.- Specified by:
closingConnectionin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
wrapStatement
public java.sql.Statement wrapStatement(java.sql.Statement s) throws java.sql.SQLExceptionNo need to wrap statements for PooledConnections.- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Throws:
java.sql.SQLException
-
wrapStatement
public java.sql.PreparedStatement wrapStatement(java.sql.PreparedStatement ps, java.lang.String sql, java.lang.Object generatedKeys) throws java.sql.SQLExceptionCall the setBrokeredConnectionControl method inside the EmbedPreparedStatement class to set the BrokeredConnectionControl variable to this instance of EmbedPooledConnection This will then be used to call the onStatementErrorOccurred and onStatementClose events when the corresponding events occur on the PreparedStatement- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Parameters:
ps- PreparedStatment to be wrappedsql- StringgeneratedKeys- Object- Returns:
- returns the wrapped PreparedStatement
- Throws:
java.sql.SQLException
-
wrapStatement
public java.sql.CallableStatement wrapStatement(java.sql.CallableStatement cs, java.lang.String sql) throws java.sql.SQLExceptionCall the setBrokeredConnectionControl method inside the EmbedCallableStatement class to set the BrokeredConnectionControl variable to this instance of EmbedPooledConnection This will then be used to call the onStatementErrorOccurred and onStatementClose events when the corresponding events occur on the CallableStatement- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Parameters:
cs- CallableStatment to be wrappedsql- String- Returns:
- returns the wrapped CallableStatement
- Throws:
java.sql.SQLException
-
toString
public java.lang.String toString()
Get the string representation of this pooled connection. A pooled connection is assigned a separate id from a physical connection. When a container calls PooledConnection.toString(), it gets the string representation of this id. This is useful for developers implementing connection pools when they are trying to debug pooled connections.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of the uniquie id for this pooled connection.
-
onStatementClose
public void onStatementClose(java.sql.PreparedStatement statement)
Raise the statementClosed event for all the listeners when the corresponding events occurs- Specified by:
onStatementClosein interfaceBrokeredConnectionControl- Parameters:
statement- thePreparedStatementthat was closed
-
onStatementErrorOccurred
public void onStatementErrorOccurred(java.sql.PreparedStatement statement, java.sql.SQLException sqle)Raise the statementErrorOccurred event for all the listeners when the corresponding events occurs- Specified by:
onStatementErrorOccurredin interfaceBrokeredConnectionControl- Parameters:
statement- thePreparedStatementin which the error occurredsqle- theSQLExceptionthat was thrown
-
removeStatementEventListener
public void removeStatementEventListener(javax.sql.StatementEventListener listener)
Removes the specifiedStatementEventListenerfrom the list of components that will be notified when the driver detects that aPreparedStatementhas been closed or is invalid.- Specified by:
removeStatementEventListenerin interfacejavax.sql.PooledConnection- Parameters:
listener- the component which implements theStatementEventListenerinterface that was previously registered with thisPooledConnectionobject
-
addStatementEventListener
public void addStatementEventListener(javax.sql.StatementEventListener listener)
Registers aStatementEventListenerwith thisPooledConnectionobject. Components that wish to be notified whenPreparedStatements created by the connection are closed or are detected to be invalid may use this method to register aStatementEventListenerwith thisPooledConnectionobject.- Specified by:
addStatementEventListenerin interfacejavax.sql.PooledConnection- Parameters:
listener- an component which implements theStatementEventListenerinterface that is to be registered with thisPooledConnectionobject
-
getLanguageConnectionContext
private static LanguageConnectionContext getLanguageConnectionContext(EmbedConnection conn)
Gets the LanguageConnectionContext for this connection.
-
-