KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > ClientPooledConnection


1 /*
2
3    Derby - Class org.apache.derby.client.ClientPooledConnection
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client;
22
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
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 JavaDoc {
35     private boolean newPC_ = true;
36
37     private java.util.Vector JavaDoc 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     // Cached stuff from constructor
49
private ClientBaseDataSource ds_;
50     private String JavaDoc user_;
51     private String JavaDoc password_;
52
53     // Constructor for Non-XA pooled connections.
54
// Using standard Java APIs, a CPDS is passed in.
55
// user/password overrides anything on the ds.
56
public ClientPooledConnection(ClientBaseDataSource ds,
57                                   org.apache.derby.client.am.LogWriter logWriter,
58                                   String JavaDoc user,
59                                   String JavaDoc password) throws SQLException JavaDoc {
60         try
61         {
62             logWriter_ = logWriter;
63             ds_ = ds;
64             user_ = user;
65             password_ = password;
66             listeners_ = new java.util.Vector JavaDoc();
67             
68             //pass the client pooled connection instance to this
69
//instance of the NetConnection object
70
//this object is then used to pass the close and the error events
71
//that occur in the PreparedStatement object back to the
72
//PooledConnection which will then raise the events
73
//on the listeners
74

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     // Constructor for XA pooled connections only.
94
// Using standard Java APIs, a CPDS is passed in.
95
// user/password overrides anything on the ds.
96
public ClientPooledConnection(ClientBaseDataSource ds,
97                                   org.apache.derby.client.am.LogWriter logWriter,
98                                   String JavaDoc user,
99                                   String JavaDoc password,
100                                   int rmId) throws SQLException JavaDoc {
101         try {
102             logWriter_ = logWriter;
103             ds_ = ds;
104             user_ = user;
105             password_ = password;
106             rmId_ = rmId;
107             listeners_ = new java.util.Vector JavaDoc();
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 JavaDoc {
121         logWriter_ = logWriter;
122         ds_ = ds;
123         listeners_ = new java.util.Vector JavaDoc();
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 JavaDoc {
143         if (logWriter_ != null) {
144             logWriter_.traceEntry(this, "finalize");
145         }
146         close();
147     }
148
149     public synchronized void close() throws SQLException JavaDoc {
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             // Even if the physcial connection is marked closed (in the pool),
166
// this will close its underlying resources.
167
physicalConnection_.closeResources();
168         }
169         finally
170         {
171             physicalConnection_ = null;
172         }
173     }
174
175     // This is the standard API for getting a logical connection handle for a pooled connection.
176
// No "resettable" properties are passed, so user, password, and all other properties may not change.
177
public synchronized java.sql.Connection JavaDoc getConnection() throws SQLException JavaDoc {
178         try
179         {
180             if (logWriter_ != null) {
181                 logWriter_.traceEntry(this, "getConnection");
182             }
183             createLogicalConnection();
184
185             
186             if (!newPC_) {
187                 // DERBY-1144 changed the last parameter of this method to true
188
// to reset the connection state to the default on
189
// PooledConnection.getConnection() otherwise the
190
// isolation level and holdability was not correct and out of sync with the server.
191
physicalConnection_.reset(logWriter_, user_, password_, ds_, true);
192             }
193             else {
194                 physicalConnection_.lightReset(); //poolfix
195
}
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         // Roll back any pending transactions. Otherwise we get an exception
216
// when we try to close the connection (even for re-use), with an error
217
// saying we can't close the connection with active transactions
218
// (this fixes DERBY-1004)
219
try {
220             if ( physicalConnection_.transactionInProgress() ) {
221                 physicalConnection_.rollback();
222             }
223         } catch ( SQLException JavaDoc sqle ) {
224             throw new SqlException(sqle);
225         }
226         
227         // Not the usual case, but if we have an existing logical connection, then we must close it by spec.
228
// We close the logical connection without notifying the pool manager that this pooled connection is availabe for reuse.
229
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 JavaDoc 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 JavaDoc listener) {
245         if (logWriter_ != null) {
246             logWriter_.traceEntry(this, "removeConnectionEventListener", listener);
247         }
248         listeners_.removeElement(listener);
249     }
250
251     // Not public, but needs to be visible to am.LogicalConnection
252
public void recycleConnection() {
253         if (physicalConnection_.agent_.loggingEnabled()) {
254             physicalConnection_.agent_.logWriter_.traceEntry(this, "recycleConnection");
255         }
256
257         for (java.util.Enumeration JavaDoc e = listeners_.elements(); e.hasMoreElements();) {
258             javax.sql.ConnectionEventListener JavaDoc listener = (javax.sql.ConnectionEventListener JavaDoc) e.nextElement();
259             javax.sql.ConnectionEvent JavaDoc event = new javax.sql.ConnectionEvent JavaDoc(this);
260             listener.connectionClosed(event);
261         }
262     }
263
264     // Not public, but needs to be visible to am.LogicalConnection
265
public void trashConnection(SqlException exception) {
266         for (java.util.Enumeration JavaDoc e = listeners_.elements(); e.hasMoreElements();) {
267             javax.sql.ConnectionEventListener JavaDoc listener = (javax.sql.ConnectionEventListener JavaDoc) e.nextElement();
268             java.sql.SQLException JavaDoc sqle = exception.getSQLException();
269             javax.sql.ConnectionEvent JavaDoc event = new javax.sql.ConnectionEvent JavaDoc(this, sqle);
270             listener.connectionErrorOccurred(event);
271         }
272     }
273
274     // Used by LogicalConnection close when it disassociates itself from the ClientPooledConnection
275
public synchronized void nullLogicalConnection() {
276         logicalConnection_ = null;
277     }
278     
279     /*-----------------------------------------------------------------*/
280     /*
281      * These methods are needed to provide StatementEvent support for
282      * derby.
283      * They are actually implemented in EmbedPooledConnection40 but have
284      * a dummy implementation here
285      */

286     
287     /**
288      *
289      * The onStatementClose contains the logic for raising the Statement Closed
290      * events. This method has a dummy implementation here to avoid error when
291      * this class is compiled with jdk1.4. The class the actual implementation
292      * in ClientPooledConnection40.
293      *
294      * @param statement The PreparedStatement that was closed
295      *
296      */

297     public void onStatementClose(PreparedStatement JavaDoc statement) {
298         
299     }
300     
301     /**
302      * The method contains the logic for raising the Statement error occurred
303      * events. This method has a dummy implementation here to avoid error when
304      * this class is compiled with jdk1.4. The class the actual implementation
305      * in ClientPooledConnection40.
306      *
307      * @param statement The PreparedStatement that was closed
308      * @param sqle The SQLException associated with the error that caused
309      * the invalidation of this PreparedStatement
310      */

311     public void onStatementErrorOccurred(PreparedStatement JavaDoc statement,
312                     SQLException JavaDoc sqle) {
313         
314     }
315     
316     /**
317      * creates and returns NetXAConnection.
318      * Overwrite this method to create different version of NetXAConnection
319      * @param ds
320      * @param logWriter
321      * @param user
322      * @param password
323      * @param rmId
324      * @return NetXAConnection
325      */

326     protected NetXAConnection getNetXAConnection (ClientBaseDataSource ds,
327                                   NetLogWriter logWriter,
328                                   String JavaDoc user,
329                                   String JavaDoc 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