1 56 57 package org.objectstyle.cayenne.conn; 58 59 import java.sql.Connection ; 60 import java.sql.SQLException ; 61 import java.util.ArrayList ; 62 import java.util.Collections ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 66 import javax.sql.ConnectionEvent ; 67 import javax.sql.ConnectionEventListener ; 68 import javax.sql.DataSource ; 69 import javax.sql.PooledConnection ; 70 71 import org.apache.log4j.Logger; 72 73 81 public class PooledConnectionImpl implements PooledConnection { 82 private static Logger logObj = Logger.getLogger(PooledConnectionImpl.class); 83 84 private Connection connectionObj; 85 private List connectionEventListeners; 86 private boolean hadErrors; 87 private DataSource connectionSource; 88 private String userName; 89 private String password; 90 91 protected PooledConnectionImpl() { 92 this.connectionEventListeners = 96 Collections.synchronizedList(new ArrayList (10)); 97 } 98 99 100 public PooledConnectionImpl( 101 DataSource connectionSource, 102 String userName, 103 String password) 104 throws SQLException { 105 106 this(); 107 108 this.connectionSource = connectionSource; 109 this.userName = userName; 110 this.password = password; 111 112 } 113 114 public void reconnect() throws SQLException { 115 if (connectionObj != null) { 116 try { 117 connectionObj.close(); 118 } catch (SQLException ex) { 119 } finally { 122 connectionObj = null; 123 } 124 } 125 126 connectionObj = 127 (userName != null) 128 ? connectionSource.getConnection(userName, password) 129 : connectionSource.getConnection(); 130 } 131 132 public void addConnectionEventListener(ConnectionEventListener listener) { 133 synchronized (connectionEventListeners) { 134 if (!connectionEventListeners.contains(listener)) 135 connectionEventListeners.add(listener); 136 } 137 } 138 139 public void removeConnectionEventListener(ConnectionEventListener listener) { 140 synchronized (connectionEventListeners) { 141 connectionEventListeners.remove(listener); 142 } 143 } 144 145 public void close() throws SQLException { 146 147 synchronized (connectionEventListeners) { 148 connectionEventListeners.clear(); 150 } 151 152 if (connectionObj != null) { 153 try { 154 connectionObj.close(); 155 } finally { 156 connectionObj = null; 157 } 158 } 159 } 160 161 public Connection getConnection() throws SQLException { 162 if (connectionObj == null) { 163 reconnect(); 164 } 165 166 if (!connectionObj.getAutoCommit()) { 169 170 try { 171 connectionObj.setAutoCommit(true); 172 } catch (SQLException sqlEx) { 173 ConnectionWrapper.sybaseAutoCommitPatch( 175 connectionObj, 176 sqlEx, 177 true); 178 } 179 } 180 181 connectionObj.clearWarnings(); 182 return new ConnectionWrapper(connectionObj, this); 183 } 184 185 protected void returnConnectionToThePool() throws SQLException { 186 if (hadErrors) 188 close(); 189 else 190 this.connectionClosedNotification(); 192 } 193 194 199 public void connectionErrorNotification(SQLException exception) { 200 hadErrors = true; 202 203 if (logObj.isDebugEnabled()) { 204 logObj.debug( 205 "Child connection error, retiring pooled connection.", 206 exception); 207 } 208 209 synchronized (connectionEventListeners) { 210 if (connectionEventListeners.size() == 0) 211 return; 212 213 ConnectionEvent closedEvent = new ConnectionEvent (this, exception); 214 Iterator listeners = connectionEventListeners.iterator(); 215 while (listeners.hasNext()) { 216 ConnectionEventListener nextListener = 217 (ConnectionEventListener ) listeners.next(); 218 nextListener.connectionErrorOccurred(closedEvent); 219 } 220 } 221 } 222 223 226 protected void connectionClosedNotification() { 227 synchronized (connectionEventListeners) { 228 if (connectionEventListeners.size() == 0) 229 return; 230 231 ConnectionEvent closedEvent = new ConnectionEvent (this); 232 Iterator listeners = connectionEventListeners.iterator(); 233 234 while (listeners.hasNext()) { 235 ConnectionEventListener nextListener = 236 (ConnectionEventListener ) listeners.next(); 237 nextListener.connectionClosed(closedEvent); 238 } 239 } 240 } 241 } | Popular Tags |