KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > jdbc > ConnectionFactory


1 /*
2  * $Id: ConnectionFactory.java 6609 2006-01-29 09:50:01Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.jdbc;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.Driver JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.w3c.dom.Element JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.transaction.MinervaConnectionFactory;
38 import org.ofbiz.entity.transaction.TransactionFactory;
39
40 /**
41  * ConnectionFactory - central source for JDBC connections
42  *
43  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 6609 $
46  * @since 2.0
47  */

48 public class ConnectionFactory {
49     // Debug module name
50
public static final String JavaDoc module = ConnectionFactory.class.getName();
51
52     public static Connection JavaDoc getConnection(String JavaDoc driverName, String JavaDoc connectionUrl, Properties JavaDoc props, String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
53         // first register the JDBC driver with the DriverManager
54
if (driverName != null) {
55             ConnectionFactory.loadDriver(driverName);
56         }
57
58         try {
59             if (userName != null && userName.length() > 0)
60                 return DriverManager.getConnection(connectionUrl, userName, password);
61             else if (props != null)
62                 return DriverManager.getConnection(connectionUrl, props);
63             else
64                 return DriverManager.getConnection(connectionUrl);
65         } catch (SQLException JavaDoc e) {
66             Debug.logError(e, "SQL Error obtaining JDBC connection", module);
67             throw e;
68         }
69     }
70
71     public static Connection JavaDoc getConnection(String JavaDoc connectionUrl, String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
72         return getConnection(null, connectionUrl, null, userName, password);
73     }
74
75     public static Connection JavaDoc getConnection(String JavaDoc connectionUrl, Properties JavaDoc props) throws SQLException JavaDoc {
76         return getConnection(null, connectionUrl, props, null, null);
77     }
78
79     public static Connection JavaDoc getConnection(String JavaDoc helperName) throws SQLException JavaDoc, GenericEntityException {
80         // Debug.logVerbose("Getting a connection", module);
81

82         Connection JavaDoc con = TransactionFactory.getConnection(helperName);
83         if (con == null) {
84             Debug.logError("******* ERROR: No database connection found for helperName \"" + helperName + "\"", module);
85         }
86         return con;
87     }
88     
89     public static Connection JavaDoc tryGenericConnectionSources(String JavaDoc helperName, Element JavaDoc inlineJdbcElement) throws SQLException JavaDoc, GenericEntityException {
90         // Minerva Based
91
try {
92             Connection JavaDoc con = MinervaConnectionFactory.getConnection(helperName, inlineJdbcElement);
93             if (con != null) return con;
94         } catch (Exception JavaDoc ex) {
95             Debug.logError(ex, "There was an error getting a Minerva datasource.", module);
96         }
97
98         /* DEJ20040103 XAPool still seems to have some serious issues and isn't working right, of course we may not be using it right, but I don't really feel like trying to track it down now
99         // XAPool & JOTM Based
100         try {
101             Connection con = XaPoolConnectionFactory.getConnection(helperName, inlineJdbcElement);
102             if (con != null) return con;
103         } catch (Exception ex) {
104             Debug.logError(ex, "There was an error getting a Minerva datasource.", module);
105         }
106         */

107
108         /* DEJ20050103 This pretty much never works anyway, so leaving out to reduce error messages when things go bad
109         // next try DBCP
110         try {
111             Connection con = DBCPConnectionFactory.getConnection(helperName, inlineJdbcElement);
112             if (con != null) return con;
113         } catch (Exception ex) {
114             Debug.logError(ex, "There was an error getting a DBCP datasource.", module);
115         }
116         
117         // Default to plain JDBC.
118         String driverClassName = inlineJdbcElement.getAttribute("jdbc-driver");
119         if (driverClassName != null && driverClassName.length() > 0) {
120             try {
121                 ClassLoader loader = Thread.currentThread().getContextClassLoader();
122                 Class clazz = loader.loadClass(driverClassName);
123                 clazz.newInstance();
124             } catch (ClassNotFoundException e) {
125                 Debug.logWarning(e, "Could not find JDBC driver class named " + driverClassName, module);
126                 return null;
127             } catch (java.lang.IllegalAccessException e) {
128                 Debug.logWarning(e, "Not allowed to access JDBC driver class named " + driverClassName, module);
129                 return null;
130             } catch (java.lang.InstantiationException e) {
131                 Debug.logWarning(e, "Could not create new instance of JDBC driver class named " + driverClassName, module);
132                 return null;
133             }
134             return DriverManager.getConnection(inlineJdbcElement.getAttribute("jdbc-uri"),
135                     inlineJdbcElement.getAttribute("jdbc-username"), inlineJdbcElement.getAttribute("jdbc-password"));
136         }
137         */

138
139         return null;
140     }
141
142     public static void loadDriver(String JavaDoc driverName) throws SQLException JavaDoc {
143         if (DriverManager.getDriver(driverName) == null) {
144             try {
145                 Driver JavaDoc driver = (Driver JavaDoc) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
146                 DriverManager.registerDriver(driver);
147             } catch (ClassNotFoundException JavaDoc e) {
148                 Debug.logWarning(e, "Unable to load driver [" + driverName + "]", module);
149             } catch (InstantiationException JavaDoc e) {
150                 Debug.logWarning(e, "Unable to instantiate driver [" + driverName + "]", module);
151             } catch (IllegalAccessException JavaDoc e) {
152                 Debug.logWarning(e, "Illegal access exception [" + driverName + "]", module);
153             }
154         }
155     }
156
157     public static void unloadDriver(String JavaDoc driverName) throws SQLException JavaDoc {
158         Driver JavaDoc driver = DriverManager.getDriver(driverName);
159         if (driver != null) {
160             DriverManager.deregisterDriver(driver);
161         }
162     }
163 }
164
Popular Tags