Class EmbedPooledConnection
java.lang.Object
org.apache.derby.jdbc.EmbedPooledConnection
- All Implemented Interfaces:
PooledConnection, BrokeredConnectionControl
- Direct Known Subclasses:
EmbedXAConnection
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
FieldsModifier and TypeFieldDescriptionprivate Stringthe connection string(package private) BrokeredConnection(package private) final BasicEmbeddedDataSource40(package private) intprivate booleanprivate intThe number of iterators going through the list of connection event listeners at the current time.private ArrayList<ConnectionEventListener> The list ofConnectionEventListeners.protected booleanprivate final String(package private) EmbedConnectionprivate final booleanTrue if the password was passed in on the connection request, false if it came from the data source property.private final CopyOnWriteArrayList<StatementEventListener> List of statement event listeners.private final String -
Constructor Summary
ConstructorsConstructorDescriptionEmbedPooledConnection(BasicEmbeddedDataSource40 ds, String u, String p, boolean requestPassword) -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAdd an event listener.voidRegisters aStatementEventListenerwith thisPooledConnectionobject.(package private) final voidvoidcheckAutoCommit(boolean autoCommit) Allow control over setting auto commit mode.voidCheck if the brokered connection can be closed.voidAllow control over calling commit.intcheckHoldCursors(int holdability, boolean downgrade) Are held cursors allowed.voidAllow control over calling rollback.voidAllow control over creating a Savepoint (JDBC 3.0)voidclose()Close the Pooled connection.private voidIn this case the Listeners are *not* notified.booleanClose called on BrokeredConnection.private voidfireConnectionEventListeners(SQLException exception) Fire all theConnectionEventListeners registered.Create an object handle for a database connection.private static LanguageConnectionContextGets the LanguageConnectionContext for this connection.(package private) final Connection(package private) StringReturn the real JDBC connection for the brokered connection.(package private) StringbooleanisActive()getter function for isActivebooleanIs this a global transactionbooleanReturns true if isolation level has been set using either JDBC api or SQLvoidnotifyError(SQLException exception) voidnotifyException(SQLException sqle) Notify the control class that a SQLException was thrown during a call on one of the brokered connection's methods.voidonStatementClose(PreparedStatement statement) Raise the statementClosed event for all the listeners when the corresponding events occursvoidonStatementErrorOccurred(PreparedStatement statement, SQLException sqle) Raise the statementErrorOccurred event for all the listeners when the corresponding events occurs(package private) final voidfinal voidRemove an event listener.voidRemoves the specifiedStatementEventListenerfrom the list of components that will be notified when the driver detects that aPreparedStatementhas been closed or is invalid.voidReset the isolation level flag used to keep state in BrokeredConnection.(package private) voidtoString()Get the string representation of this pooled connection.wrapStatement(CallableStatement cs, 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 CallableStatementwrapStatement(PreparedStatement ps, String sql, 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 PreparedStatementNo need to wrap statements for PooledConnections.
-
Field Details
-
connString
the connection string -
eventListener
The list ofConnectionEventListeners. It is initiallynulland will be initialized lazily when the first listener is added. -
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 eventIteratorsThe 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
-
username
-
password
-
requestPassword
private final boolean requestPasswordTrue if the password was passed in on the connection request, false if it came from the data source property. -
isActive
protected boolean isActive
-
-
Constructor Details
-
EmbedPooledConnection
EmbedPooledConnection(BasicEmbeddedDataSource40 ds, String u, String p, boolean requestPassword) throws SQLException - Throws:
SQLException
-
-
Method Details
-
isActive
public boolean isActive()getter function for isActive- Returns:
- boolean is isActive is true
-
getUsername
String getUsername() -
getPassword
String getPassword() -
getConnection
Create an object handle for a database connection.- Specified by:
getConnectionin interfacePooledConnection- Returns:
- a Connection object
- Throws:
SQLException- - if a database-access error occurs.
-
openRealConnection
- Throws:
SQLException
-
getNewCurrentConnectionHandle
- Throws:
SQLException
-
closeCurrentConnectionHandle
In this case the Listeners are *not* notified. JDBC 3.0 spec section 11.4- Throws:
SQLException
-
resetRealConnection
- Throws:
SQLException
-
close
Close the Pooled connection.- Specified by:
closein interfacePooledConnection- Throws:
SQLException- - if a database-access error occurs.
-
addConnectionEventListener
Add an event listener.- Specified by:
addConnectionEventListenerin interfacePooledConnection
-
removeConnectionEventListener
Remove an event listener.- Specified by:
removeConnectionEventListenerin interfacePooledConnection
-
getRealConnection
Description copied from interface:BrokeredConnectionControlReturn the real JDBC connection for the brokered connection.- Specified by:
getRealConnectionin interfaceBrokeredConnectionControl- Throws:
SQLException
-
getLanguageConnection
- Returns:
- The underlying language connection.
- Throws:
SQLException
-
notifyError
-
fireConnectionEventListeners
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
- Throws:
SQLException
-
isIsolationLevelSetUsingSQLorJDBC
Returns true if isolation level has been set using either JDBC api or SQL- Specified by:
isIsolationLevelSetUsingSQLorJDBCin interfaceBrokeredConnectionControl- Throws:
SQLException
-
resetIsolationLevelFlag
Reset 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:
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:
-
notifyException
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
Allow control over setting auto commit mode.- Specified by:
checkAutoCommitin interfaceBrokeredConnectionControl- Throws:
SQLException
-
checkHoldCursors
Are held cursors allowed.- Specified by:
checkHoldCursorsin interfaceBrokeredConnectionControl- Parameters:
downgrade- true to downgrade the holdability, false to throw an exception.- Throws:
SQLException
-
checkSavepoint
Allow control over creating a Savepoint (JDBC 3.0)- Specified by:
checkSavepointin interfaceBrokeredConnectionControl- Throws:
SQLException
-
checkRollback
Allow control over calling rollback.- Specified by:
checkRollbackin interfaceBrokeredConnectionControl- Throws:
SQLException
-
checkCommit
Allow control over calling commit.- Specified by:
checkCommitin interfaceBrokeredConnectionControl- Throws:
SQLException
-
checkClose
Description copied from interface:BrokeredConnectionControlCheck if the brokered connection can be closed.- Specified by:
checkClosein interfaceBrokeredConnectionControl- Throws:
SQLException- if it is not allowed to call close on the brokered connection- See Also:
-
closingConnection
Close 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:
SQLException
-
wrapStatement
No need to wrap statements for PooledConnections.- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Throws:
SQLException
-
wrapStatement
public PreparedStatement wrapStatement(PreparedStatement ps, String sql, Object generatedKeys) throws SQLException 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 PreparedStatement- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Parameters:
ps- PreparedStatment to be wrappedsql- StringgeneratedKeys- Object- Returns:
- returns the wrapped PreparedStatement
- Throws:
SQLException
-
wrapStatement
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 CallableStatement- Specified by:
wrapStatementin interfaceBrokeredConnectionControl- Parameters:
cs- CallableStatment to be wrappedsql- String- Returns:
- returns the wrapped CallableStatement
- Throws:
SQLException
-
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. -
onStatementClose
Raise the statementClosed event for all the listeners when the corresponding events occurs- Specified by:
onStatementClosein interfaceBrokeredConnectionControl- Parameters:
statement- thePreparedStatementthat was closed
-
onStatementErrorOccurred
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
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 interfacePooledConnection- Parameters:
listener- the component which implements theStatementEventListenerinterface that was previously registered with thisPooledConnectionobject
-
addStatementEventListener
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 interfacePooledConnection- Parameters:
listener- an component which implements theStatementEventListenerinterface that is to be registered with thisPooledConnectionobject
-
getLanguageConnectionContext
Gets the LanguageConnectionContext for this connection.
-