KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > JdbcConnectionFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.datasource;
9
10 import org.apache.avalon.framework.logger.AbstractLogEnabled;
11 import org.apache.avalon.excalibur.pool.ObjectFactory;
12
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.sql.DriverManager JavaDoc;
15 import java.sql.Connection JavaDoc;
16
17 /**
18  * The Factory implementation for JdbcConnections.
19  *
20  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
21  * @version CVS $Revision: 1.10 $ $Date: 2002/01/26 16:58:06 $
22  * @since 4.0
23  */

24 public class JdbcConnectionFactory extends AbstractLogEnabled implements ObjectFactory
25 {
26     private final String JavaDoc m_dburl;
27     private final String JavaDoc m_username;
28     private final String JavaDoc m_password;
29     private final boolean m_autoCommit;
30     private final String JavaDoc m_keepAlive;
31     private final Class JavaDoc m_class;
32     private final static String JavaDoc DEFAULT_KEEPALIVE = "SELECT 1";
33     private final static String JavaDoc ORACLE_KEEPALIVE = JdbcConnectionFactory.DEFAULT_KEEPALIVE + " FROM DUAL";
34     private Connection JavaDoc m_firstConnection;
35
36     /**
37      * @deprecated Use the new constructor with the keepalive and connectionClass
38      * specified.
39      */

40     public JdbcConnectionFactory( final String JavaDoc url,
41                                   final String JavaDoc username,
42                                   final String JavaDoc password,
43                                   final boolean autoCommit,
44                                   final boolean oradb )
45     {
46         this(url, username, password, autoCommit, oradb, null);
47     }
48
49     /**
50      * @ deprecated Use the new constructor with the keepalive and connectionClass
51      * specified.
52      */

53     public JdbcConnectionFactory( final String JavaDoc url,
54                                   final String JavaDoc username,
55                                   final String JavaDoc password,
56                                   final boolean autoCommit,
57                                   final boolean oradb,
58                                   final String JavaDoc connectionClass)
59     {
60         this(url, username, password, autoCommit, (oradb) ? JdbcConnectionFactory.ORACLE_KEEPALIVE : JdbcConnectionFactory.DEFAULT_KEEPALIVE, connectionClass);
61     }
62     
63     /**
64      * Creates and configures a new JdbcConnectionFactory.
65      *
66      * @param url full JDBC database url.
67      * @param username username to use when connecting to the database.
68      * @param password password to use when connecting to the database.
69      * @param autoCommit true if connections to the database should operate with auto commit
70      * enabled.
71      * @param keepAlive a query which will be used to check the statis of a connection after it
72      * has been idle. A null value will cause the keep alive feature to
73      * be disabled.
74      * @param connectionClass class of connections created by the factory.
75      */

76     public JdbcConnectionFactory( final String JavaDoc url,
77                                   final String JavaDoc username,
78                                   final String JavaDoc password,
79                                   final boolean autoCommit,
80                                   final String JavaDoc keepAlive,
81                                   final String JavaDoc connectionClass)
82     {
83         this.m_dburl = url;
84         this.m_username = username;
85         this.m_password = password;
86         this.m_autoCommit = autoCommit;
87         this.m_keepAlive = keepAlive;
88
89         Class JavaDoc clazz = null;
90
91         try
92         {
93             if( null == m_username )
94             {
95                 m_firstConnection = DriverManager.getConnection( m_dburl );
96             }
97             else
98             {
99                 m_firstConnection = DriverManager.getConnection( m_dburl, m_username, m_password );
100             }
101
102             String JavaDoc className = connectionClass;
103             if ( null == className )
104             {
105                 try
106                 {
107                     java.lang.reflect.Method JavaDoc meth = m_firstConnection.getClass().getMethod("getHoldability", new Class JavaDoc[] {});
108                     className = "org.apache.avalon.excalibur.datasource.Jdbc3Connection";
109                 }
110                 catch (Exception JavaDoc e)
111                 {
112                     className = "org.apache.avalon.excalibur.datasource.JdbcConnection";
113                 }
114             }
115
116             clazz = Thread.currentThread().getContextClassLoader().loadClass( className );
117         }
118         catch (Exception JavaDoc e)
119         {
120             // ignore for now
121
}
122
123         this.m_class = clazz;
124     }
125
126     public Object JavaDoc newInstance() throws Exception JavaDoc
127     {
128         AbstractJdbcConnection jdbcConnection = null;
129         Connection JavaDoc connection = m_firstConnection;
130
131         if ( null == connection )
132         {
133             if( null == m_username )
134             {
135                 connection = DriverManager.getConnection( m_dburl );
136             }
137             else
138             {
139                 connection = DriverManager.getConnection( m_dburl, m_username, m_password );
140             }
141         }
142         else
143         {
144             m_firstConnection = null;
145         }
146
147         if ( null != this.m_class )
148         {
149             try
150             {
151                 Class JavaDoc[] paramTypes = new Class JavaDoc[] { Connection JavaDoc.class, String JavaDoc.class };
152                 Object JavaDoc[] params = new Object JavaDoc[] { connection, this.m_keepAlive };
153
154                 Constructor JavaDoc constructor = m_class.getConstructor( paramTypes );
155                 jdbcConnection = (AbstractJdbcConnection) constructor.newInstance( params );
156             }
157             catch ( Exception JavaDoc e )
158             {
159                 try
160                 {
161                     // Support the deprecated connection constructor as well.
162
boolean oracleKeepAlive = ( m_keepAlive != null ) && m_keepAlive.equalsIgnoreCase( JdbcConnectionFactory.ORACLE_KEEPALIVE );
163                     
164                     Class JavaDoc[] paramTypes = new Class JavaDoc[] { Connection JavaDoc.class, boolean.class };
165                     Object JavaDoc[] params = new Object JavaDoc[] { connection, new Boolean JavaDoc( oracleKeepAlive ) };
166
167                     Constructor JavaDoc constructor = m_class.getConstructor( paramTypes );
168                     jdbcConnection = (AbstractJdbcConnection) constructor.newInstance( params );
169                 }
170                 catch ( Exception JavaDoc ie )
171                 {
172                     if ( getLogger().isDebugEnabled() )
173                     {
174                         getLogger().debug("Exception in JdbcConnectionFactory.newInstance:", ie);
175                     }
176
177                     throw new NoValidConnectionException(ie.getMessage());
178                 }
179             }
180         }
181         else
182         {
183             throw new NoValidConnectionException("No valid JdbcConnection class available");
184         }
185
186         jdbcConnection.enableLogging(getLogger());
187
188         // Not all drivers are friendly to explicitly setting autocommit
189
if (jdbcConnection.getAutoCommit() != m_autoCommit) {
190             jdbcConnection.setAutoCommit(m_autoCommit);
191         }
192
193         if (getLogger().isDebugEnabled())
194         {
195             getLogger().debug( "JdbcConnection object created" );
196         }
197
198         return jdbcConnection;
199     }
200
201     public Class JavaDoc getCreatedClass()
202     {
203         return m_class;
204     }
205
206     public void decommission(Object JavaDoc object) throws Exception JavaDoc
207     {
208         if (object instanceof AbstractJdbcConnection) {
209             ((AbstractJdbcConnection) object).dispose();
210         }
211     }
212 }
213
Popular Tags