KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > db > DBConnection


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.db;
66
67 import com.jcorporate.expresso.core.db.config.JDBCConfig;
68 import com.jcorporate.expresso.core.db.config.JNDIConfig;
69 import com.jcorporate.expresso.core.db.datasource.DSException;
70 import com.jcorporate.expresso.core.db.datasource.JndiDataSource;
71 import com.jcorporate.expresso.core.dbobj.DBField;
72 import com.jcorporate.expresso.core.misc.ConfigManager;
73 import com.jcorporate.expresso.core.misc.ConfigurationException;
74 import com.jcorporate.expresso.core.misc.StringUtil;
75 import com.jcorporate.expresso.kernel.util.ClassLocator;
76 import org.apache.log4j.Logger;
77
78 import java.io.PrintWriter JavaDoc;
79 import java.sql.CallableStatement JavaDoc;
80 import java.sql.Connection JavaDoc;
81 import java.sql.DatabaseMetaData JavaDoc;
82 import java.sql.DriverManager JavaDoc;
83 import java.sql.PreparedStatement JavaDoc;
84 import java.sql.ResultSet JavaDoc;
85 import java.sql.ResultSetMetaData JavaDoc;
86 import java.sql.SQLException JavaDoc;
87 import java.sql.Statement JavaDoc;
88 import java.util.ArrayList JavaDoc;
89 import java.util.Date JavaDoc;
90 import java.util.Enumeration JavaDoc;
91 import java.util.HashMap JavaDoc;
92 import java.util.Hashtable JavaDoc;
93 import java.util.Properties JavaDoc;
94 import java.util.Vector JavaDoc;
95
96
97 /**
98  * Generic database connection object - hides the implementation
99  * details of using jdbc and allows for JDBC message and exceptions to be
100  * handled better than by default.
101  * DBConnection's are also designed to be used in conjunction with connection
102  * pooling, and have special methods to support this.
103  *
104  * @author Michael Nash
105  */

