svn commit: r1619984 - in /ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc: ConnectionFactory.java DatabaseUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1619984 - in /ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc: ConnectionFactory.java DatabaseUtil.java

jacopoc
Author: jacopoc
Date: Sat Aug 23 08:19:21 2014
New Revision: 1619984

URL: http://svn.apache.org/r1619984
Log:
Moved the logic to create unmanaged connections from ConnectionFactory to DatabaseUtil where it made more sense (it is the only place where it can be used, so made it private).

Modified:
    ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/ConnectionFactory.java
    ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java

Modified: ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/ConnectionFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/ConnectionFactory.java?rev=1619984&r1=1619983&r2=1619984&view=diff
==============================================================================
--- ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/ConnectionFactory.java (original)
+++ ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/ConnectionFactory.java Sat Aug 23 08:19:21 2014
@@ -18,14 +18,7 @@
  *******************************************************************************/
 package org.ofbiz.entity.jdbc;
 
-import java.sql.Connection;
-import java.sql.Driver;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.util.Properties;
-
 import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.entity.config.model.EntityConfig;
 import org.ofbiz.entity.connection.ConnectionFactoryInterface;
 
@@ -62,46 +55,4 @@ public class ConnectionFactory {
         }
         return connFactory;
     }
-
-    public static Connection getConnection(String driverName, String connectionUrl, Properties props, String userName, String password) throws SQLException {
-        // first register the JDBC driver with the DriverManager
-        if (driverName != null) {
-            ConnectionFactory.loadDriver(driverName);
-        }
-
-        try {
-            if (UtilValidate.isNotEmpty(userName))
-                return DriverManager.getConnection(connectionUrl, userName, password);
-            else if (props != null)
-                return DriverManager.getConnection(connectionUrl, props);
-            else
-                return DriverManager.getConnection(connectionUrl);
-        } catch (SQLException e) {
-            Debug.logError(e, "SQL Error obtaining JDBC connection", module);
-            throw e;
-        }
-    }
-
-    public static Connection getConnection(String connectionUrl, String userName, String password) throws SQLException {
-        return getConnection(null, connectionUrl, null, userName, password);
-    }
-
-    public static Connection getConnection(String connectionUrl, Properties props) throws SQLException {
-        return getConnection(null, connectionUrl, props, null, null);
-    }
-
-    public static void loadDriver(String driverName) throws SQLException {
-        if (DriverManager.getDriver(driverName) == null) {
-            try {
-                Driver driver = (Driver) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
-                DriverManager.registerDriver(driver);
-            } catch (ClassNotFoundException e) {
-                Debug.logWarning(e, "Unable to load driver [" + driverName + "]", module);
-            } catch (InstantiationException e) {
-                Debug.logWarning(e, "Unable to instantiate driver [" + driverName + "]", module);
-            } catch (IllegalAccessException e) {
-                Debug.logWarning(e, "Illegal access exception [" + driverName + "]", module);
-            }
-        }
-    }
 }

Modified: ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java?rev=1619984&r1=1619983&r2=1619984&view=diff
==============================================================================
--- ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java (original)
+++ ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java Sat Aug 23 08:19:21 2014
@@ -22,6 +22,8 @@ import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
+import java.sql.Driver;
+import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
@@ -33,6 +35,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.concurrent.Callable;
@@ -135,7 +138,7 @@ public class DatabaseUtil {
         if (!isLegacy) {
             connection = TransactionFactoryLoader.getInstance().getConnection(helperInfo);
         } else {
-            connection = ConnectionFactory.getConnection(driverName, connectionUrl, null, userName, password);
+            connection = getConnection(driverName, connectionUrl, null, userName, password);
         }
 
         if (connection == null) {
@@ -151,6 +154,36 @@ public class DatabaseUtil {
         return connection;
     }
 
+    private Connection getConnection(String driverName, String connectionUrl, Properties props, String userName, String password) throws SQLException {
+        // first register the JDBC driver with the DriverManager
+        if (driverName != null) {
+            if (DriverManager.getDriver(driverName) == null) {
+                try {
+                    Driver driver = (Driver) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
+                    DriverManager.registerDriver(driver);
+                } catch (ClassNotFoundException e) {
+                    Debug.logWarning(e, "Unable to load driver [" + driverName + "]", module);
+                } catch (InstantiationException e) {
+                    Debug.logWarning(e, "Unable to instantiate driver [" + driverName + "]", module);
+                } catch (IllegalAccessException e) {
+                    Debug.logWarning(e, "Illegal access exception [" + driverName + "]", module);
+                }
+            }
+        }
+
+        try {
+            if (UtilValidate.isNotEmpty(userName))
+                return DriverManager.getConnection(connectionUrl, userName, password);
+            else if (props != null)
+                return DriverManager.getConnection(connectionUrl, props);
+            else
+                return DriverManager.getConnection(connectionUrl);
+        } catch (SQLException e) {
+            Debug.logError(e, "SQL Error obtaining JDBC connection", module);
+            throw e;
+        }
+    }
+
     protected Connection getConnectionLogged(Collection<String> messages) {
         try {
             return getConnection();