1 21 package org.apache.derby.client; 22 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import org.apache.derby.client.net.NetXAConnection; 26 import org.apache.derby.jdbc.ClientBaseDataSource; 27 import org.apache.derby.jdbc.ClientDataSource; 28 import org.apache.derby.jdbc.ClientDriver; 29 import org.apache.derby.client.am.ClientMessageId; 30 import org.apache.derby.client.am.SqlException; 31 import org.apache.derby.client.net.NetLogWriter; 32 import org.apache.derby.shared.common.reference.SQLState; 33 34 public class ClientPooledConnection implements javax.sql.PooledConnection { 35 private boolean newPC_ = true; 36 37 private java.util.Vector listeners_ = null; 38 org.apache.derby.client.am.Connection physicalConnection_ = null; 39 org.apache.derby.client.net.NetConnection netPhysicalConnection_ = null; 40 org.apache.derby.client.net.NetXAConnection netXAPhysicalConnection_ = null; 41 42 org.apache.derby.client.am.LogicalConnection logicalConnection_ = null; 43 44 protected org.apache.derby.client.am.LogWriter logWriter_ = null; 45 46 protected int rmId_ = 0; 47 48 private ClientBaseDataSource ds_; 50 private String user_; 51 private String password_; 52 53 public ClientPooledConnection(ClientBaseDataSource ds, 57 org.apache.derby.client.am.LogWriter logWriter, 58 String user, 59 String password) throws SQLException { 60 try 61 { 62 logWriter_ = logWriter; 63 ds_ = ds; 64 user_ = user; 65 password_ = password; 66 listeners_ = new java.util.Vector (); 67 68 75 netPhysicalConnection_ = (org.apache.derby.client.net.NetConnection) 76 ClientDriver.getFactory().newNetConnection( 77 (NetLogWriter) logWriter_, 78 user, 79 password, 80 ds, 81 -1, 82 false, 83 this); 84 85 physicalConnection_ = netPhysicalConnection_; 86 } 87 catch ( SqlException se ) 88 { 89 throw se.getSQLException(); 90 } 91 } 92 93 public ClientPooledConnection(ClientBaseDataSource ds, 97 org.apache.derby.client.am.LogWriter logWriter, 98 String user, 99 String password, 100 int rmId) throws SQLException { 101 try { 102 logWriter_ = logWriter; 103 ds_ = ds; 104 user_ = user; 105 password_ = password; 106 rmId_ = rmId; 107 listeners_ = new java.util.Vector (); 108 netXAPhysicalConnection_ = getNetXAConnection(ds, 109 (NetLogWriter) logWriter_, 110 user, 111 password, 112 rmId); 113 physicalConnection_ = netXAPhysicalConnection_.getNetConnection(); 114 } catch ( SqlException se ) { 115 throw se.getSQLException(); 116 } 117 } 118 119 public ClientPooledConnection(ClientBaseDataSource ds, 120 org.apache.derby.client.am.LogWriter logWriter) throws SQLException { 121 logWriter_ = logWriter; 122 ds_ = ds; 123 listeners_ = new java.util.Vector (); 124 try { 125 netPhysicalConnection_ = (org.apache.derby.client.net.NetConnection) 126 ClientDriver.getFactory().newNetConnection( 127 (NetLogWriter) logWriter_, 128 null, 129 null, 130 ds, 131 -1, 132 false); 133 134 physicalConnection_ = netPhysicalConnection_; 135 } 136 catch (SqlException se) 137 { 138 throw se.getSQLException(); 139 } 140 } 141 142 protected void finalize() throws java.lang.Throwable { 143 if (logWriter_ != null) { 144 logWriter_.traceEntry(this, "finalize"); 145 } 146 close(); 147 } 148 149 public synchronized void close() throws SQLException { 150 try 151 { 152 if (logWriter_ != null) { 153 logWriter_.traceEntry(this, "close"); 154 } 155 156 if (logicalConnection_ != null) { 157 logicalConnection_.nullPhysicalConnection(); 158 logicalConnection_ = null; 159 } 160 161 if (physicalConnection_ == null) { 162 return; 163 } 164 165 physicalConnection_.closeResources(); 168 } 169 finally 170 { 171 physicalConnection_ = null; 172 } 173 } 174 175 public synchronized java.sql.Connection getConnection() throws SQLException { 178 try 179 { 180 if (logWriter_ != null) { 181 logWriter_.traceEntry(this, "getConnection"); 182 } 183 createLogicalConnection(); 184 185 186 if (!newPC_) { 187 physicalConnection_.reset(logWriter_, user_, password_, ds_, true); 192 } 193 else { 194 physicalConnection_.lightReset(); } 196 newPC_ = false; 197 198 if (logWriter_ != null) { 199 logWriter_.traceExit(this, "getConnection", logicalConnection_); 200 } 201 return logicalConnection_; 202 } 203 catch (SqlException se) 204 { 205 throw se.getSQLException(); 206 } 207 } 208 209 private void createLogicalConnection() throws SqlException { 210 if (physicalConnection_ == null) { 211 throw new SqlException(logWriter_, 212 new ClientMessageId(SQLState.NOGETCONN_ON_CLOSED_POOLED_CONNECTION)); 213 } 214 215 try { 220 if ( physicalConnection_.transactionInProgress() ) { 221 physicalConnection_.rollback(); 222 } 223 } catch ( SQLException sqle ) { 224 throw new SqlException(sqle); 225 } 226 227 if (logicalConnection_ != null) { 230 logicalConnection_.closeWithoutRecyclingToPool(); 231 } 232 logicalConnection_ = ClientDriver.getFactory().newLogicalConnection( 233 physicalConnection_, 234 this); 235 } 236 237 public synchronized void addConnectionEventListener(javax.sql.ConnectionEventListener listener) { 238 if (logWriter_ != null) { 239 logWriter_.traceEntry(this, "addConnectionEventListener", listener); 240 } 241 listeners_.addElement(listener); 242 } 243 244 public synchronized void removeConnectionEventListener(javax.sql.ConnectionEventListener listener) { 245 if (logWriter_ != null) { 246 logWriter_.traceEntry(this, "removeConnectionEventListener", listener); 247 } 248 listeners_.removeElement(listener); 249 } 250 251 public void recycleConnection() { 253 if (physicalConnection_.agent_.loggingEnabled()) { 254 physicalConnection_.agent_.logWriter_.traceEntry(this, "recycleConnection"); 255 } 256 257 for (java.util.Enumeration e = listeners_.elements(); e.hasMoreElements();) { 258 javax.sql.ConnectionEventListener listener = (javax.sql.ConnectionEventListener ) e.nextElement(); 259 javax.sql.ConnectionEvent event = new javax.sql.ConnectionEvent (this); 260 listener.connectionClosed(event); 261 } 262 } 263 264 public void trashConnection(SqlException exception) { 266 for (java.util.Enumeration e = listeners_.elements(); e.hasMoreElements();) { 267 javax.sql.ConnectionEventListener listener = (javax.sql.ConnectionEventListener ) e.nextElement(); 268 java.sql.SQLException sqle = exception.getSQLException(); 269 javax.sql.ConnectionEvent event = new javax.sql.ConnectionEvent (this, sqle); 270 listener.connectionErrorOccurred(event); 271 } 272 } 273 274 public synchronized void nullLogicalConnection() { 276 logicalConnection_ = null; 277 } 278 279 280 286 287 297 public void onStatementClose(PreparedStatement statement) { 298 299 } 300 301 311 public void onStatementErrorOccurred(PreparedStatement statement, 312 SQLException sqle) { 313 314 } 315 316 326 protected NetXAConnection getNetXAConnection (ClientBaseDataSource ds, 327 NetLogWriter logWriter, 328 String user, 329 String password, 330 int rmId) throws SqlException { 331 return new NetXAConnection(logWriter, 332 user, 333 password, 334 ds, 335 rmId, 336 true, 337 this); 338 339 } 340 } 341 | Popular Tags |