~team-java-connector/mariadb-java-client/mariadb_java_connector_1.1.7

« back to all changes in this revision

Viewing changes to src/main/java/org/mariadb/jdbc/MySQLPooledConnection.java

  • Committer: Puneet Dewan
  • Date: 2014-05-22 21:11:43 UTC
  • Revision ID: puneetd30@gmail.com-20140522211143-fk6no51e4berop92
Original code version 1.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.mariadb.jdbc;
 
2
 
 
3
import javax.sql.*;
 
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;
 
9
import java.util.List;
 
10
 
 
11
 
 
12
public class MySQLPooledConnection implements  PooledConnection{
 
13
 
 
14
    MySQLConnection connection;
 
15
    List<ConnectionEventListener> connectionEventListeners;
 
16
    List<StatementEventListener> statementEventListeners;
 
17
 
 
18
    public MySQLPooledConnection(MySQLConnection connection)
 
19
    {
 
20
       this.connection = connection;
 
21
       connection.pooledConnection = this;
 
22
       statementEventListeners = new ArrayList<StatementEventListener>();
 
23
       connectionEventListeners = new ArrayList<ConnectionEventListener>();
 
24
    }
 
25
    /**
 
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.
 
33
     *
 
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
 
38
     *                               this method
 
39
     * @since 1.4
 
40
     */
 
41
    public Connection getConnection() throws SQLException {
 
42
        return connection;
 
43
    }
 
44
 
 
45
    /**
 
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.
 
49
     * <p/>
 
50
     * See the {@link javax.sql.PooledConnection interface description} for more
 
51
     * information.
 
52
     *
 
53
     * @throws java.sql.SQLException if a database access error occurs
 
54
     *                               if the JDBC driver does not support
 
55
     *                               this method
 
56
     * @since 1.4
 
57
     */
 
58
    public void close() throws SQLException {
 
59
        connection.pooledConnection = null;
 
60
        connection.close();
 
61
    }
 
62
 
 
63
    /**
 
64
     * Registers the given event listener so that it will be notified
 
65
     * when an event occurs on this <code>PooledConnection</code> object.
 
66
     *
 
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
 
72
     */
 
73
    public void addConnectionEventListener(ConnectionEventListener listener) {
 
74
        connectionEventListeners.add(listener);
 
75
    }
 
76
 
 
77
    /**
 
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.
 
81
     *
 
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
 
86
     *                 a listener
 
87
     * @see #addConnectionEventListener
 
88
     */
 
89
    public void removeConnectionEventListener(ConnectionEventListener listener) {
 
90
       connectionEventListeners.remove(listener);
 
91
    }
 
92
 
 
93
    /**
 
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.
 
98
     * <p/>
 
99
     *
 
100
     * @param listener an component which implements the <code>StatementEventListener</code>
 
101
     *                 interface that is to be registered with this <code>PooledConnection</code> object
 
102
     *                 <p/>
 
103
     * @since 1.6
 
104
     */
 
105
    public void addStatementEventListener(StatementEventListener listener) {
 
106
        statementEventListeners.add(listener);
 
107
    }
 
108
 
 
109
    /**
 
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.
 
113
     * <p/>
 
114
     *
 
115
     * @param listener the component which implements the
 
116
     *                 <code>StatementEventListener</code> interface that was previously
 
117
     *                 registered with this <code>PooledConnection</code> object
 
118
     *                 <p/>
 
119
     * @since 1.6
 
120
     */
 
121
    public void removeStatementEventListener(StatementEventListener listener) {
 
122
       statementEventListeners.remove(listener);
 
123
    }
 
124
 
 
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);
 
130
        }
 
131
    }
 
132
 
 
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);
 
138
        }
 
139
    }
 
140
 
 
141
    public void fireConnectionClosed() {
 
142
        ConnectionEvent event = new ConnectionEvent(this);
 
143
        for(ConnectionEventListener listener: connectionEventListeners)
 
144
           listener.connectionClosed(event);
 
145
    }
 
146
 
 
147
    public void fireConnectionErrorOccured(SQLException e) {
 
148
        ConnectionEvent event = new ConnectionEvent(this,e);
 
149
        for(ConnectionEventListener listener: connectionEventListeners)
 
150
           listener.connectionErrorOccurred(event);
 
151
    }
 
152
}