Package org.apache.derby.jdbc
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 String
the connection string(package private) BrokeredConnection
(package private) final BasicEmbeddedDataSource40
(package private) int
private boolean
private int
The number of iterators going through the list of connection event listeners at the current time.private ArrayList
<ConnectionEventListener> The list ofConnectionEventListener
s.protected boolean
private final String
(package private) EmbedConnection
private final boolean
True 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 void
Add an event listener.void
Registers aStatementEventListener
with thisPooledConnection
object.(package private) final void
void
checkAutoCommit
(boolean autoCommit) Allow control over setting auto commit mode.void
Check if the brokered connection can be closed.void
Allow control over calling commit.int
checkHoldCursors
(int holdability, boolean downgrade) Are held cursors allowed.void
Allow control over calling rollback.void
Allow control over creating a Savepoint (JDBC 3.0)void
close()
Close the Pooled connection.private void
In this case the Listeners are *not* notified.boolean
Close called on BrokeredConnection.private void
fireConnectionEventListeners
(SQLException exception) Fire all theConnectionEventListener
s registered.Create an object handle for a database connection.private static LanguageConnectionContext
Gets the LanguageConnectionContext for this connection.(package private) final Connection
(package private) String
Return the real JDBC connection for the brokered connection.(package private) String
boolean
isActive()
getter function for isActiveboolean
Is this a global transactionboolean
Returns true if isolation level has been set using either JDBC api or SQLvoid
notifyError
(SQLException exception) void
notifyException
(SQLException sqle) Notify the control class that a SQLException was thrown during a call on one of the brokered connection's methods.void
onStatementClose
(PreparedStatement statement) Raise the statementClosed event for all the listeners when the corresponding events occursvoid
onStatementErrorOccurred
(PreparedStatement statement, SQLException sqle) Raise the statementErrorOccurred event for all the listeners when the corresponding events occurs(package private) final void
final void
Remove an event listener.void
Removes the specifiedStatementEventListener
from the list of components that will be notified when the driver detects that aPreparedStatement
has been closed or is invalid.void
Reset the isolation level flag used to keep state in BrokeredConnection.(package private) void
toString()
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 ofConnectionEventListener
s. It is initiallynull
and 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:
getConnection
in 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:
close
in interfacePooledConnection
- Throws:
SQLException
- - if a database-access error occurs.
-
addConnectionEventListener
Add an event listener.- Specified by:
addConnectionEventListener
in interfacePooledConnection
-
removeConnectionEventListener
Remove an event listener.- Specified by:
removeConnectionEventListener
in interfacePooledConnection
-
getRealConnection
Description copied from interface:BrokeredConnectionControl
Return the real JDBC connection for the brokered connection.- Specified by:
getRealConnection
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
getLanguageConnection
- Returns:
- The underlying language connection.
- Throws:
SQLException
-
notifyError
-
fireConnectionEventListeners
Fire all theConnectionEventListener
s registered. Callers must synchronize onthis
to prevent others from modifying the list of listeners.- Parameters:
exception
- the exception that caused the event, ornull
if 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:
isIsolationLevelSetUsingSQLorJDBC
in 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:
resetIsolationLevelFlag
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
isInGlobalTransaction
public boolean isInGlobalTransaction()Description copied from interface:BrokeredConnectionControl
Is this a global transaction- Specified by:
isInGlobalTransaction
in 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:
notifyException
in interfaceBrokeredConnectionControl
-
checkAutoCommit
Allow control over setting auto commit mode.- Specified by:
checkAutoCommit
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
checkHoldCursors
Are held cursors allowed.- Specified by:
checkHoldCursors
in 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:
checkSavepoint
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
checkRollback
Allow control over calling rollback.- Specified by:
checkRollback
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
checkCommit
Allow control over calling commit.- Specified by:
checkCommit
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
checkClose
Description copied from interface:BrokeredConnectionControl
Check if the brokered connection can be closed.- Specified by:
checkClose
in 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:
closingConnection
in interfaceBrokeredConnectionControl
- Throws:
SQLException
-
wrapStatement
No need to wrap statements for PooledConnections.- Specified by:
wrapStatement
in 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:
wrapStatement
in 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:
wrapStatement
in 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:
onStatementClose
in interfaceBrokeredConnectionControl
- Parameters:
statement
- thePreparedStatement
that was closed
-
onStatementErrorOccurred
Raise the statementErrorOccurred event for all the listeners when the corresponding events occurs- Specified by:
onStatementErrorOccurred
in interfaceBrokeredConnectionControl
- Parameters:
statement
- thePreparedStatement
in which the error occurredsqle
- theSQLException
that was thrown
-
removeStatementEventListener
Removes the specifiedStatementEventListener
from the list of components that will be notified when the driver detects that aPreparedStatement
has been closed or is invalid.- Specified by:
removeStatementEventListener
in interfacePooledConnection
- Parameters:
listener
- the component which implements theStatementEventListener
interface that was previously registered with thisPooledConnection
object
-
addStatementEventListener
Registers aStatementEventListener
with thisPooledConnection
object. Components that wish to be notified whenPreparedStatement
s created by the connection are closed or are detected to be invalid may use this method to register aStatementEventListener
with thisPooledConnection
object.- Specified by:
addStatementEventListener
in interfacePooledConnection
- Parameters:
listener
- an component which implements theStatementEventListener
interface that is to be registered with thisPooledConnection
object
-
getLanguageConnectionContext
Gets the LanguageConnectionContext for this connection.
-