1 25 package org.ofbiz.entity.jdbc; 26 27 import java.sql.Connection ; 28 import java.sql.Driver ; 29 import java.sql.DriverManager ; 30 import java.sql.SQLException ; 31 import java.util.Properties ; 32 33 import org.w3c.dom.Element ; 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 48 public class ConnectionFactory { 49 public static final String module = ConnectionFactory.class.getName(); 51 52 public static Connection getConnection(String driverName, String connectionUrl, Properties props, String userName, String password) throws SQLException { 53 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 e) { 66 Debug.logError(e, "SQL Error obtaining JDBC connection", module); 67 throw e; 68 } 69 } 70 71 public static Connection getConnection(String connectionUrl, String userName, String password) throws SQLException { 72 return getConnection(null, connectionUrl, null, userName, password); 73 } 74 75 public static Connection getConnection(String connectionUrl, Properties props) throws SQLException { 76 return getConnection(null, connectionUrl, props, null, null); 77 } 78 79 public static Connection getConnection(String helperName) throws SQLException , GenericEntityException { 80 82 Connection 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 tryGenericConnectionSources(String helperName, Element inlineJdbcElement) throws SQLException , GenericEntityException { 90 try { 92 Connection con = MinervaConnectionFactory.getConnection(helperName, inlineJdbcElement); 93 if (con != null) return con; 94 } catch (Exception ex) { 95 Debug.logError(ex, "There was an error getting a Minerva datasource.", module); 96 } 97 98 107 108 138 139 return null; 140 } 141 142 public static void loadDriver(String driverName) throws SQLException { 143 if (DriverManager.getDriver(driverName) == null) { 144 try { 145 Driver driver = (Driver ) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance(); 146 DriverManager.registerDriver(driver); 147 } catch (ClassNotFoundException e) { 148 Debug.logWarning(e, "Unable to load driver [" + driverName + "]", module); 149 } catch (InstantiationException e) { 150 Debug.logWarning(e, "Unable to instantiate driver [" + driverName + "]", module); 151 } catch (IllegalAccessException e) { 152 Debug.logWarning(e, "Illegal access exception [" + driverName + "]", module); 153 } 154 } 155 } 156 157 public static void unloadDriver(String driverName) throws SQLException { 158 Driver driver = DriverManager.getDriver(driverName); 159 if (driver != null) { 160 DriverManager.deregisterDriver(driver); 161 } 162 } 163 } 164 | Popular Tags |