1
package org.mariadb.jdbc;
4
import java.sql.Statement;
5
import java.sql.Connection;
6
import java.sql.PreparedStatement;
7
import java.sql.SQLException;
8
import java.util.ArrayList;
12
public class MySQLPooledConnection implements PooledConnection{
14
MySQLConnection connection;
15
List<ConnectionEventListener> connectionEventListeners;
16
List<StatementEventListener> statementEventListeners;
18
public MySQLPooledConnection(MySQLConnection connection)
20
this.connection = connection;
21
connection.pooledConnection = this;
22
statementEventListeners = new ArrayList<StatementEventListener>();
23
connectionEventListeners = new ArrayList<ConnectionEventListener>();
26
* Creates and returns a <code>Connection</code> object that is a handle
27
* for the physical connection that
28
* this <code>PooledConnection</code> object represents.
29
* The connection pool manager calls this method when an application has
30
* called the method <code>DataSource.getConnection</code> and there are
31
* no <code>PooledConnection</code> objects available. See the
32
* {@link javax.sql.PooledConnection interface description} for more information.
34
* @return a <code>Connection</code> object that is a handle to
35
* this <code>PooledConnection</code> object
36
* @throws java.sql.SQLException if a database access error occurs
37
* if the JDBC driver does not support
41
public Connection getConnection() throws SQLException {
46
* Closes the physical connection that this <code>PooledConnection</code>
47
* object represents. An application never calls this method directly;
48
* it is called by the connection pool module, or manager.
50
* See the {@link javax.sql.PooledConnection interface description} for more
53
* @throws java.sql.SQLException if a database access error occurs
54
* if the JDBC driver does not support
58
public void close() throws SQLException {
59
connection.pooledConnection = null;
64
* Registers the given event listener so that it will be notified
65
* when an event occurs on this <code>PooledConnection</code> object.
67
* @param listener a component, usually the connection pool manager,
68
* that has implemented the
69
* <code>ConnectionEventListener</code> interface and wants to be
70
* notified when the connection is closed or has an error
71
* @see #removeConnectionEventListener
73
public void addConnectionEventListener(ConnectionEventListener listener) {
74
connectionEventListeners.add(listener);
78
* Removes the given event listener from the list of components that
79
* will be notified when an event occurs on this
80
* <code>PooledConnection</code> object.
82
* @param listener a component, usually the connection pool manager,
83
* that has implemented the
84
* <code>ConnectionEventListener</code> interface and
85
* been registered with this <code>PooledConnection</code> object as
87
* @see #addConnectionEventListener
89
public void removeConnectionEventListener(ConnectionEventListener listener) {
90
connectionEventListeners.remove(listener);
94
* Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. Components that
95
* wish to be notified when <code>PreparedStatement</code>s created by the
96
* connection are closed or are detected to be invalid may use this method
97
* to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object.
100
* @param listener an component which implements the <code>StatementEventListener</code>
101
* interface that is to be registered with this <code>PooledConnection</code> object
105
public void addStatementEventListener(StatementEventListener listener) {
106
statementEventListeners.add(listener);
110
* Removes the specified <code>StatementEventListener</code> from the list of
111
* components that will be notified when the driver detects that a
112
* <code>PreparedStatement</code> has been closed or is invalid.
115
* @param listener the component which implements the
116
* <code>StatementEventListener</code> interface that was previously
117
* registered with this <code>PooledConnection</code> object
121
public void removeStatementEventListener(StatementEventListener listener) {
122
statementEventListeners.remove(listener);
125
public void fireStatementClosed(Statement st) {
126
if (st instanceof PreparedStatement) {
127
StatementEvent event = new StatementEvent(this, (PreparedStatement)st);
128
for(StatementEventListener listener:statementEventListeners)
129
listener.statementClosed(event);
133
public void fireStatementErrorOccured(Statement st, SQLException e) {
134
if (st instanceof PreparedStatement) {
135
StatementEvent event = new StatementEvent(this,(PreparedStatement) st,e);
136
for(StatementEventListener listener:statementEventListeners)
137
listener.statementErrorOccurred(event);
141
public void fireConnectionClosed() {
142
ConnectionEvent event = new ConnectionEvent(this);
143
for(ConnectionEventListener listener: connectionEventListeners)
144
listener.connectionClosed(event);
147
public void fireConnectionErrorOccured(SQLException e) {
148
ConnectionEvent event = new ConnectionEvent(this,e);
149
for(ConnectionEventListener listener: connectionEventListeners)
150
listener.connectionErrorOccurred(event);