9
9
import java .beans .BeanInfo ;
10
10
import java .beans .PropertyDescriptor ;
11
11
import java .lang .reflect .Method ;
12
+ import java .sql .Connection ;
12
13
import java .util .HashMap ;
13
14
import java .util .HashSet ;
14
15
import java .util .Map ;
15
16
import java .util .Properties ;
16
17
import java .util .Set ;
18
+ import java .util .concurrent .ConcurrentHashMap ;
17
19
18
20
import org .hibernate .HibernateException ;
19
21
import org .hibernate .MultiTenancyStrategy ;
20
22
import org .hibernate .boot .registry .StandardServiceInitiator ;
21
23
import org .hibernate .boot .registry .selector .spi .StrategySelector ;
22
24
import org .hibernate .cfg .AvailableSettings ;
23
- import org .hibernate .cfg .Environment ;
24
25
import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
25
26
import org .hibernate .internal .CoreMessageLogger ;
26
27
import org .hibernate .internal .log .DeprecationLogger ;
28
+ import org .hibernate .internal .util .StringHelper ;
27
29
import org .hibernate .internal .util .beans .BeanInfoHelper ;
28
30
import org .hibernate .service .spi .ServiceRegistryImplementor ;
29
31
@@ -108,7 +110,7 @@ public ConnectionProvider initiateService(Map configurationValues, ServiceRegist
108
110
if ( providerName != null ) {
109
111
connectionProvider = instantiateExplicitConnectionProvider ( providerName , strategySelector );
110
112
}
111
- else if ( configurationValues .get ( Environment .DATASOURCE ) != null ) {
113
+ else if ( configurationValues .get ( AvailableSettings .DATASOURCE ) != null ) {
112
114
connectionProvider = new DatasourceConnectionProviderImpl ();
113
115
}
114
116
@@ -131,7 +133,7 @@ else if ( configurationValues.get( Environment.DATASOURCE ) != null ) {
131
133
}
132
134
133
135
if ( connectionProvider == null ) {
134
- if ( configurationValues .get ( Environment .URL ) != null ) {
136
+ if ( configurationValues .get ( AvailableSettings .URL ) != null ) {
135
137
connectionProvider = new DriverManagerConnectionProviderImpl ();
136
138
}
137
139
}
@@ -169,7 +171,7 @@ public void processBeanInfo(BeanInfo beanInfo) throws Exception {
169
171
}
170
172
171
173
private String getConfiguredConnectionProviderName ( Map configurationValues ) {
172
- String providerName = (String ) configurationValues .get ( Environment .CONNECTION_PROVIDER );
174
+ String providerName = (String ) configurationValues .get ( AvailableSettings .CONNECTION_PROVIDER );
173
175
if ( LEGACY_CONNECTION_PROVIDER_MAPPING .containsKey ( providerName ) ) {
174
176
final String actualProviderName = LEGACY_CONNECTION_PROVIDER_MAPPING .get ( providerName );
175
177
DeprecationLogger .DEPRECATION_LOGGER .connectionProviderClassDeprecated ( providerName , actualProviderName );
@@ -274,15 +276,15 @@ public static Properties getConnectionProperties(Map<?,?> properties) {
274
276
}
275
277
final String key = (String ) entry .getKey ();
276
278
final String value = (String ) entry .getValue ();
277
- if ( key .startsWith ( Environment .CONNECTION_PREFIX ) ) {
279
+ if ( key .startsWith ( AvailableSettings .CONNECTION_PREFIX ) ) {
278
280
if ( SPECIAL_PROPERTIES .contains ( key ) ) {
279
- if ( Environment .USER .equals ( key ) ) {
281
+ if ( AvailableSettings .USER .equals ( key ) ) {
280
282
result .setProperty ( "user" , value );
281
283
}
282
284
}
283
285
else {
284
286
result .setProperty (
285
- key .substring ( Environment .CONNECTION_PREFIX .length () + 1 ),
287
+ key .substring ( AvailableSettings .CONNECTION_PREFIX .length () + 1 ),
286
288
value
287
289
);
288
290
}
@@ -296,15 +298,45 @@ else if ( CONDITIONAL_PROPERTIES.containsKey( key ) ) {
296
298
297
299
private static final Set <String > SPECIAL_PROPERTIES ;
298
300
301
+ private static final Map <String ,Integer > ISOLATION_VALUE_MAP ;
302
+ private static final Map <Integer , String > ISOLATION_VALUE_CONSTANT_NAME_MAP ;
303
+ private static final Map <Integer , String > ISOLATION_VALUE_NICE_NAME_MAP ;
304
+
299
305
static {
300
306
SPECIAL_PROPERTIES = new HashSet <String >();
301
- SPECIAL_PROPERTIES .add ( Environment .DATASOURCE );
302
- SPECIAL_PROPERTIES .add ( Environment .URL );
303
- SPECIAL_PROPERTIES .add ( Environment .CONNECTION_PROVIDER );
304
- SPECIAL_PROPERTIES .add ( Environment .POOL_SIZE );
305
- SPECIAL_PROPERTIES .add ( Environment .ISOLATION );
306
- SPECIAL_PROPERTIES .add ( Environment .DRIVER );
307
- SPECIAL_PROPERTIES .add ( Environment .USER );
307
+ SPECIAL_PROPERTIES .add ( AvailableSettings .DATASOURCE );
308
+ SPECIAL_PROPERTIES .add ( AvailableSettings .URL );
309
+ SPECIAL_PROPERTIES .add ( AvailableSettings .CONNECTION_PROVIDER );
310
+ SPECIAL_PROPERTIES .add ( AvailableSettings .POOL_SIZE );
311
+ SPECIAL_PROPERTIES .add ( AvailableSettings .ISOLATION );
312
+ SPECIAL_PROPERTIES .add ( AvailableSettings .DRIVER );
313
+ SPECIAL_PROPERTIES .add ( AvailableSettings .USER );
314
+
315
+ ISOLATION_VALUE_MAP = new ConcurrentHashMap <String , Integer >();
316
+ ISOLATION_VALUE_MAP .put ( "TRANSACTION_NONE" , Connection .TRANSACTION_NONE );
317
+ ISOLATION_VALUE_MAP .put ( "NONE" , Connection .TRANSACTION_NONE );
318
+ ISOLATION_VALUE_MAP .put ( "TRANSACTION_READ_UNCOMMITTED" , Connection .TRANSACTION_READ_UNCOMMITTED );
319
+ ISOLATION_VALUE_MAP .put ( "READ_UNCOMMITTED" , Connection .TRANSACTION_READ_UNCOMMITTED );
320
+ ISOLATION_VALUE_MAP .put ( "TRANSACTION_READ_COMMITTED" , Connection .TRANSACTION_READ_COMMITTED );
321
+ ISOLATION_VALUE_MAP .put ( "READ_COMMITTED" , Connection .TRANSACTION_READ_COMMITTED );
322
+ ISOLATION_VALUE_MAP .put ( "TRANSACTION_REPEATABLE_READ" , Connection .TRANSACTION_REPEATABLE_READ );
323
+ ISOLATION_VALUE_MAP .put ( "REPEATABLE_READ" , Connection .TRANSACTION_REPEATABLE_READ );
324
+ ISOLATION_VALUE_MAP .put ( "TRANSACTION_SERIALIZABLE" , Connection .TRANSACTION_SERIALIZABLE );
325
+ ISOLATION_VALUE_MAP .put ( "SERIALIZABLE" , Connection .TRANSACTION_SERIALIZABLE );
326
+
327
+ ISOLATION_VALUE_CONSTANT_NAME_MAP = new ConcurrentHashMap <Integer , String >();
328
+ ISOLATION_VALUE_CONSTANT_NAME_MAP .put ( Connection .TRANSACTION_NONE , "TRANSACTION_NONE" );
329
+ ISOLATION_VALUE_CONSTANT_NAME_MAP .put ( Connection .TRANSACTION_READ_UNCOMMITTED , "TRANSACTION_READ_UNCOMMITTED" );
330
+ ISOLATION_VALUE_CONSTANT_NAME_MAP .put ( Connection .TRANSACTION_READ_COMMITTED , "TRANSACTION_READ_COMMITTED" );
331
+ ISOLATION_VALUE_CONSTANT_NAME_MAP .put ( Connection .TRANSACTION_REPEATABLE_READ , "TRANSACTION_REPEATABLE_READ" );
332
+ ISOLATION_VALUE_CONSTANT_NAME_MAP .put ( Connection .TRANSACTION_SERIALIZABLE , "TRANSACTION_SERIALIZABLE" );
333
+
334
+ ISOLATION_VALUE_NICE_NAME_MAP = new ConcurrentHashMap <Integer , String >();
335
+ ISOLATION_VALUE_NICE_NAME_MAP .put ( Connection .TRANSACTION_NONE , "NONE" );
336
+ ISOLATION_VALUE_NICE_NAME_MAP .put ( Connection .TRANSACTION_READ_UNCOMMITTED , "READ_UNCOMMITTED" );
337
+ ISOLATION_VALUE_NICE_NAME_MAP .put ( Connection .TRANSACTION_READ_COMMITTED , "READ_COMMITTED" );
338
+ ISOLATION_VALUE_NICE_NAME_MAP .put ( Connection .TRANSACTION_REPEATABLE_READ , "REPEATABLE_READ" );
339
+ ISOLATION_VALUE_NICE_NAME_MAP .put ( Connection .TRANSACTION_SERIALIZABLE , "SERIALIZABLE" );
308
340
}
309
341
310
342
// Connection properties (map value) that automatically need set if the
@@ -315,6 +347,81 @@ else if ( CONDITIONAL_PROPERTIES.containsKey( key ) ) {
315
347
static {
316
348
CONDITIONAL_PROPERTIES = new HashMap <String , String >();
317
349
// Oracle requires that includeSynonyms=true in order for getColumns to work using a table synonym name.
318
- CONDITIONAL_PROPERTIES .put ( Environment .ENABLE_SYNONYMS , "includeSynonyms" );
350
+ CONDITIONAL_PROPERTIES .put ( AvailableSettings .ENABLE_SYNONYMS , "includeSynonyms" );
351
+ }
352
+
353
+ public static Integer extractIsolation (Map settings ) {
354
+ return interpretIsolation ( settings .get ( AvailableSettings .ISOLATION ) );
355
+ }
356
+
357
+ public static Integer interpretIsolation (Object setting ) {
358
+ if ( setting == null ) {
359
+ return null ;
360
+ }
361
+
362
+ if ( Number .class .isInstance ( setting ) ) {
363
+ return ( (Number ) setting ).intValue ();
364
+ }
365
+
366
+ final String settingAsString = setting .toString ();
367
+ if ( StringHelper .isEmpty ( settingAsString ) ) {
368
+ return null ;
369
+ }
370
+
371
+ if ( ISOLATION_VALUE_MAP .containsKey ( settingAsString ) ) {
372
+ return ISOLATION_VALUE_MAP .get ( settingAsString );
373
+ }
374
+
375
+ // it could be a String representation of the isolation numeric value...
376
+ try {
377
+ return Integer .valueOf ( settingAsString );
378
+ }
379
+ catch (NumberFormatException ignore ) {
380
+ }
381
+
382
+ throw new HibernateException ( "Could not interpret transaction isolation setting [" + setting + "]" );
383
+ }
384
+
385
+ /**
386
+ * Gets the {@link Connection} constant name corresponding to the given isolation.
387
+ *
388
+ * @param isolation The transaction isolation numeric value.
389
+ *
390
+ * @return The corresponding Connection constant name.
391
+ *
392
+ * @throws HibernateException If the given isolation does not map to JDBC standard isolation
393
+ *
394
+ * @see #toIsolationNiceName
395
+ */
396
+ public static String toIsolationConnectionConstantName (Integer isolation ) {
397
+ final String name = ISOLATION_VALUE_CONSTANT_NAME_MAP .get ( isolation );
398
+ if ( name == null ) {
399
+ throw new HibernateException (
400
+ "Could not convert isolation value [" + isolation + "] to java.sql.Connection constant name"
401
+ );
402
+ }
403
+ return name ;
404
+ }
405
+
406
+ /**
407
+ * Get the name of a JDBC transaction isolation level
408
+ *
409
+ * @param isolation The transaction isolation numeric value.
410
+ *
411
+ * @return a nice human-readable name
412
+ *
413
+ * @see #toIsolationConnectionConstantName
414
+ */
415
+ public static String toIsolationNiceName (Integer isolation ) {
416
+ String name = null ;
417
+
418
+ if ( isolation != null ) {
419
+ name = ISOLATION_VALUE_NICE_NAME_MAP .get ( isolation );
420
+ }
421
+
422
+ if ( name == null ) {
423
+ name = "<unknown>" ;
424
+ }
425
+ return name ;
319
426
}
320
427
}
0 commit comments