Index: src/com/mysql/jdbc/jdbc2/optional/MysqlPooledConnection.java =================================================================== --- src/com/mysql/jdbc/jdbc2/optional/MysqlPooledConnection.java (revision 34541) +++ src/com/mysql/jdbc/jdbc2/optional/MysqlPooledConnection.java (working copy) @@ -92,11 +92,13 @@ // ~ Instance/static variables ............................................. - private Map connectionEventListeners; + private Map connectionEventListeners = new HashMap(); private Connection logicalHandle; private com.mysql.jdbc.Connection physicalConn; + + private final Object connectionEventListenerLock = new Object(); // ~ Constructors .......................................................... @@ -109,7 +111,6 @@ public MysqlPooledConnection(com.mysql.jdbc.Connection connection) { this.logicalHandle = null; this.physicalConn = connection; - this.connectionEventListeners = new HashMap(); } /** @@ -119,12 +120,14 @@ * @param connectioneventlistener * listener to be notified with ConnectionEvents */ - public synchronized void addConnectionEventListener( + public void addConnectionEventListener( ConnectionEventListener connectioneventlistener) { - if (this.connectionEventListeners != null) { - this.connectionEventListeners.put(connectioneventlistener, - connectioneventlistener); + synchronized (this.connectionEventListenerLock) { + if (this.connectionEventListeners != null) { + this.connectionEventListeners.put(connectioneventlistener, + connectioneventlistener); + } } } @@ -138,8 +141,10 @@ public synchronized void removeConnectionEventListener( ConnectionEventListener connectioneventlistener) { - if (this.connectionEventListeners != null) { - this.connectionEventListeners.remove(connectioneventlistener); + synchronized (this.connectionEventListenerLock) { + if (this.connectionEventListeners != null) { + this.connectionEventListeners.remove(connectioneventlistener); + } } } @@ -202,10 +207,12 @@ this.physicalConn = null; } - if (this.connectionEventListeners != null) { - this.connectionEventListeners.clear(); - - this.connectionEventListeners = null; + synchronized (this.connectionEventListenerLock) { + if (this.connectionEventListeners != null) { + this.connectionEventListeners.clear(); + + this.connectionEventListeners = null; + } } } @@ -221,29 +228,34 @@ * @param sqlException * the exception being thrown */ - protected synchronized void callConnectionEventListeners(int eventType, + protected void callConnectionEventListeners(int eventType, SQLException sqlException) { - if (this.connectionEventListeners == null) { + Map snapShot; + synchronized (this.connectionEventListenerLock) { + if (this.connectionEventListeners == null) { + + return; + } - return; + snapShot = new HashMap(this.connectionEventListeners); } - - Iterator iterator = this.connectionEventListeners.entrySet().iterator(); + Iterator iterator = snapShot.entrySet().iterator(); + ConnectionEvent connectionevent = new ConnectionEvent(this, sqlException); - + while (iterator.hasNext()) { - + ConnectionEventListener connectioneventlistener = (ConnectionEventListener) ((Map.Entry)iterator .next()).getValue(); - + if (eventType == CONNECTION_CLOSED_EVENT) { connectioneventlistener.connectionClosed(connectionevent); } else if (eventType == CONNECTION_ERROR_EVENT) { connectioneventlistener - .connectionErrorOccurred(connectionevent); + .connectionErrorOccurred(connectionevent); } } } Index: src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java =================================================================== --- src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java (revision 34541) +++ src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java (working copy) @@ -850,11 +850,6 @@ rollback(); } - if (fireClosedEvent) { - this.mpc.callConnectionEventListeners( - MysqlPooledConnection.CONNECTION_CLOSED_EVENT, null); - } - // set closed status to true so that if application client tries to // make additional // calls a sqlException will be thrown. The physical connection is @@ -862,6 +857,11 @@ // called. this.closed = true; } + + if (fireClosedEvent) { + this.mpc.callConnectionEventListeners( + MysqlPooledConnection.CONNECTION_CLOSED_EVENT, null); + } } protected void checkClosed() throws SQLException {