Changeset 7577 for branches/3.14-stable


Ignore:
Timestamp:
Jan 9, 2019, 3:40:55 PM (3 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #2144: Implement a fast connection test against the database

The checkJdbcDriver() method was modified to also make a connection with the driver. If there is an error the error message from the database should be visible on the first line:

Could not connect to database: jdbc:postgresql:base (FATAL: password authentication failed for user "base2user")
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.14-stable/src/core/net/sf/basedb/core/HibernateUtil.java

    r7541 r7577  
    291291    }
    292292    String dbUrl = Config.getString("db.url", "");
    293     checkJdbcDriver(dbUrl);
     293    String dbUser = Config.getString("db.username", "");
     294    String dbPassword = Config.getString("db.password", "");
     295    checkJdbcDriver(dbUrl, dbUser, dbPassword);
    294296   
    295297    Properties baseConfig = new Properties();
    296298    baseConfig.setProperty("hibernate.dialect", dialect);
    297299    baseConfig.setProperty("hibernate.connection.url", dbUrl);
    298     baseConfig.setProperty("hibernate.connection.username", Config.getString("db.username", ""));
    299     baseConfig.setProperty("hibernate.connection.password", Config.getString("db.password", ""));
     300    baseConfig.setProperty("hibernate.connection.username", dbUser);
     301    baseConfig.setProperty("hibernate.connection.password", dbPassword);
    300302    return baseConfig;
    301303  }
     
    304306    Check that a JDBC driver is registered and try to load a driver if not.
    305307  */
    306   private static void checkJdbcDriver(String dbUrl)
     308  private static void checkJdbcDriver(String dbUrl, String dbUser, String dbPassword)
    307309  {
    308310    Driver driver = null;
     
    320322      ServiceLoader<Driver> s = ServiceLoader.load(Driver.class);
    321323      Iterator<Driver> it = s.iterator();
    322       while (it.hasNext())
     324      while (driver == null && it.hasNext())
    323325      {
    324326        Driver d = it.next();
     
    335337              "; version=" + d.getMajorVersion() + "." + d.getMinorVersion()+
    336338              "; acceptsURL="+acceptUrl);
    337           if (acceptUrl && driver == null)
    338           {
    339             driver = d;
    340           }
     339          if (acceptUrl) driver = d;
    341340        }
    342341      }
     
    346345      throw new ConfigurationException("Could not find a suitable JDBC driver for URL: " + dbUrl);
    347346    }
     347   
     348    Properties props = new Properties();
     349    props.setProperty("user", Config.getString("db.username", ""));
     350    props.setProperty("password", Config.getString("db.password", ""));
     351
     352    try (Connection c = driver.connect(dbUrl, props))
     353    {
     354      if (!c.isValid(5))
     355      {
     356        throw new ConfigurationException("Could not connect to database: " + dbUrl);
     357      }
     358    }
     359    catch (SQLException ex)
     360    {
     361      throw new ConfigurationException("Could not connect to database: " + dbUrl + " (" + ex.getMessage() + ")", ex);
     362    }
     363   
    348364    log.info("Accepted JDBC driver " + driver.getClass().getName() +
    349365        "; version=" + driver.getMajorVersion() + "." + driver.getMinorVersion());
Note: See TracChangeset for help on using the changeset viewer.