99import java .lang .reflect .Constructor ;
1010import java .sql .Connection ;
1111import java .sql .Driver ;
12+ import java .sql .SQLException ;
1213import java .util .Properties ;
1314
1415import org .hibernate .HibernateException ;
1516import org .hibernate .cfg .Environment ;
1617import org .hibernate .dialect .Dialect ;
1718import org .hibernate .engine .jdbc .dialect .spi .DatabaseMetaDataDialectResolutionInfoAdapter ;
1819import org .hibernate .engine .jdbc .dialect .spi .DialectResolutionInfo ;
20+ import org .hibernate .exception .JDBCConnectionException ;
1921import org .hibernate .internal .util .ReflectHelper ;
2022
2123/**
@@ -27,27 +29,47 @@ public final class DialectContext {
2729
2830 static void init () {
2931 final Properties properties = Environment .getProperties ();
32+ final String diverClassName = properties .getProperty ( Environment .DRIVER );
3033 final String dialectName = properties .getProperty ( Environment .DIALECT );
34+ final String jdbcUrl = properties .getProperty ( Environment .URL );
35+ final Properties props = new Properties ();
36+ props .setProperty ( "user" , properties .getProperty ( Environment .USER ) );
37+ props .setProperty ( "password" , properties .getProperty ( Environment .PASS ) );
3138 if ( dialectName == null ) {
3239 throw new HibernateException ( "The dialect was not set. Set the property hibernate.dialect." );
3340 }
41+ final Constructor <? extends Dialect > constructor ;
3442 try {
43+ @ SuppressWarnings ("unchecked" )
3544 final Class <? extends Dialect > dialectClass = ReflectHelper .classForName ( dialectName );
36- final Constructor <? extends Dialect > constructor = dialectClass .getConstructor ( DialectResolutionInfo .class );
37- Driver driver = (Driver ) Class .forName ( properties .getProperty ( Environment .DRIVER ) ).newInstance ();
38- Properties props = new Properties ();
39- props .setProperty ( "user" , properties .getProperty ( Environment .USER ) );
40- props .setProperty ( "password" , properties .getProperty ( Environment .PASS ) );
41- try (Connection connection = driver .connect ( properties .getProperty ( Environment .URL ), props )) {
42- dialect = constructor .newInstance ( new DatabaseMetaDataDialectResolutionInfoAdapter ( connection .getMetaData () ) );
43- }
45+ constructor = dialectClass .getConstructor ( DialectResolutionInfo .class );
4446 }
4547 catch (ClassNotFoundException cnfe ) {
4648 throw new HibernateException ( "Dialect class not found: " + dialectName , cnfe );
4749 }
4850 catch (Exception e ) {
4951 throw new HibernateException ( "Could not instantiate given dialect class: " + dialectName , e );
5052 }
53+ final Driver driver ;
54+ try {
55+ driver = (Driver ) Class .forName ( diverClassName ).newInstance ();
56+ }
57+ catch (ClassNotFoundException cnfe ) {
58+ throw new HibernateException ( "JDBC Driver class not found: " + dialectName , cnfe );
59+ }
60+ catch (Exception e ) {
61+ throw new HibernateException ( "Could not instantiate given JDBC driver class: " + dialectName , e );
62+ }
63+ try ( Connection connection = driver .connect ( jdbcUrl , props ) ) {
64+ dialect = constructor .newInstance ( new DatabaseMetaDataDialectResolutionInfoAdapter ( connection .getMetaData () ) );
65+ }
66+ catch (SQLException sqle ) {
67+ throw new JDBCConnectionException ( "Could not connect to database with JDBC URL: '"
68+ + jdbcUrl + "' [" + sqle .getMessage () + "]" , sqle );
69+ }
70+ catch (Exception e ) {
71+ throw new HibernateException ( "Could not instantiate given dialect class: " + dialectName , e );
72+ }
5173 }
5274
5375 private DialectContext () {
0 commit comments