106 public class DBConnection {
107
108     /**
109      * Constant name for the 'Default' database context
110      */

111     public static final String JavaDoc DEFAULT_DB_CONTEXT_NAME = "default";
112
113     /**
114      * Every connection handled by the pool gets an ID number assigned
115      */

116     private int connectionId = 0;
117
118     /**
119      * Driver Type object name to use for JDBC connection
120      */

121     private String JavaDoc dbDriverType = "";
122
123     /**
124      * Driver object name to use for JDBC connection
125      */

126     private String JavaDoc dbDriver = "";
127
128     /**
129      * URL to use when connecting via the given driver
130      */

131     private String JavaDoc dbURL = "";
132
133     /**
134      * Format to use for the connect statement
135      */

136     private String JavaDoc dbConnectFormat = "";
137
138     /**
139      * Current JDBC statement
140      */

141     private Statement JavaDoc stmnt = null;
142
143     /**
144      * Number of rows affected by last operation
145      */

146     private int lastUpdateCount = 0;
147
148     /**
149      * Description of this connection
150      */

151     private String JavaDoc myDescription = "no description";
152
153     /**
154      * Is this connection object actually connected to the database yet?
155      */

156     private boolean isConnected = false;
157
158     /**
159      * JDBC connection object
160      */

161     private Connection JavaDoc myConnection;
162
163     /**
164      * The escape character handler for a particular database
165      */

166     private EscapeHandler escapeHandler = null;
167
168     /**
169      * Any prepared statements that might be run
170      */

171     private PreparedStatement JavaDoc preparedStatement = null;
172
173     /**
174      * Any callable statements that might be run
175      */

176     private CallableStatement JavaDoc callableStatement = null;
177
178     /**
179      * Current result set
180      */

181     private ResultSet JavaDoc myResultSet;
182
183     /**
184      * The SQL statement that we run
185      */

186     private String JavaDoc strSQL;
187
188     /**
189      * Login name used to connect to the database
190      */

191     private String JavaDoc myLogin = "";
192
193     /**
194      * Password used to connect to the database
195      */

196     private String JavaDoc myPassword = "";
197
198     /**
199      * Is this connection available for use?
200      * (Used by connection pooling)
201      */

202     private boolean currentAvailable = false;
203
204     /**
205      * constant name to help with debugging
206      */

207     private static final String JavaDoc THIS_CLASS = DBConnection
208             .class.getName() + ".";
209
210     /**
211      * When was this connection last used?
212      */

213 // private Calendar lastTouched = null;
214
private long lastTouched = System.currentTimeMillis();
215
216     /**
217      * Time that the DBConnection was created.
218      */

219     private long createdTime = System.currentTimeMillis();
220
221     /**
222      * What database data type is being used for date/time fields?
223      */

224     private String JavaDoc dateTimeType = DBField.DATETIME_TYPE;
225
226     /**
227      * Don't time out this connection - it may take a while
228      */

229     private boolean immortal = false;
230
231     /**
232      * The main Connection Log
233      */

234     private static Logger log = Logger.getLogger(DBConnection.class);
235
236     /**
237      * The SQL Log Set this category to debug
238      * to see a trace of all sql statements
239      * issued against the main database
240      */

241     private static Logger sqlLog = Logger.getLogger("com.jcorporate.expresso.core.db.SQL");
242
243     /**
244      * The current data context
245      */

246     private String JavaDoc dbName = DEFAULT_DB_CONTEXT_NAME;
247
248     /**
249      * Limitation syntax database vendor specific optimisation is disabled
250      * <p/>
251      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
252      *
253      * @since Expresso 4.0
254      */

255     public static final int LIMITATION_DISABLED = 0;
256
257     /**
258      * Insert the limitation syntax after TABLE nomination
259      * <code>
260      * SELECT {COLUMNS}... FROM {TABLE}
261      * <font color="#660066">{limitation-syntax}</font>
262      * [ WHERE {where_clause}... ]
263      * [ ORDER BY {order_by_clause}... ]
264      * </code>
265      * <p/>
266      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
267      *
268      * @since Expresso 4.0
269      */

270     public static final int LIMITATION_AFTER_TABLE = 1;
271
272
273     /**
274      * Insert the limitation syntax after WHERE key word
275      * <code>
276      * SELECT {COLUMNS}... FROM {TABLE}
277      * [ WHERE {where_clause}... ]
278      * AND <font color="#660066">{limitation-syntax}</font>
279      * [ ORDER BY {order_by_clause}... ]
280      * </code>
281      * <p/>
282      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
283      *
284      * @since Expresso 4.0
285      */

286     public static final int LIMITATION_AFTER_WHERE = 2;
287
288     /**
289      * Insert the limitation syntax after ORDER BY key words
290      * <code>
291      * SELECT {COLUMNS}... FROM {TABLE}
292      * [ WHERE {where_clause}... ]
293      * [ ORDER BY {order_by_clause}... ]
294      * <font color="#660066">{limitation-syntax}</font>
295      * </code>
296      * <p/>
297      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
298      *
299      * @since Expresso 4.0
300      */

301     public static final int LIMITATION_AFTER_ORDER_BY = 3;
302
303     /**
304      * Insert the limitation syntax after TABLE nomination
305      * <code>
306      * SELECT <font color="#660066">{limitation-syntax}</font>
307      * {COLUMNS}... FROM {TABLE}
308      * [ WHERE {where_clause}... ]
309      * [ ORDER BY {order_by_clause}... ]
310      * </code>
311      * <p/>
312      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
313      *
314      * @since Expresso 4.0
315      */

316     public static final int LIMITATION_AFTER_SELECT = 4;
317
318     /**
319      * Rowset Limitation Optimisation Syntax Position.
320      * Specifies a where in the SQL command the limitation string should be
321      * inserted.
322      * <p/>
323      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
324      *
325      * @see DBConnectionPool#LIMITATION_DISABLED
326      * @see DBConnectionPool#LIMITATION_AFTER_TABLE
327      * @see DBConnectionPool#LIMITATION_AFTER_WHERE
328      * @see DBConnectionPool#LIMITATION_AFTER_ORDER_BY
329      * @see DBConnectionPool#LIMITATION_AFTER_SELECT
330      * @see com.jcorporate.expresso.core.dbobj.DBObject#searchAndRetrieveList
331      * @see com.jcorporate.expresso.core.dbobj.DBObject#getOffsetRecord()
332      * @see com.jcorporate.expresso.core
333      * .dbobj.DBObject#setOffsetRecord( int )
334      * @see com.jcorporate.expresso.core.dbobj.DBObject#getMaxRecords()
335      * @see com.jcorporate.expresso.core.dbobj.DBObject#setMaxRecords( int )
336      * @since Expresso 4.0
337      */

338     private int limitationPosition = LIMITATION_DISABLED;
339
340
341     /**
342      * Committed read transaction mode
343      * <p/>
344      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
345      *
346      * @since Expresso 5.3
347      */

348     public static final int TRANSACTION_NORMAL_MODE = 1;
349
350     /**
351      * Uncommitted read transaction mode
352      * <p/>
353      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
354      *
355      * @since Expresso 5.3
356      */

357     public static final int TRANSACTION_DIRTY_MODE = 2;
358
359     /**
360      * Repeatable read transaction mode
361      * <p/>
362      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
363      *
364      * @since Expresso 5.3
365      */

366     public static final int TRANSACTION_RESTRICTIVE_MODE = 3;
367
368     /**
369      * Serializable read transaction mode
370      * <p/>
371      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
372      *
373      * @since Expresso 5.3
374      */

375     public static final int TRANSACTION_EXCLUSIVE_MODE = 4;
376
377
378     /**
379      * Transaction isolation mode set : Default Expresso transaction mode.
380      * <p/>
381      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
382      *
383      * @see Connection#TRANSACTION_READ_COMMITTED MODE
384      * @since Expresso 5.3
385      */

386     private int transactionCommittedMode = TRANSACTION_NORMAL_MODE;
387
388     /**
389      * Transaction isolation mode set
390      * <p/>
391      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
392      *
393      * @see Connection#TRANSACTION_READ_UNCOMMITTED MODE
394      * @since Expresso 5.3
395      */

396     private int transactionUncommittedMode = TRANSACTION_DIRTY_MODE;
397
398     /**
399      * Transaction isolation mode set
400      * <p/>
401      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
402      *
403      * @see Connection#TRANSACTION_REPEATABLE_READ MODE
404      * @since Expresso 5.3
405      */

406     private int transactionRepeatableMode = TRANSACTION_RESTRICTIVE_MODE;
407
408     /**
409      * Transaction isolation mode set
410      * <p/>
411      * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
412      *
413      * @see Connection#TRANSACTION_SERIALIZABLE MODE
414      * @since Expresso 5.3
415      */

416     private int transactionSerializableMode = TRANSACTION_EXCLUSIVE_MODE;
417
418     /**
419      * Rowset Limitation Optimisation Syntax String.
420      * Specifies a string to add database query to retrieve
421      * only a finite number of rows from the <code>ResultSet</code>.
422      * <p/>
423      * <p>For example for MYSQL the string should be
424      * <code>"LIMIT %offset% , %maxrecord%"</code>
425      * </p>
426      * <p/>
427      * <p>For example for ORACLE the string should be
428      * <code>"ROWNUM >= %offset% AND ROWNUM <=%endrecord%"</code>
429      * </p>
430      * <p/>
431      * author Peter Pilgrim, Thu Jun 21 10:30:59 BST 2001
432      *
433      * @see com.jcorporate.expresso.core.dbobj.DBObject#searchAndRetrieveList()
434      * @see com.jcorporate.expresso.core.dbobj
435      * .DBObject#setOffsetRecord( int )
436      * @see com.jcorporate.expresso.core.dbobj.DBObject#setMaxRecords( int )
437      * @since Expresso 4.0
438      */

439     private String JavaDoc limitationSyntax = null;
440
441     /**
442      * The JDBC Config get from expresso-config.xml
443      * author Yves Henri AMAIZO, Sun Jul 29 01:30:59 BST 2002
444      *
445      * @see com.jcorporate.expresso.core.misc.ConfigJdbc
446      * @since Expresso 4.0
447      */

448     private JDBCConfig myJdbc = null;
449
450
451     /**
452      * The ConnectionPool that created this connection.
453      */

454     protected DBConnectionPool parentPool = null;
455
456     /**
457      * Constructor
458      * Create a new connection object
459      *
460      * @param newDBDriver The class name of the JDBC driver to use for this
461      * connection
462      * @param newDBURL The URL passed to the driver to connect to the
463      * database
464      * @param newDBConnectFormat The style or format of the call to actually
465      * connect to the database. The options are as follows:
466      * <ol><li>1: Create the connection with a call to
467      * DriverManager.getConnection(dbURL, login,
468      * password) where login and password are the
469      * supplied login name and password</li>
470      * <li>2: Create the connection with a call to
471      * DriverManager.getConnection(dbURL + "?user="
472      * + login + ";password=" + password) where login
473      * and password are the supplied login and
474      * password</li>
475      * <li>3: Create the connection by setting the login
476      * and password into a properties object and
477      * calling DriverManager.getConnection(dbURL,
478      * props) where props is the created properties
479      * object</li>
480      * <li>4: Create the connection with a string like
481      * DriverManager.getConnection(dbURL
482      * + "?user="
483      * + newLogin + "&password=" + newPassword)</li>
484      * </ol>
485      * @throws DBException If the information provided cannot be used to
486      * create a new connection
487      */

488     public DBConnection(String JavaDoc newDBDriver, String JavaDoc newDBURL,
489                         String JavaDoc newDBConnectFormat)
490             throws DBException {
491         connectionSetup("manager", newDBDriver, newDBURL, newDBConnectFormat);
492         try {
493             myJdbc = ConfigManager.getJdbcRequired(dbName);
494         } catch (ConfigurationException e) {
495             throw new DBException(e);
496         }
497     }
498
499
500     /**
501      * Constructor
502      * Create a new connection object
503      *
504      * @param newConfigJdbc The JDBC driver configuration retrieve from the context
505      * @throws DBException If the information provided cannot be used to
506      * create a new connection
507      */

508     public DBConnection(JDBCConfig newConfigJdbc) throws DBException {
509         if (newConfigJdbc != null) {
510             myJdbc = newConfigJdbc;
511             connectionSetup(newConfigJdbc.getDriverType(), newConfigJdbc.getDriver(),
512                     newConfigJdbc.getUrl(), newConfigJdbc.getConnectFormat());
513         } else {
514             String JavaDoc myName = (THIS_CLASS +
515                     "DBConnection(ConfigJdbc)");
516             throw new DBException(myName + ":ConfigJdbc not initialiazed ");
517         }
518     }
519
520     /**
521      * Constructor
522      * Create a new connection object
523      *
524      * @param newDBDriverType The JDBC driver Type name to use for this
525      * connection
526      * @param newDBDriver The class name of the JDBC driver to use for this
527      * connection
528      * @param newDBURL The URL passed to the driver to connect to the
529      * database
530      * @param newDBConnectFormat The style or format of the call to actually
531      * connect to the database. The options are as follows:
532      * <ol><li>1: Create the connection with a call to
533      * DriverManager.getConnection(dbURL, login,
534      * password) where login and password are the
535      * supplied login name and password</li>
536      * <li>2: Create the connection with a call to
537      * DriverManager.getConnection(dbURL + "?user="
538      * + login + ";password=" + password) where login
539      * and password are the supplied login and
540      * password</li>
541      * <li>3: Create the connection by setting the login
542      * and password into a properties object and
543      * calling DriverManager.getConnection(dbURL,
544      * props) where props is the created properties
545      * object</li>
546      * <li>4: Create the connection with a string like
547      * DriverManager.getConnection(dbURL
548      * + "?user="
549      * + newLogin + "&password=" + newPassword)</li>
550      * </ol>
551      * @throws DBException If the information provided cannot be used to
552      * create a new connection
553      */

554     public DBConnection(String JavaDoc newDBDriverType, String JavaDoc newDBDriver, String JavaDoc newDBURL,
555                         String JavaDoc newDBConnectFormat)
556             throws DBException {
557         connectionSetup(newDBDriverType, newDBDriver, newDBURL, newDBConnectFormat);
558         try {
559             myJdbc = ConfigManager.getJdbcRequired(dbName);
560         } catch (ConfigurationException e) {
561             throw new DBException(e);
562         }
563     }
564
565
566     /**
567      * Sets up the connection whether JDBC or JNDI Datasources
568      *
569      * @param newDBDriverType The driver type, either &quote;datasource&quote; or another name
570      * if you want a straight JDBC Driver
571      * @param newDBDriver The driver name
572      * @param newDBURL the URL of the database driver
573      * @param newDBConnectFormat The connection format. See the DBConnection constructors
574      * for more information on what this paramter means.
575      */

576     public void connectionSetup(String JavaDoc newDBDriverType, String JavaDoc newDBDriver, String JavaDoc newDBURL,
577                                 String JavaDoc newDBConnectFormat)
578             throws DBException {
579
580         dbDriverType = newDBDriverType;
581         dbDriver = newDBDriver;
582         dbURL = newDBURL;
583         dbConnectFormat = newDBConnectFormat;
584
585         try {
586             if (!dbDriverType.equalsIgnoreCase("datasource")) {
587                 ClassLocator.loadClass(dbDriver).newInstance();
588             }
589
590             Properties JavaDoc p = System.getProperties();
591             p.put("jdbc.drivers", dbDriver);
592             System.setProperties(p);
593         } catch (Exception JavaDoc se) {
594             log.error("Cant find/load the database " +
595                     "driver '" + dbDriver, se);
596             String JavaDoc myName = (THIS_CLASS +
597                     "DBConnection(String, String, String)");
598             throw new DBException(myName + ":Cant find/load the database " +
599                     "driver '" + dbDriver + "' (" +
600                     myDescription + ")");
601         }
602
603         touch();
604
605         /**
606          * If the DBConnection object's debugging level is set to DEBUG, enable
607          * tracing of the operation of the driver to standard output
608          */

609         if (log.isDebugEnabled()) {
610             try {
611                 if (!dbDriverType.equalsIgnoreCase("datasource")) {
612                     DriverManager.setLogWriter(new PrintWriter JavaDoc(java.lang.System.out));
613                 }
614             } catch (java.lang.SecurityException JavaDoc se) {
615                 log.warn("Error setting SQL Log stream: ", se);
616             }
617         }
618     } /* DBConnection(String, String, String) */
619
620     /**
621      * Checks if this connectino has timed out.
622      */

623     public void checkTimeOut()
624             throws DBException {
625         if (currentAvailable) {
626             throw new DBException(THIS_CLASS + "checkTimeOut()" + ":Connection '" + getDescription() +
627                     "' is not available - it may have timed out and been " +
628                     "returned to connection pool. Please try again.");
629         }
630     } /* checkTimeOut() */
631
632
633     /**
634      * Clear all result sets and statements associated with this connection
635      */

636     public synchronized void clear()
637             throws DBException {
638         if (myResultSet != null) {
639             try {
640                 myResultSet.close();
641             } catch (SQLException JavaDoc ex) {
642                 log.warn("Error closing resultset", ex);
643             }
644             myResultSet = null;
645         }
646         if (stmnt != null) {
647             try {
648                 stmnt.close();
649             } catch (SQLException JavaDoc ex) {
650                 log.warn("Error closing statement", ex);
651             }
652             stmnt = null;
653         }
654
655         if (preparedStatement != null) {
656             try {
657                 preparedStatement.close();
658             } catch (SQLException JavaDoc ex) {
659                 log.warn("Error closing prepared statement", ex);
660             }
661             preparedStatement = null;
662         }
663
664         this.strSQL = null;
665     } /* clear() */
666
667
668     /**
669      * Send a COMMIT to the database, closing the current transaction If the
670      * database driver claims it doesn't support transactions, then we skip
671      * this.
672      *
673      * @throws DBException If the commit does not succeed
674      */

675     public synchronized void commit()
676             throws DBException {
677
678         if (this.supportsTransactions()) {
679             try {
680                 myConnection.commit();
681             } catch (SQLException JavaDoc se) {
682                 throw new DBException(THIS_CLASS + "commit():Could not commit (" +
683                         myDescription + ")", se.getMessage());
684             }
685         }
686
687         touch();
688     } /* commit() */
689
690
691     /**
692      * Connect to the database, ready to execute statements
693      *
694      * @param newLogin Login user name to connect with
695      * @param newPassword Password to connect with
696      * @throws DBException If the connection does not succeed
697      */

698     public synchronized void connect(String JavaDoc newLogin, String JavaDoc newPassword)
699             throws DBException {
700         createdTime = System.currentTimeMillis();
701         myLogin = newLogin.trim();
702         myPassword = newPassword.trim();
703
704         if (dbConnectFormat == null) {
705             String JavaDoc myName = (THIS_CLASS + "connect(String, String)");
706             throw new DBException(myName +
707                     ":dbConnectFormat argument cannot " +
708                     "be null");
709         }
710         try {
711             if (dbConnectFormat.equals("1")) {
712                 if (log.isDebugEnabled()) {
713                     log.debug("Using Connect Format #1: DriverManager.getConnection(" +
714                             dbURL + "," + newLogin + "," + newPassword +
715                             ");");
716                 }
717
718                 myConnection = DriverManager.getConnection(dbURL, newLogin,
719                         newPassword);
720             } else if (dbConnectFormat.equals("2")) {
721                 myConnection = DriverManager.getConnection(dbURL + "?user=" +
722                         newLogin +
723                         ";password=" +
724                         newPassword);
725             } else if (dbConnectFormat.equals("3")) {
726                 Properties JavaDoc props = new Properties JavaDoc();
727                 props.put("user", newLogin);
728                 props.put("password", newPassword);
729                 myConnection = DriverManager.getConnection(dbURL, props);
730             } else if (dbConnectFormat.equals("4")) {
731                 myConnection = DriverManager.getConnection(dbURL + "?user=" +
732                         newLogin +
733                         "&password=" +
734                         newPassword);
735             } else {
736                 String JavaDoc myName = (THIS_CLASS + "connect(String, String)");
737                 throw new DBException(myName + ":Unknown dbConnectFormat " +
738                         "choice:" + dbConnectFormat);
739             }
740         } catch (SQLException JavaDoc se) {
741             String JavaDoc myName = (THIS_CLASS + "connect(String, String)");
742             throw new DBException(myName +
743                     ":Cant get connection to database via driver '" +
744                     dbDriver + "' and URL '" + dbURL + "' (" +
745                     myDescription + ")", se.getMessage());
746         }
747         if (myConnection == null) {
748             String JavaDoc myName = (THIS_CLASS + "connect(String, String)");
749             throw new DBException(myName +
750                     ":Cant get connection to database via driver '" +
751                     dbDriver + "' and URL '" + dbURL +
752                     ":JDBC returned a null connection. (" +
753                     myDescription + ")");
754         }
755
756         // Open a connection
757
isConnected = true;
758         this.setAutoCommit(true);
759         touch();
760     } /* connect(String, String) */
761
762     /**
763      * Connect to the database, ready to execute statements
764      *
765      * @param newJndiDS a configuration bean describing the connection
766      * datasource properties.
767      * @throws DBException If the connection does not succeed
768      * author Yves Henri AMAIZO (Creation)
769      * @since Expresso 5.0
770      */

771     public synchronized void connect(JndiDataSource newJndiDS) throws DBException {
772         createdTime = System.currentTimeMillis();
773         myLogin = myJdbc.getLogin().trim();
774         myPassword = myJdbc.getPassword().trim();
775         if (parentPool.getJNDIConfig(myJdbc) == null) {
776             String JavaDoc myName = (THIS_CLASS + "connect(JndiDataSource)");
777             throw new DBException(myName + " JNDI info configure for datasource");
778         }
779
780         if (dbConnectFormat == null) {
781             String JavaDoc myName = (THIS_CLASS + "connect(JndiDataSource)");
782             throw new DBException(myName +
783                     ":dbConnectFormat argument cannot " +
784                     "be null");
785         }
786         try {
787             if (dbConnectFormat.equals("1")) {
788                 if (log.isDebugEnabled()) {
789                     log.debug("Using Connect Format #1: Datasource.getConnection(" +
790                             dbURL + "," + myLogin + "," + myPassword +
791                             ");");
792                 }
793
794                 myConnection = newJndiDS.getConnection();
795             } else {
796                 if (dbConnectFormat.equals("2")) {
797                     myConnection = newJndiDS.getConnection(myLogin, myPassword);
798                 } else {
799                     String JavaDoc myName = (THIS_CLASS + "connect(JndiDataSource)");
800                     throw new DBException(myName + ":Unknown dbConnectFormat " +
801                             "choice:" + dbConnectFormat);
802                 }
803             }
804         } catch (DSException se) {
805             String JavaDoc myName = (THIS_CLASS + "connect(JndiDataSource)");
806             throw new DBException(myName +
807                     ":Cannot get connection to database via JNDI DataSource '" +
808                     dbURL + "' (" +
809                     myDescription + ")", se.getMessage());
810         }
811         if (myConnection == null) {
812             String JavaDoc myName = (THIS_CLASS + "connect(JndiDataSource)");
813             throw new DBException(myName +
814                     ":Cannot get connection to database via JNDI DataSource '" +
815                     "' URL '" + dbURL +
816                     ":JNDI JDBC returned a null connection. (" +
817                     myDescription + ")");
818         }
819
820         // Open a connection
821
isConnected = true;
822         this.setAutoCommit(true);
823         touch();
824     } /* connect(JndiDataSource) */
825
826     /**
827      * Return a Statement object from this connection - for compatibility
828      * with the DBConnection object in Turbine
829      *
830      * @return The fully created statement
831      */

832     public Statement JavaDoc createStatement() {
833         try {
834             if (myConnection == null) {
835                 return null;
836             }
837
838             stmnt = myConnection.createStatement();
839
840             return stmnt;
841         } catch (SQLException JavaDoc e) {
842             log.error(e);
843         }
844
845         return stmnt;
846     } /* createStatement() */
847
848     /**
849      * Creates a <code>PreparedStatment</code> object.
850      *
851      * @param sqlString The string to create the prepared statement with.
852      * @return an instantiated prepared statement, that you then modify for
853      * your various parameters May return null if an exception occurred
854      */

855     public PreparedStatement JavaDoc createPreparedStatement(String JavaDoc sqlString) {
856         try {
857             if (myConnection == null) {
858                 return null;
859             }
860             touch();
861
862             if (preparedStatement != null) {
863                 try {
864                     preparedStatement.close();
865                 } catch (SQLException JavaDoc ex) {
866                     log.warn("Error closing older prepared statement: ", ex);
867                 }
868             }
869
870             if (sqlLog.isDebugEnabled()) {
871                 sqlLog.debug("Connection " + getId() + " preparing statement from:'" + sqlString +
872                         "' on db '" + getDataContext() + "'");
873            &