Changeset 7533


Ignore:
Timestamp:
Nov 27, 2018, 10:25:31 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2130: Upgrade 3-rd party libraries

The change made in [7521] did not work when running inside Tomcat. Probably due to classloader issues where the DriverManager is initialized early in the startup process before web apps are accessible. It can be solved by repeating the registration with the correct class loader (implicit via ServiceLoader).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r7522 r7533  
    6464import java.util.Map;
    6565import java.util.Properties;
     66import java.util.ServiceLoader;
    6667import java.util.jar.JarEntry;
    6768import java.util.jar.JarFile;
     
    7475import java.sql.ResultSet;
    7576import java.sql.DatabaseMetaData;
     77import java.sql.Driver;
    7678import java.sql.SQLException;
    7779import java.io.IOException;
     
    288290      throw new ConfigurationException("PostgreSQLDialect has been deprecated. Use PostgreSQL9Dialect instead (base.config).");
    289291    }
     292    String dbUrl = Config.getString("db.url", "");
     293    checkJdbcDriver(dbUrl);
     294   
    290295    Properties baseConfig = new Properties();
    291296    baseConfig.setProperty("hibernate.dialect", dialect);
    292     baseConfig.setProperty("hibernate.connection.url", Config.getString("db.url", ""));
     297    baseConfig.setProperty("hibernate.connection.url", dbUrl);
    293298    baseConfig.setProperty("hibernate.connection.username", Config.getString("db.username", ""));
    294299    baseConfig.setProperty("hibernate.connection.password", Config.getString("db.password", ""));
     
    296301  }
    297302
     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 
    298352  /**
    299353    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.