1 22 package org.enhydra.jdbc.standard; 23 24 import org.enhydra.jdbc.util.Logger; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import java.util.Vector ; 29 import javax.sql.ConnectionEvent ; 30 import javax.sql.ConnectionEventListener ; 31 import javax.sql.PooledConnection ; 32 33 46 public class StandardPooledConnection implements PooledConnection { 47 48 protected StandardConnectionPoolDataSource dataSource; 49 public Connection con; public StandardConnectionHandle connectionHandle; 51 Vector listeners; boolean isClosed; public Logger log; 55 56 59 public StandardPooledConnection( 60 StandardConnectionPoolDataSource dataSource, 61 String user, 62 String password) 63 throws SQLException { 64 this.dataSource = dataSource; 65 con = dataSource.getConnection(user, password); 66 listeners = new Vector (5, 5); 67 } 68 69 74 synchronized public Connection getConnection() 75 throws java.sql.SQLException { 76 if (connectionHandle != null) { 77 if (!connectionHandle.isClosed()) { connectionHandle.close(); } 81 } 82 newConnectionHandle(); 83 return connectionHandle; 84 } 85 86 protected void newConnectionHandle() { 87 log.debug("StandardPooledConnection:newConnectionHandle"); 88 connectionHandle = 89 new StandardConnectionHandle( 90 this, 91 dataSource.getMasterPrepStmtCache(), 92 dataSource.getPreparedStmtCacheSize()); 93 } 94 95 public void close() throws java.sql.SQLException { 96 con.close(); 97 dataSource.getMasterPrepStmtCache().remove(con.toString()); 98 } 99 100 public void addConnectionEventListener(ConnectionEventListener listener) { 101 listeners.addElement(listener); 102 } 103 104 public void removeConnectionEventListener(ConnectionEventListener listener) { 105 listeners.removeElement(listener); 106 } 107 108 112 void closeEvent() { 113 ConnectionEvent event = new ConnectionEvent (this); 114 for (int i = 0; i < listeners.size(); i++) { Object obj = listeners.elementAt(i); ConnectionEventListener cel = (ConnectionEventListener ) obj; 118 cel.connectionClosed(event); } 121 122 } 123 124 133 public void connectionErrorOccurred(ConnectionEvent event) { 134 for (int i = 0; i < listeners.size(); i++) { Object obj = listeners.elementAt(i); ConnectionEventListener cel = (ConnectionEventListener ) obj; 137 cel.connectionErrorOccurred(event); } 141 } 142 143 146 public Connection getPhysicalConnection() { 147 return con; 148 } 149 150 public void setLogger(Logger alog) { 151 log = alog; 152 } 153 154 public String toString() { 155 StringBuffer sb = new StringBuffer (); 156 sb.append("StandardPooledConnection:\n"); 157 sb.append(" is closed =<"+this.isClosed + ">\n"); 158 sb.append(" connection =<"+this.con + ">\n"); 159 160 return sb.toString(); 161 } 162 } 163 | Popular Tags |