19
19
import org .hibernate .engine .jdbc .connections .spi .ConnectionProviderConfigurationException ;
20
20
import org .hibernate .engine .jdbc .connections .spi .DatabaseConnectionInfo ;
21
21
import org .hibernate .exception .JDBCConnectionException ;
22
+ import org .hibernate .service .ServiceRegistry ;
22
23
import org .hibernate .service .UnknownUnwrapTypeException ;
23
24
import org .hibernate .service .spi .Configurable ;
24
25
import org .hibernate .service .spi .ServiceException ;
44
45
import static org .hibernate .internal .util .config .ConfigurationHelper .getBoolean ;
45
46
import static org .hibernate .internal .util .config .ConfigurationHelper .getInt ;
46
47
import static org .hibernate .internal .util .config .ConfigurationHelper .getLong ;
48
+ import static org .hibernate .internal .util .config .ConfigurationHelper .getString ;
47
49
48
50
/**
49
51
* A connection provider that uses the {@link DriverManager} directly to open connections and provides
@@ -70,7 +72,7 @@ public class DriverManagerConnectionProviderImpl
70
72
71
73
// create the pool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
74
73
- private volatile ServiceRegistryImplementor serviceRegistry ;
75
+ private volatile ServiceRegistry serviceRegistry ;
74
76
75
77
@ Override
76
78
public void injectServices (ServiceRegistryImplementor serviceRegistry ) {
@@ -80,67 +82,33 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
80
82
@ Override
81
83
public void configure (Map <String , Object > configurationValues ) {
82
84
ConnectionInfoLogger .INSTANCE .usingHibernateBuiltInConnectionPool ();
83
- PooledConnections pool = buildPool ( configurationValues , serviceRegistry );
85
+ final PooledConnections pool = buildPool ( configurationValues , serviceRegistry );
84
86
final long validationInterval = getLong ( VALIDATION_INTERVAL , configurationValues , 30 );
85
87
state = new PoolState ( pool , validationInterval );
86
88
}
87
89
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 );
90
95
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
91
100
final int minSize = getInt ( MIN_SIZE , configuration , 1 );
92
101
final int maxSize = getInt ( POOL_SIZE , configuration , 20 );
93
102
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 );
106
103
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 ();
132
108
}
133
109
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
-
142
110
final var connectionCreator =
143
- getConnectionCreatorFactory ( configurationValues , serviceRegistry )
111
+ getConnectionCreatorFactory ( configuration , serviceRegistry )
144
112
.create (
145
113
driver ,
146
114
serviceRegistry ,
@@ -149,15 +117,14 @@ private static ConnectionCreator buildCreator(
149
117
autoCommit ,
150
118
isolation ,
151
119
initSql ,
152
- configurationValues
120
+ configuration
153
121
);
154
122
155
-
156
123
try ( var connection = connectionCreator .createConnection () ) {
157
124
dbInfo = new DatabaseConnectionInfoImpl (
158
125
DriverManagerConnectionProviderImpl .class ,
159
126
url ,
160
- driverList ,
127
+ driver == null ? null : driver . getClass (). getName () ,
161
128
null ,
162
129
SimpleDatabaseVersion .ZERO_VERSION ,
163
130
hasSchema ( connection ),
@@ -168,8 +135,8 @@ private static ConnectionCreator buildCreator(
168
135
isolation != null
169
136
? toIsolationNiceName ( isolation )
170
137
: toIsolationNiceName ( getIsolation ( connection ) ),
171
- getInt ( MIN_SIZE , configurationValues , 1 ) ,
172
- getInt ( POOL_SIZE , configurationValues , 20 ) ,
138
+ minSize ,
139
+ maxSize ,
173
140
getFetchSize ( connection )
174
141
);
175
142
if ( !connection .getAutoCommit () ) {
@@ -180,11 +147,39 @@ private static ConnectionCreator buildCreator(
180
147
throw new JDBCConnectionException ( "Could not create connection" , e );
181
148
}
182
149
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
+ }
184
180
}
185
181
186
- private static String driverList () {
187
- //we're hoping that the driver is already loaded
182
+ private static void logAvailableDrivers () {
188
183
ConnectionInfoLogger .INSTANCE .jdbcDriverNotSpecified ();
189
184
final var list = new StringBuilder ();
190
185
DriverManager .drivers ()
@@ -194,20 +189,20 @@ private static String driverList() {
194
189
}
195
190
list .append ( driver .getClass ().getName () );
196
191
} );
197
- return list .toString ();
192
+ ConnectionInfoLogger . INSTANCE . availableJdbcDrivers ( list .toString () );
198
193
}
199
194
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 );
202
197
if ( url == null ) {
203
198
throw new ConnectionProviderConfigurationException ( "No JDBC URL specified by property '" + JAKARTA_JDBC_URL + "'" );
204
199
}
205
200
return url ;
206
201
}
207
202
208
203
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 );
211
206
final ConnectionCreatorFactory factory ;
212
207
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
213
208
factory = instance ;
@@ -221,7 +216,7 @@ else if ( connectionCreatorFactory != null ) {
221
216
return factory == null ? ConnectionCreatorFactoryImpl .INSTANCE : factory ;
222
217
}
223
218
224
- private static Driver loadDriverIfPossible (String driverClassName , ServiceRegistryImplementor serviceRegistry ) {
219
+ private static Driver loadDriverIfPossible (String driverClassName , ServiceRegistry serviceRegistry ) {
225
220
if ( driverClassName == null ) {
226
221
ConnectionInfoLogger .INSTANCE .debug ( "No driver class specified" );
227
222
return null ;
@@ -248,7 +243,7 @@ else if ( serviceRegistry != null ) {
248
243
}
249
244
250
245
private static ConnectionCreatorFactory loadConnectionCreatorFactory (
251
- String connectionCreatorFactoryClassName , ServiceRegistryImplementor serviceRegistry ) {
246
+ String connectionCreatorFactoryClassName , ServiceRegistry serviceRegistry ) {
252
247
if ( connectionCreatorFactoryClassName == null ) {
253
248
ConnectionInfoLogger .INSTANCE .debug ( "No connection creator factory class specified" );
254
249
return null ;
@@ -261,17 +256,17 @@ else if ( serviceRegistry != null ) {
261
256
return factoryClass .newInstance ();
262
257
}
263
258
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 );
265
261
}
266
262
}
267
263
else {
268
264
try {
269
265
return (ConnectionCreatorFactory ) Class .forName ( connectionCreatorFactoryClassName ).newInstance ();
270
266
}
271
267
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 );
275
270
}
276
271
}
277
272
}
0 commit comments