1 19 20 21 package org.apache.cayenne.conn; 22 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.sql.ConnectionEvent ; 31 import javax.sql.ConnectionEventListener ; 32 import javax.sql.DataSource ; 33 import javax.sql.PooledConnection ; 34 35 42 public class PooledConnectionImpl implements PooledConnection { 43 44 private Connection connectionObj; 45 private List connectionEventListeners; 46 private boolean hadErrors; 47 private DataSource connectionSource; 48 private String userName; 49 private String password; 50 51 protected PooledConnectionImpl() { 52 this.connectionEventListeners = Collections.synchronizedList(new ArrayList (10)); 56 } 57 58 59 public PooledConnectionImpl(DataSource connectionSource, String userName, 60 String password) { 61 62 this(); 63 64 this.connectionSource = connectionSource; 65 this.userName = userName; 66 this.password = password; 67 68 } 69 70 public void reconnect() throws SQLException { 71 if (connectionObj != null) { 72 try { 73 connectionObj.close(); 74 } 75 catch (SQLException ex) { 76 } 79 finally { 80 connectionObj = null; 81 } 82 } 83 84 connectionObj = (userName != null) ? connectionSource.getConnection( 85 userName, 86 password) : connectionSource.getConnection(); 87 } 88 89 public void addConnectionEventListener(ConnectionEventListener listener) { 90 synchronized (connectionEventListeners) { 91 if (!connectionEventListeners.contains(listener)) 92 connectionEventListeners.add(listener); 93 } 94 } 95 96 public void removeConnectionEventListener(ConnectionEventListener listener) { 97 synchronized (connectionEventListeners) { 98 connectionEventListeners.remove(listener); 99 } 100 } 101 102 public void close() throws SQLException { 103 104 synchronized (connectionEventListeners) { 105 connectionEventListeners.clear(); 107 } 108 109 if (connectionObj != null) { 110 try { 111 connectionObj.close(); 112 } 113 finally { 114 connectionObj = null; 115 } 116 } 117 } 118 119 public Connection getConnection() throws SQLException { 120 if (connectionObj == null) { 121 reconnect(); 122 } 123 124 if (!connectionObj.getAutoCommit()) { 127 128 try { 129 connectionObj.setAutoCommit(true); 130 } 131 catch (SQLException sqlEx) { 132 ConnectionWrapper.sybaseAutoCommitPatch(connectionObj, sqlEx, true); 134 } 135 } 136 137 connectionObj.clearWarnings(); 138 return new ConnectionWrapper(connectionObj, this); 139 } 140 141 protected void returnConnectionToThePool() throws SQLException { 142 if (hadErrors) 144 close(); 145 else 146 this.connectionClosedNotification(); 148 } 149 150 155 public void connectionErrorNotification(SQLException exception) { 156 hadErrors = true; 158 159 synchronized (connectionEventListeners) { 160 if (connectionEventListeners.size() == 0) 161 return; 162 163 ConnectionEvent closedEvent = new ConnectionEvent (this, exception); 164 Iterator listeners = connectionEventListeners.iterator(); 165 while (listeners.hasNext()) { 166 ConnectionEventListener nextListener = (ConnectionEventListener ) listeners 167 .next(); 168 nextListener.connectionErrorOccurred(closedEvent); 169 } 170 } 171 } 172 173 177 protected void connectionClosedNotification() { 178 synchronized (connectionEventListeners) { 179 if (connectionEventListeners.size() == 0) 180 return; 181 182 ConnectionEvent closedEvent = new ConnectionEvent (this); 183 Iterator listeners = connectionEventListeners.iterator(); 184 185 while (listeners.hasNext()) { 186 ConnectionEventListener nextListener = (ConnectionEventListener ) listeners 187 .next(); 188 nextListener.connectionClosed(closedEvent); 189 } 190 } 191 } 192 } 193 | Popular Tags |