KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > ConnectionManagerImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ConnectionManagerImpl.java,v 1.52 2005/07/22 11:34:59 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.resource;
28
29 import java.io.PrintWriter JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import javax.naming.Context JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44 import javax.resource.ResourceException JavaDoc;
45 import javax.resource.spi.ConnectionEvent JavaDoc;
46 import javax.resource.spi.ConnectionEventListener JavaDoc;
47 import javax.resource.spi.ConnectionManager JavaDoc;
48 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
49 import javax.resource.spi.ManagedConnection JavaDoc;
50 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
51 import javax.resource.spi.ResourceAllocationException JavaDoc;
52 import javax.resource.spi.ValidatingManagedConnectionFactory JavaDoc;
53 import javax.security.auth.Subject JavaDoc;
54 import javax.transaction.RollbackException JavaDoc;
55 import javax.transaction.Transaction JavaDoc;
56 import javax.transaction.xa.XAResource JavaDoc;
57
58 import org.objectweb.jonas.resource.pool.api.Pool;
59 import org.objectweb.jonas.resource.pool.api.PoolMatchFactory;
60 import org.objectweb.jonas.resource.pool.lib.HArrayPool;
61 import org.objectweb.jotm.Current;
62 import org.objectweb.jotm.TransactionResourceManager;
63 import org.objectweb.transaction.jta.ResourceManagerEventListener;
64 import org.objectweb.transaction.jta.TransactionManager;
65 import org.objectweb.util.monolog.api.BasicLevel;
66 import org.objectweb.util.monolog.api.Logger;
67 import org.objectweb.util.monolog.api.LoggerFactory;
68 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
69
70 /**
71  * Description of the ConnectionManagerImpl
72  *
73  *@author chassand
74  *created 15 novembre 2001
75  */

