1919import org .hibernate .engine .jdbc .connections .spi .ConnectionProviderConfigurationException ;
2020import org .hibernate .engine .jdbc .connections .spi .DatabaseConnectionInfo ;
2121import org .hibernate .exception .JDBCConnectionException ;
22+ import org .hibernate .service .ServiceRegistry ;
2223import org .hibernate .service .UnknownUnwrapTypeException ;
2324import org .hibernate .service .spi .Configurable ;
2425import org .hibernate .service .spi .ServiceException ;
4445import static org .hibernate .internal .util .config .ConfigurationHelper .getBoolean ;
4546import static org .hibernate .internal .util .config .ConfigurationHelper .getInt ;
4647import static org .hibernate .internal .util .config .ConfigurationHelper .getLong ;
48+ import static org .hibernate .internal .util .config .ConfigurationHelper .getString ;
4749
4850/**
4951 * A connection provider that uses the {@link DriverManager} directly to open connections and provides
@@ -70,7 +72,7 @@ public class DriverManagerConnectionProviderImpl
7072
7173 // create the pool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7274
73- private volatile ServiceRegistryImplementor serviceRegistry ;
75+ private volatile ServiceRegistry serviceRegistry ;
7476
7577 @ Override
7678 public void injectServices (ServiceRegistryImplementor serviceRegistry ) {
@@ -80,67 +82,33 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
8082 @ Override
8183 public void configure (Map <String , Object > configurationValues ) {
8284 ConnectionInfoLogger .INSTANCE .usingHibernateBuiltInConnectionPool ();
83- PooledConnections pool = buildPool ( configurationValues , serviceRegistry );
85+ final PooledConnections pool = buildPool ( configurationValues , serviceRegistry );
8486 final long validationInterval = getLong ( VALIDATION_INTERVAL , configurationValues , 30 );
8587 state = new PoolState ( pool , validationInterval );
8688 }
8789
88- private PooledConnections buildPool (Map <String ,Object > configuration , ServiceRegistryImplementor serviceRegistry ) {
89- final var creator = buildCreator ( configuration , serviceRegistry );
90+ private PooledConnections buildPool (Map <String ,Object > configuration , ServiceRegistry serviceRegistry ) {
91+ // connection settings
92+ final String url = jdbcUrl ( configuration );
93+ final String driverClassName = getString ( DRIVER , configuration );
94+ final Properties connectionProps = getConnectionProperties ( configuration );
9095 final boolean autoCommit = getBoolean ( AUTOCOMMIT , configuration ); // default autocommit to false
96+ final Integer isolation = extractIsolation ( configuration );
97+ final String initSql = getString ( INIT_SQL , configuration );
98+
99+ // pool settings
91100 final int minSize = getInt ( MIN_SIZE , configuration , 1 );
92101 final int maxSize = getInt ( POOL_SIZE , configuration , 20 );
93102 final int initialSize = getInt ( INITIAL_SIZE , configuration , minSize );
94- return new PooledConnections .Builder ( creator )
95- .autoCommit ( autoCommit )
96- .initialSize ( initialSize )
97- .minSize ( minSize )
98- .maxSize ( maxSize )
99- .validator ( this )
100- .build ();
101- }
102-
103- private static ConnectionCreator buildCreator (
104- Map <String ,Object > configurationValues , ServiceRegistryImplementor serviceRegistry ) {
105- final String url = jdbcUrl ( configurationValues );
106103
107- String driverClassName = (String ) configurationValues .get ( DRIVER );
108- boolean success = false ;
109- Driver driver = null ;
110- if ( driverClassName != null ) {
111- driver = loadDriverIfPossible ( driverClassName , serviceRegistry );
112- success = true ;
113- }
114- else {
115- //try to guess the driver class from the JDBC URL
116- for ( var database : Database .values () ) {
117- if ( database .matchesUrl ( url ) ) {
118- driverClassName = database .getDriverClassName ( url );
119- if ( driverClassName != null ) {
120- try {
121- loadDriverIfPossible ( driverClassName , serviceRegistry );
122- success = true ;
123- }
124- catch (Exception e ) {
125- //swallow it, since this was not
126- //an explicit setting by the user
127- }
128- break ;
129- }
130- }
131- }
104+ final Driver driver = loadDriver ( driverClassName , serviceRegistry , url );
105+ if ( driver == null ) {
106+ //we're hoping that the driver is already loaded
107+ logAvailableDrivers ();
132108 }
133109
134- final String driverList = success ? driverClassName : driverList ();
135-
136- final Properties connectionProps = getConnectionProperties ( configurationValues );
137-
138- final boolean autoCommit = getBoolean ( AUTOCOMMIT , configurationValues );
139- final Integer isolation = extractIsolation ( configurationValues );
140- final String initSql = (String ) configurationValues .get ( INIT_SQL );
141-
142110 final var connectionCreator =
143- getConnectionCreatorFactory ( configurationValues , serviceRegistry )
111+ getConnectionCreatorFactory ( configuration , serviceRegistry )
144112 .create (
145113 driver ,
146114 serviceRegistry ,
@@ -149,15 +117,14 @@ private static ConnectionCreator buildCreator(
149117 autoCommit ,
150118 isolation ,
151119 initSql ,
152- configurationValues
120+ configuration
153121 );
154122
155-
156123 try ( var connection = connectionCreator .createConnection () ) {
157124 dbInfo = new DatabaseConnectionInfoImpl (
158125 DriverManagerConnectionProviderImpl .class ,
159126 url ,
160- driverList ,
127+ driver == null ? null : driver . getClass (). getName () ,
161128 null ,
162129 SimpleDatabaseVersion .ZERO_VERSION ,
163130 hasSchema ( connection ),
@@ -168,8 +135,8 @@ private static ConnectionCreator buildCreator(
168135 isolation != null
169136 ? toIsolationNiceName ( isolation )
170137 : toIsolationNiceName ( getIsolation ( connection ) ),
171- getInt ( MIN_SIZE , configurationValues , 1 ) ,
172- getInt ( POOL_SIZE , configurationValues , 20 ) ,
138+ minSize ,
139+ maxSize ,
173140 getFetchSize ( connection )
174141 );
175142 if ( !connection .getAutoCommit () ) {
@@ -180,11 +147,39 @@ private static ConnectionCreator buildCreator(
180147 throw new JDBCConnectionException ( "Could not create connection" , e );
181148 }
182149
183- return connectionCreator ;
150+ return new PooledConnections .Builder ( connectionCreator )
151+ .autoCommit ( autoCommit )
152+ .initialSize ( initialSize )
153+ .minSize ( minSize )
154+ .maxSize ( maxSize )
155+ .validator ( this )
156+ .build ();
157+ }
158+
159+ private static Driver loadDriver (String driverClassName , ServiceRegistry serviceRegistry , String url ) {
160+ if ( driverClassName != null ) {
161+ return loadDriverIfPossible ( driverClassName , serviceRegistry );
162+ }
163+ else {
164+ // try to guess the driver class from the JDBC URL
165+ for ( var database : Database .values () ) {
166+ if ( database .matchesUrl ( url ) ) {
167+ final String databaseDriverClassName = database .getDriverClassName ( url );
168+ if ( databaseDriverClassName != null ) {
169+ try {
170+ return loadDriverIfPossible ( databaseDriverClassName , serviceRegistry );
171+ }
172+ catch (Exception e ) {
173+ // swallow it, since this was not an explicit setting by the user
174+ }
175+ }
176+ }
177+ }
178+ return null ;
179+ }
184180 }
185181
186- private static String driverList () {
187- //we're hoping that the driver is already loaded
182+ private static void logAvailableDrivers () {
188183 ConnectionInfoLogger .INSTANCE .jdbcDriverNotSpecified ();
189184 final var list = new StringBuilder ();
190185 DriverManager .drivers ()
@@ -194,20 +189,20 @@ private static String driverList() {
194189 }
195190 list .append ( driver .getClass ().getName () );
196191 } );
197- return list .toString ();
192+ ConnectionInfoLogger . INSTANCE . availableJdbcDrivers ( list .toString () );
198193 }
199194
200- private static String jdbcUrl (Map <String , Object > configurationValues ) {
201- final String url = (String ) configurationValues .get ( URL );
195+ private static String jdbcUrl (Map <String , Object > configuration ) {
196+ final String url = (String ) configuration .get ( URL );
202197 if ( url == null ) {
203198 throw new ConnectionProviderConfigurationException ( "No JDBC URL specified by property '" + JAKARTA_JDBC_URL + "'" );
204199 }
205200 return url ;
206201 }
207202
208203 private static ConnectionCreatorFactory getConnectionCreatorFactory (
209- Map <String , Object > configurationValues , ServiceRegistryImplementor serviceRegistry ) {
210- final Object connectionCreatorFactory = configurationValues .get ( CONNECTION_CREATOR_FACTORY );
204+ Map <String , Object > configuration , ServiceRegistry serviceRegistry ) {
205+ final Object connectionCreatorFactory = configuration .get ( CONNECTION_CREATOR_FACTORY );
211206 final ConnectionCreatorFactory factory ;
212207 if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
213208 factory = instance ;
@@ -221,7 +216,7 @@ else if ( connectionCreatorFactory != null ) {
221216 return factory == null ? ConnectionCreatorFactoryImpl .INSTANCE : factory ;
222217 }
223218
224- private static Driver loadDriverIfPossible (String driverClassName , ServiceRegistryImplementor serviceRegistry ) {
219+ private static Driver loadDriverIfPossible (String driverClassName , ServiceRegistry serviceRegistry ) {
225220 if ( driverClassName == null ) {
226221 ConnectionInfoLogger .INSTANCE .debug ( "No driver class specified" );
227222 return null ;
@@ -248,7 +243,7 @@ else if ( serviceRegistry != null ) {
248243 }
249244
250245 private static ConnectionCreatorFactory loadConnectionCreatorFactory (
251- String connectionCreatorFactoryClassName , ServiceRegistryImplementor serviceRegistry ) {
246+ String connectionCreatorFactoryClassName , ServiceRegistry serviceRegistry ) {
252247 if ( connectionCreatorFactoryClassName == null ) {
253248 ConnectionInfoLogger .INSTANCE .debug ( "No connection creator factory class specified" );
254249 return null ;
@@ -261,17 +256,17 @@ else if ( serviceRegistry != null ) {
261256 return factoryClass .newInstance ();
262257 }
263258 catch ( Exception e ) {
264- throw new ServiceException ( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded" , e );
259+ throw new ServiceException ( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName
260+ + " could not be loaded" , e );
265261 }
266262 }
267263 else {
268264 try {
269265 return (ConnectionCreatorFactory ) Class .forName ( connectionCreatorFactoryClassName ).newInstance ();
270266 }
271267 catch (Exception e1 ) {
272- throw new ServiceException (
273- "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded" ,
274- e1 );
268+ throw new ServiceException ( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName
269+ + " could not be loaded" , e1 );
275270 }
276271 }
277272 }
0 commit comments