Changeset 7533
- Timestamp:
- Nov 27, 2018, 10:25:31 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/HibernateUtil.java
r7522 r7533 64 64 import java.util.Map; 65 65 import java.util.Properties; 66 import java.util.ServiceLoader; 66 67 import java.util.jar.JarEntry; 67 68 import java.util.jar.JarFile; … … 74 75 import java.sql.ResultSet; 75 76 import java.sql.DatabaseMetaData; 77 import java.sql.Driver; 76 78 import java.sql.SQLException; 77 79 import java.io.IOException; … … 288 290 throw new ConfigurationException("PostgreSQLDialect has been deprecated. Use PostgreSQL9Dialect instead (base.config)."); 289 291 } 292 String dbUrl = Config.getString("db.url", ""); 293 checkJdbcDriver(dbUrl); 294 290 295 Properties baseConfig = new Properties(); 291 296 baseConfig.setProperty("hibernate.dialect", dialect); 292 baseConfig.setProperty("hibernate.connection.url", Config.getString("db.url", ""));297 baseConfig.setProperty("hibernate.connection.url", dbUrl); 293 298 baseConfig.setProperty("hibernate.connection.username", Config.getString("db.username", "")); 294 299 baseConfig.setProperty("hibernate.connection.password", Config.getString("db.password", "")); … … 296 301 } 297 302 303 /** 304 Check that a JDBC driver is registered and try to load a driver if not. 305 */ 306 private static void checkJdbcDriver(String dbUrl) 307 { 308 Driver driver = null; 309 try 310 { 311 driver = DriverManager.getDriver(dbUrl); 312 } 313 catch (SQLException ex) 314 { 315 log.warn("JDBC driver for '" + dbUrl + "' is not registered. Trying to re-load via ServiceLoader."); 316 // The driver has not been registered which can happen when the DriverManager class 317 // is initialized by the "wrong" class loader (eg. Tomcat will load this in a bootstrap 318 // class loader which doesn't have access to JAR files in the WEB-INF/lib directory) 319 // So, we need let the drivers register themselves via the ServiceLoader 320 ServiceLoader<Driver> s = ServiceLoader.load(Driver.class); 321 Iterator<Driver> it = s.iterator(); 322 while (it.hasNext()) 323 { 324 Driver d = it.next(); 325 if (d != null) 326 { 327 boolean acceptUrl = false; 328 try 329 { 330 acceptUrl = d.acceptsURL(dbUrl); 331 } 332 catch (SQLException ex2) 333 {} 334 log.info("Loaded JDBC driver " + d.getClass().getName() + 335 "; version=" + d.getMajorVersion() + "." + d.getMinorVersion()+ 336 "; acceptsURL="+acceptUrl); 337 if (acceptUrl && driver == null) 338 { 339 driver = d; 340 } 341 } 342 } 343 } 344 if (driver == null) 345 { 346 throw new ConfigurationException("Could not find a suitable JDBC driver for URL: " + dbUrl); 347 } 348 log.info("Accepted JDBC driver " + driver.getClass().getName() + 349 "; version=" + driver.getMajorVersion() + "." + driver.getMinorVersion()); 350 } 351 298 352 /** 299 353 Read all mapping files, which must be named <code>Class.hbm.xml</code> where class
Note: See TracChangeset
for help on using the changeset viewer.