76 public class ConnectionManagerImpl implements ConnectionEventListener JavaDoc, ConnectionManager JavaDoc,
77                                               PoolMatchFactory, SQLManager,
78                                               TransactionResourceManager {
79
80     /**
81      * Main logger
82      */

83     protected static Logger trace = null;
84     /**
85      * Pool infomation logger
86      */

87     protected static Logger poolTrace = null;
88
89     /**
90      * Used by the server to register connections when no
91      * transactionnal context exists
92      */

93     protected ResourceManagerEventListener rmel = null;
94
95     /**
96      * Holds the resource bundle name
97      */

98     private String JavaDoc resourceBundleName = null;
99
100     /**
101      * The transaction manager in server
102      */

103     protected TransactionManager tm;
104
105     /**
106      * This hashtable allows to find the list of connection handle associated to a
107      * ManagedConnection
108      */

109     protected Hashtable JavaDoc mc2mci = null;
110
111     /**
112      * The max pool size of ManagedConnection. The default value is -1.
113      */

114     private int mcMaxPoolSize = -1;
115
116     /**
117      * The min pool size of ManagedConnection. The default value is 0
118      */

119     private int mcMinPoolSize = 0;
120
121     /**
122      * The ManagedConnectionFactory instance which represents the resource
123      * adapter.
124      */

125     private ManagedConnectionFactory JavaDoc mcf;
126     /**
127      * The ValidatingManagedConnectionFactory instance which represents the resource
128      * adapter.
129      */

130     private ValidatingManagedConnectionFactory JavaDoc vmcf = null;
131
132     /**
133      * The pool of ManagedConnections associated to a ManagedConnectionFactory
134      */

135     protected Pool poolMCs = null;
136
137     /**
138      * The list of used ManagedConnections key = transaction reference value =
139      * MCInfo
140      */

141     protected Hashtable JavaDoc usedMCs = null;
142
143     /**
144      * The default max pool size of pstmts per ManagedConnection.
145      */

146     static final int MAX_PSTMT_SIZE = 10;
147
148     /**
149      * PrepareStatement cache max pool size
150      */

151     private int maxPstmtPoolSize = MAX_PSTMT_SIZE;
152
153     /**
154      * JDBC connection level value
155      */

156     private int jdbcConnLevel = 0;
157     /**
158      * JDBC connection level value
159      */

160     private String JavaDoc jdbcConnTestStmt = "";
161
162     /**
163      * The list of managedConnection used without transaction
164      */

165     protected Vector JavaDoc mcs = new Vector JavaDoc();
166
167     /**
168      * The list of Synchronisation instance managed in this ConnectionManager
169      */

170     protected Vector JavaDoc synchros = new Vector JavaDoc();
171
172     /**
173      * This is a cache to the last instance of ResourceSpec used by the
174      * ConnectionManager. Indeed this instance is always the same.
175      */

176     private ResourceSpec rs = null;
177
178     /**
179      * The holds the transaction support level for the associated RAR file
180      */

181     private String JavaDoc transSupport = null;
182
183     /**
184      * This constant is used in the by the init method
185      */

186     public final static String JavaDoc RESOURCE_BUNDLE_NAME = "resourceBundleName";
187     /**
188      * This constant is used in the by the init method
189      */

190     public final static String JavaDoc LOGGER = "org.objectweb.util.monolog.logger";
191     /**
192      * This constant is used in the by the init method
193      */

194     public final static String JavaDoc POOL_LOGGER = "org.objectweb.util.monolog.logger_pool";
195     /**
196      * This constant is used in the by the init method
197      */

198     public final static String JavaDoc LOGGER_FACTORY = "org.objectweb.util.monolog.loggerFactory";
199     /**
200      * This constant is used in the by the init method
201      */

202     public final static String JavaDoc TRANSACTION_MANAGER = "transactionManager";
203     /**
204      * This constant is used in the by the init method
205      */

206     public final static String JavaDoc RESOURCE_MANAGER_EVENT_LISTENER = "resourceManagerEventListener";
207     /**
208      * This constant is used in the by the init method
209      */

210     public final static String JavaDoc RESOURCE_ADAPTER = "resourceAdapter";
211     /**
212      * This constant is used in the by the init method
213      */

214     public final static String JavaDoc PRINT_WRITER = "printWriter";
215
216     /*
217      * These constants define the different transaction support values
218      */

219     /**
220      * Rar doesn't support transactions
221      */

222     public final static String JavaDoc NO_TRANS_SUPPORT = "NoTransaction";
223     /**
224      * Rar supports local transactions
225      */

226     public final static String JavaDoc LOCAL_TRANS_SUPPORT = "LocalTransaction";
227     /**
228      * Rar supports XA transactions
229      */

230     public final static String JavaDoc XA_TRANS_SUPPORT = "XATransaction";
231
232     /**
233      * Constants to determine which PreparedStatement types to call
234      *
235      */

236     public final static int PSWRAP_1 = 1;
237     public final static int PSWRAP_2 = 2;
238     public final static int PSWRAP_3 = 3;
239     public final static int PSWRAP_4 = 4;
240     public final static int PSWRAP_5 = 5;
241
242     // JOTM variables
243
/**
244      * ManagedConnection used with JOTM recovery
245      */

246     private ManagedConnection JavaDoc jotmMc = null;
247     /**
248      * XAResource used with JOTM recovery
249      */

250     private XAResource JavaDoc jotmXar = null;
251     /**
252      * XA Name used with JOTM recovery
253      */

254     private String JavaDoc xaName = null;
255
256     /**
257      * Debug is enabled ?
258      */

259     private boolean isEnabledDebug = false;
260
261
262     /**
263      * ConnectionManagerImpl constructor
264      * @param transSupport String defining level of support needed
265      */

266     public ConnectionManagerImpl(String JavaDoc transSupport) {
267         if (transSupport.length() == 0) {
268             transSupport = NO_TRANS_SUPPORT;
269         } else {
270             this.transSupport = transSupport;
271         }
272     }
273
274     /**
275      * Setters method to initialize the ConnectionManager The logger instance
276      * where events are logged
277      *
278      *@param l The new Logger value
279      */

280     public void setLogger(Logger l) {
281         trace = l;
282         isEnabledDebug = trace.isLoggable(BasicLevel.DEBUG);
283     }
284
285
286     /**
287      * Setters method to initialize the ConnectionManager A logger factory to
288      * obtain a logger
289      *
290      *@param lf The new LoggerFactory value
291      */

292     public void setLoggerFactory(LoggerFactory lf) {
293         trace = lf.getLogger("org.objectweb.resource.server");
294         isEnabledDebug = trace.isLoggable(BasicLevel.DEBUG);
295     }
296
297
298     /**
299      * Setters method to initialize the ConnectionManager The printwriter where
300      * event are logged
301      *
302      *@param pw The new PrintWriter value
303      */

304     public void setPrintWriter(PrintWriter JavaDoc pw) {
305         trace = new LoggerImpl(pw);
306     }
307
308
309     /**
310      * Setters method to initialize the ConnectionManager The logger instance
311      * where events are logged
312      *
313      *@param rmel The new ResourceManagerEventListener value
314      */

315     public void setResourceManagerEventListener(
316             ResourceManagerEventListener rmel) {
317         this.rmel = rmel;
318     }
319
320
321     /**
322      * Setters method to initialize the ConnectionManager The Transaction manager
323      * linked to this resource managed
324      *
325      *@param tm TransactionManager value
326      */

327     public void setTransactionManager(TransactionManager tm) {
328         this.tm = tm;
329     }
330
331
332     /**
333      * Setters method to initialize the ConnectionManager The
334      * managedConnectionFactory instance of the resource which must be managed by
335      * this connectionManager
336      *
337      *@param tmcf The new ResourceAdapter value
338      *@exception Exception Description of Exception
339      */

340     public void setResourceAdapter(ManagedConnectionFactory JavaDoc tmcf)
341             throws Exception JavaDoc {
342         setResourceAdapter(tmcf, new ConnectionManagerPoolParams());
343     }
344
345     /**
346      * Setters method to initialize the ConnectionManager The
347      * managedConnectionFactory instance of the resource which must be maneged by
348      * this connectionManager
349      *
350      *@param tmcf The ManagedConnectionFactory object
351      *@param cmpp The pool parameters
352      *@exception Exception Description of Exception
353      */

354     public void setResourceAdapter(ManagedConnectionFactory JavaDoc tmcf,
355                                    ConnectionManagerPoolParams cmpp)
356             throws Exception JavaDoc {
357
358         // set the max/min pool values
359
if (cmpp.getPoolMax() != 0) {
360             mcMaxPoolSize = cmpp.getPoolMax();
361         }
362         if (cmpp.getPoolMin() > 0) {
363             mcMinPoolSize = cmpp.getPoolMin();
364         }
365
366         // set the jdbc connection info
367
jdbcConnLevel = cmpp.getJdbcConnLevel();
368         jdbcConnTestStmt = cmpp.getJdbcConnTestStmt();
369
370         mcf = tmcf;
371         if (mcf instanceof ValidatingManagedConnectionFactory JavaDoc) {
372             vmcf = (ValidatingManagedConnectionFactory JavaDoc) mcf;
373         }
374
375         poolMCs = new HArrayPool(poolTrace);
376         poolMCs.setMatchFactory(this);
377         poolMCs.setMaxSize(mcMaxPoolSize);
378         poolMCs.setMinSize(mcMinPoolSize);
379         poolMCs.setInitSize(cmpp.getPoolInit());
380
381         if (cmpp.getPoolMaxAge() > 0) {
382             int min = (int) (cmpp.getPoolMaxAge() / 60);
383             poolMCs.setMaxAge(min);
384         } else {
385             poolMCs.setMaxAge(cmpp.getPoolMaxAgeMinutes());
386         }
387
388         if (cmpp.getPoolMaxOpentime() > 0) {
389             poolMCs.setMaxOpentime(cmpp.getPoolMaxOpentime());
390         }
391         poolMCs.setMaxWaiters(cmpp.getPoolMaxWaiters());
392         if (cmpp.getPoolMaxWaittime() > 0) {
393             poolMCs.setMaxWaitTime(cmpp.getPoolMaxWaittime());
394         }
395
396         poolMCs.startMonitor();
397         poolMCs.setSamplingPeriod(cmpp.getPoolSamplingPeriod());
398         maxPstmtPoolSize = cmpp.getPstmtMax();
399
400         usedMCs = new Hashtable JavaDoc();
401         rs = new ResourceSpec(null, null);
402
403         if (isEnabledDebug) {
404             trace.log(BasicLevel.DEBUG, "");
405         }
406     }
407
408
409     /**
410      * This method permits to initialize the ConnectionManager with the following
411      * parameter: RESOURCE_BUNDLE_NAME: The name of the resource bundle in order
412      * to internationalize the logging LOGGER: The logger instance where events
413      * are logged LOGGER_FACTORY: A logger factory to obtain a logger
414      * PRINT_WRITER: The printwriter where event are logged TRANSACTION_MANAGER:
415      * The Transaction manager linked to this resource manager
416      * RESOURCE_MANAGER_EVENT_LISTENER: The resource manage event listener which
417      * subscribed to later connection enlistement. RESOURCE_ADAPTER: The
418      * managedConnectionFactory instance of the resource which must be maneged by
419      * this connectionManager
420      *
421      *@param ctx Description of Parameter
422      *@exception Exception Description of Exception
423      */

424     public void init(Context JavaDoc ctx) throws Exception JavaDoc {
425         mc2mci = new Hashtable JavaDoc();
426
427         // Get the resource bundle name to internationalise the log
428
// Optional
429
String JavaDoc resourceBundleName = null;
430         try {
431             resourceBundleName = (String JavaDoc) ctx.lookup(RESOURCE_BUNDLE_NAME);
432         } catch (NamingException JavaDoc e) {
433         }
434
435         // Get the logger or the logger factory or the printwriter
436
try {
437             trace = (Logger) ctx.lookup(LOGGER);
438             poolTrace = (Logger) ctx.lookup(POOL_LOGGER);
439         } catch (NamingException JavaDoc e) {
440         }
441
442         if (trace == null) {
443             try {
444                 setLoggerFactory((LoggerFactory) ctx.lookup(LOGGER_FACTORY));
445             } catch (NamingException JavaDoc e2) {
446             }
447         }
448         if (trace == null) {
449             PrintWriter JavaDoc pw = null;
450             try {
451                 pw = (PrintWriter JavaDoc) ctx.lookup(PRINT_WRITER);
452             } catch (NamingException JavaDoc e3) {
453             }
454             setPrintWriter(pw);
455         }
456
457         if (!transSupport.equalsIgnoreCase(NO_TRANS_SUPPORT)) {
458             // Get the transaction manager
459
tm = (TransactionManager) ctx.lookup(TRANSACTION_MANAGER);
460         }
461
462         // Get the set of connection
463
try {
464             rmel = (ResourceManagerEventListener)
465                     ctx.lookup(RESOURCE_MANAGER_EVENT_LISTENER);
466         } catch (NamingException JavaDoc ne) {
467         }
468
469         // Get the managedConnectionFactory instance which represents the resource
470
// adapter
471
try {
472             setResourceAdapter(
473                     (ManagedConnectionFactory JavaDoc) ctx.lookup(RESOURCE_ADAPTER));
474         } catch (NamingException JavaDoc ne) {
475         }
476     }
477
478
479     /**
480      * Description of the Method
481      *
482      *@exception ResourceException Description of Exception
483      */

484     public void cleanResourceAdapter() throws ResourceException JavaDoc {
485         // Delete the PreparedStatements
486
PreparedStatementWrapper pw = null;
487         while (mcs != null && mcs.size() > 0) {
488             MCInfo mci = (MCInfo) mcs.remove(0);
489             mci.usedCs.clear();
490             synchronized (mci.pStmts) {
491                 // Remove the PreparedStatements on the MC
492
while (mci.pStmts != null && mci.pStmts.size() > 0) {
493                     pw = (PreparedStatementWrapper) mci.pStmts.remove(0);
494                     try {
495                         pw.destroy();
496                     } catch (Exception JavaDoc ex) {
497                     }
498                 }
499             }
500             try {
501                 mc2mci.remove(mci.mc);
502                 mci.mc.destroy();
503             } catch (Exception JavaDoc ex) {
504             }
505         }
506         if (usedMCs != null) {
507             for (Enumeration JavaDoc en = usedMCs.keys(); en.hasMoreElements();) {
508                 Transaction JavaDoc tx = (Transaction JavaDoc) en.nextElement();
509                 MCInfo mci = (MCInfo) usedMCs.get(tx);
510                 if (mci == null) {
511                     continue;
512                 }
513                 if (mci.rmeCalled) {
514                     mci.rme.isValid = false;
515                     rmel.connectionClosed(mci.rme);
516                     mci.rmeCalled = false;
517                 }
518                 mci.usedCs.clear();
519                 // Remove the PreparedStatements on the MC
520
synchronized (mci.pStmts) {
521                     while (mci.pStmts != null && mci.pStmts.size() > 0) {
522                         pw = (PreparedStatementWrapper) mci.pStmts.remove(0);
523                         try {
524                             pw.destroy();
525                         } catch (Exception JavaDoc ex) {
526                         }
527                     }
528                 }
529                 try {
530                     mc2mci.remove(mci.mc);
531                     mci.mc.destroy();
532                 } catch (Exception JavaDoc ex) {
533                 }
534             }
535         }
536         while (synchros != null && synchros.size() > 0) {
537             MCInfo mci = (MCInfo) synchros.remove(0);
538             mci.usedCs.clear();
539             // Remove the PreparedStatements on the MC
540
synchronized (mci.pStmts) {
541                 while (mci.pStmts != null && mci.pStmts.size() > 0) {
542                     pw = (PreparedStatementWrapper) mci.pStmts.remove(0);
543                     try {
544                         pw.destroy();
545                     } catch (Exception JavaDoc ex) {
546                     }
547                 }
548             }
549             try {
550                 mc2mci.remove(mci.mc);
551                 mci.mc.destroy();
552             } catch (Exception JavaDoc ex) {
553             }
554         }
555     }
556
557
558     /**
559      *
560      * The method allocateConnection gets called by the resource adapter's
561      * connection factory instance.
562      *
563      * @see javax.resource.cci.ConnectionManager
564      */

565     public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc pMcf, ConnectionRequestInfo JavaDoc cxRequestInfo)
566             throws ResourceException JavaDoc {
567
568         MCInfo mci = null;
569         Transaction JavaDoc currentTx = null;
570         Object JavaDoc connection = null;
571         int retries = 0;
572         Subject JavaDoc subject = null;
573
574         while (connection == null && retries < 20) {
575             if (mcf != pMcf) {
576                 throw new ResourceException JavaDoc(
577                         "This ConnectionManager doesn't manage this RA:" + mcf);
578             }
579
580             currentTx = null;
581             try {
582                 if (tm != null) {
583                     currentTx = tm.getTransaction();
584                 }
585             } catch (Exception JavaDoc e) {
586                 trace.log(BasicLevel.ERROR,
587                         "Impossible to get the current transaction", e,
588                         "ConnectionManagerImpl", "allocateConnection");
589             }
590
591             //if there is a transaction check if a MC is already associated
592
mci = (currentTx == null ? null : (MCInfo) usedMCs.get(currentTx));
593             if (mci != null) {
594                 if (mci.mc != null) {
595                     // There are connections, try to match with the
596
// ManagedConnectionFactory
597
if (isEnabledDebug) {
598                         trace.log(BasicLevel.DEBUG, "MC (" + mci.mc + ") associated to the current Tx (" + currentTx + ") found");
599                     }
600                     Set JavaDoc s = new HashSet JavaDoc();
601                     s.add(mci.mc);
602                     if (mci.mc != mcf.matchManagedConnections(s, null, cxRequestInfo)) {
603                         throw new ResourceException JavaDoc(
604                                 "ConnectionManagerImpl.allocateConnection: illegal state : no mc is matched by mcf");
605                     }
606                     if (isEnabledDebug) {
607                         trace.log(BasicLevel.DEBUG, "XA Resource " + mci.getXAResource()
608                                                 + " is already enlisted in Tx:" + mci.ctx);
609                     }
610                 } else {
611                     // This connection occurred an error before
612
trace.log(BasicLevel.INFO, "remnant of an old failed connection");
613                     mci.ctx = null;
614                     mci = null;
615                     usedMCs.remove(currentTx);
616                 }
617             }
618
619             if (mci == null) {
620                 // No managed connection found => get a free ManagedConnection
621
// from the right pool
622

623                 //ri.rs.subject = null; // Never set => already at null
624
rs.cxRequestInfo = cxRequestInfo;
625                 if (subject == null && cxRequestInfo != null) {
626                     // Create a subject to pass on
627
}
628                 try {
629                     ManagedConnection JavaDoc mc = (ManagedConnection JavaDoc) poolMCs.getResource(rs);
630                     if (mc == null) {
631                         throw new ResourceException JavaDoc("ConnectionManagerImpl.allocateConnection: cannot allocate a ManagedConnection");
632                     }
633                     mci = (MCInfo) mc2mci.get(mc);
634                     if (mci == null) {
635                         mci = new MCInfo(mc);
636                         mc2mci.put(mc, mci);
637                     }
638                     if (isEnabledDebug) {
639                         trace.log(BasicLevel.DEBUG, "get a MC from the ra pool, mc=" + mci.mc);
640                     }
641                     if (transSupport.equalsIgnoreCase(LOCAL_TRANS_SUPPORT)) {
642                         if (mci.lw == null) {
643                             mci.lw = new LocalXAWrapper(mci.mc.getLocalTransaction(), trace);
644                         }
645                     } else if (mci.lw != null) {
646                         mci.lw = null;
647                     }
648                     if (!mci.connectionEventListener) {
649                         mci.mc.addConnectionEventListener(this);
650                         mci.connectionEventListener = true;
651                     }
652                     mci.synchro = null;
653                     // If a global transaction is already started then enlist the
654
// ManagedConnection instance
655
if (currentTx != null) {
656                         if (isEnabledDebug) {
657                             trace.log(BasicLevel.DEBUG, "Enlist the XA Resource "
658                                                     + mci.getXAResource() + " in Tx:"
659                                                     + currentTx);
660                         }
661                         currentTx.enlistResource(mci.getXAResource());
662                         usedMCs.put(currentTx, mci);
663                         mci.ctx = currentTx;
664                     } else {
665                         // There are not Transaction at the moment but the user can
666
// start a transaction after the getConnection call.
667
mci.ctx = null;
668                         // must be clean
669
mcs.add(mci);
670                         // NoTransaction is specified, so don't register the MC
671
if (!transSupport.equalsIgnoreCase(NO_TRANS_SUPPORT)) {
672                             mci.rme = new RMEImpl(mci, trace);
673                             if (isEnabledDebug) {
674                                 trace.log(BasicLevel.DEBUG, "Register the managed connection (no tx)");
675                             }
676                             // Always put in list, fix bug on connection late enlistment
677
if (!mci.rmeCalled) {
678                                 mci.rme.isValid = true;
679                                 rmel.connectionOpened(mci.rme);
680                                 mci.rmeCalled = true;
681                             }
682                         }
683                     }
684                 } catch (ResourceException JavaDoc re) {
685                     trace.log(BasicLevel.ERROR, re.getMessage(), re);
686                     throw re;
687                 } catch (Exception JavaDoc e) {
688                     String JavaDoc err = "Error related allocation of ManagedConnection";
689                     trace.log(BasicLevel.ERROR, err, e);
690                     throw new ResourceException JavaDoc(err, e);
691                 }
692
693             }
694
695             //Fetch a free Connection from the ManagedConnection
696
connection = mci.mc.getConnection(null, cxRequestInfo);
697             // Want to add the non-wrapped Connection object
698
mci.usedCs.add(connection);
699             if (connection instanceof java.sql.Connection JavaDoc) {
700                 try {
701                     // Need a wrapper for non JOnAS jdbc ConnectionImpl
702
if (connection instanceof org.objectweb.jonas.jdbc.ConnectionImpl) {
703                         ((org.objectweb.jonas.jdbc.ConnectionImpl) connection).setJonasInfo(mci, this);
704                     } else {
705                         connection = JonasSQLWrapper.createSQLWrapper(connection, mci, this, trace);
706                     }
707                     // Check the connection before reusing it
708
if (jdbcConnLevel > 0) {
709                         try {
710                             if (isEnabledDebug) {
711                                 trace.log(BasicLevel.DEBUG, "Check the JDBC connection");
712                             }
713                             boolean isClosed = true;
714                             // Used for logical testing of connection
715
if (connection instanceof org.objectweb.jonas.jdbc.ConnectionImpl) {
716                                 isClosed = ((org.objectweb.jonas.jdbc.ConnectionImpl) connection).isPhysicallyClosed();
717                             } else {
718                                 isClosed = ((Connection JavaDoc) connection).isClosed();
719                             }
720                             if (isClosed) {
721                                 connectionErrorOccurred(new ConnectionEvent JavaDoc(mci.mc,
722                                         ConnectionEvent.CONNECTION_ERROR_OCCURRED));
723                                 connection = null;
724                                 retries++;
725                                 continue;
726                             }
727                             if (jdbcConnLevel > 1 && jdbcConnTestStmt != null
728                                     && jdbcConnTestStmt.length() > 0) {
729                                 if (isEnabledDebug) {
730                                     trace.log(BasicLevel.DEBUG, "retrying connection: " + jdbcConnTestStmt);
731                                 }