-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Environment.java
355 lines (323 loc) · 12.4 KB
/
Environment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.cfg;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Version;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ConfigHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.jboss.logging.Logger;
/**
* Provides access to configuration info passed in <tt>Properties</tt> objects.
* <br><br>
* Hibernate has two property scopes:
* <ul>
* <li><b>Factory-level</b> properties may be passed to the <tt>SessionFactory</tt> when it
* instantiated. Each instance might have different property values. If no
* properties are specified, the factory calls <tt>Environment.getProperties()</tt>.
* <li><b>System-level</b> properties are shared by all factory instances and are always
* determined by the <tt>Environment</tt> properties.
* </ul>
* The only system-level properties are
* <ul>
* <li><tt>hibernate.jdbc.use_streams_for_binary</tt>
* <li><tt>hibernate.cglib.use_reflection_optimizer</tt>
* </ul>
* <tt>Environment</tt> properties are populated by calling <tt>System.getProperties()</tt>
* and then from a resource named <tt>/hibernate.properties</tt> if it exists. System
* properties override properties specified in <tt>hibernate.properties</tt>.<br>
* <br>
* The <tt>SessionFactory</tt> is controlled by the following properties.
* Properties may be either be <tt>System</tt> properties, properties
* defined in a resource named <tt>/hibernate.properties</tt> or an instance of
* <tt>java.util.Properties</tt> passed to
* <tt>Configuration.build()</tt><br>
* <br>
* <table>
* <tr><td><b>property</b></td><td><b>meaning</b></td></tr>
* <tr>
* <td><tt>hibernate.dialect</tt></td>
* <td>classname of <tt>org.hibernate.dialect.Dialect</tt> subclass</td>
* </tr>
* <tr>
* <td><tt>hibernate.connection.provider_class</tt></td>
* <td>classname of <tt>ConnectionProvider</tt>
* subclass (if not specified hueristics are used)</td>
* </tr>
* <tr><td><tt>hibernate.connection.username</tt></td><td>database username</td></tr>
* <tr><td><tt>hibernate.connection.password</tt></td><td>database password</td></tr>
* <tr>
* <td><tt>hibernate.connection.url</tt></td>
* <td>JDBC URL (when using <tt>java.sql.DriverManager</tt>)</td>
* </tr>
* <tr>
* <td><tt>hibernate.connection.driver_class</tt></td>
* <td>classname of JDBC driver</td>
* </tr>
* <tr>
* <td><tt>hibernate.connection.isolation</tt></td>
* <td>JDBC transaction isolation level (only when using
* <tt>java.sql.DriverManager</tt>)
* </td>
* </tr>
* <td><tt>hibernate.connection.pool_size</tt></td>
* <td>the maximum size of the connection pool (only when using
* <tt>java.sql.DriverManager</tt>)
* </td>
* </tr>
* <tr>
* <td><tt>hibernate.connection.datasource</tt></td>
* <td>databasource JNDI name (when using <tt>javax.sql.Datasource</tt>)</td>
* </tr>
* <tr>
* <td><tt>hibernate.jndi.url</tt></td><td>JNDI <tt>InitialContext</tt> URL</td>
* </tr>
* <tr>
* <td><tt>hibernate.jndi.class</tt></td><td>JNDI <tt>InitialContext</tt> classname</td>
* </tr>
* <tr>
* <td><tt>hibernate.max_fetch_depth</tt></td>
* <td>maximum depth of outer join fetching</td>
* </tr>
* <tr>
* <td><tt>hibernate.jdbc.batch_size</tt></td>
* <td>enable use of JDBC2 batch API for drivers which support it</td>
* </tr>
* <tr>
* <td><tt>hibernate.jdbc.fetch_size</tt></td>
* <td>set the JDBC fetch size</td>
* </tr>
* <tr>
* <td><tt>hibernate.jdbc.use_scrollable_resultset</tt></td>
* <td>enable use of JDBC2 scrollable resultsets (you only need this specify
* this property when using user supplied connections)</td>
* </tr>
* <tr>
* <td><tt>hibernate.jdbc.use_getGeneratedKeys</tt></td>
* <td>enable use of JDBC3 PreparedStatement.getGeneratedKeys() to retrieve
* natively generated keys after insert. Requires JDBC3+ driver and JRE1.4+</td>
* </tr>
* <tr>
* <td><tt>hibernate.hbm2ddl.auto</tt></td>
* <td>enable auto DDL export</td>
* </tr>
* <tr>
* <td><tt>hibernate.default_schema</tt></td>
* <td>use given schema name for unqualified tables (always optional)</td>
* </tr>
* <tr>
* <td><tt>hibernate.default_catalog</tt></td>
* <td>use given catalog name for unqualified tables (always optional)</td>
* </tr>
* <tr>
* <td><tt>hibernate.session_factory_name</tt></td>
* <td>If set, the factory attempts to bind this name to itself in the
* JNDI context. This name is also used to support cross JVM <tt>
* Session</tt> (de)serialization.</td>
* </tr>
* <tr>
* <td><tt>hibernate.transaction.jta.platform</tt></td>
* <td>classname of <tt>org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform</tt>
* implementor</td>
* </tr>
* <tr>
* <td><tt>hibernate.transaction.factory_class</tt></td>
* <td>the factory to use for instantiating <tt>Transaction</tt>s.
* (Defaults to <tt>JdbcTransactionFactory</tt>.)</td>
* </tr>
* <tr>
* <td><tt>hibernate.query.substitutions</tt></td><td>query language token substitutions</td>
* </tr>
* </table>
*
* @see org.hibernate.SessionFactory
* @author Gavin King
*/
public final class Environment implements AvailableSettings {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, Environment.class.getName());
private static final BytecodeProvider BYTECODE_PROVIDER_INSTANCE;
private static final boolean ENABLE_BINARY_STREAMS;
private static final boolean ENABLE_REFLECTION_OPTIMIZER;
private static final Properties GLOBAL_PROPERTIES;
private static final Map OBSOLETE_PROPERTIES = new HashMap();
private static final Map RENAMED_PROPERTIES = new HashMap();
/**
* Issues warnings to the user when any obsolete or renamed property names are used.
*
* @param configurationValues The specified properties.
*/
public static void verifyProperties(Map<?,?> configurationValues) {
final Map propertiesToAdd = new HashMap();
for ( Map.Entry entry : configurationValues.entrySet() ) {
final Object replacementKey = OBSOLETE_PROPERTIES.get( entry.getKey() );
if ( replacementKey != null ) {
LOG.unsupportedProperty( entry.getKey(), replacementKey );
}
final Object renamedKey = RENAMED_PROPERTIES.get( entry.getKey() );
if ( renamedKey != null ) {
LOG.renamedProperty( entry.getKey(), renamedKey );
propertiesToAdd.put( renamedKey, entry.getValue() );
}
}
configurationValues.putAll( propertiesToAdd );
}
static {
Version.logVersion();
GLOBAL_PROPERTIES = new Properties();
//Set USE_REFLECTION_OPTIMIZER to false to fix HHH-227
GLOBAL_PROPERTIES.setProperty( USE_REFLECTION_OPTIMIZER, Boolean.FALSE.toString() );
try {
InputStream stream = ConfigHelper.getResourceAsStream( "/hibernate.properties" );
try {
GLOBAL_PROPERTIES.load(stream);
LOG.propertiesLoaded( ConfigurationHelper.maskOut( GLOBAL_PROPERTIES, PASS ) );
}
catch (Exception e) {
LOG.unableToLoadProperties();
}
finally {
try{
stream.close();
}
catch (IOException ioe){
LOG.unableToCloseStreamError( ioe );
}
}
}
catch (HibernateException he) {
LOG.propertiesNotFound();
}
try {
Properties systemProperties = System.getProperties();
// Must be thread-safe in case an application changes System properties during Hibernate initialization.
// See HHH-8383.
synchronized (systemProperties) {
GLOBAL_PROPERTIES.putAll(systemProperties);
}
}
catch (SecurityException se) {
LOG.unableToCopySystemProperties();
}
verifyProperties(GLOBAL_PROPERTIES);
ENABLE_BINARY_STREAMS = ConfigurationHelper.getBoolean(USE_STREAMS_FOR_BINARY, GLOBAL_PROPERTIES);
if ( ENABLE_BINARY_STREAMS ) {
LOG.usingStreams();
}
ENABLE_REFLECTION_OPTIMIZER = ConfigurationHelper.getBoolean(USE_REFLECTION_OPTIMIZER, GLOBAL_PROPERTIES);
if ( ENABLE_REFLECTION_OPTIMIZER ) {
LOG.usingReflectionOptimizer();
}
BYTECODE_PROVIDER_INSTANCE = buildBytecodeProvider( GLOBAL_PROPERTIES );
}
/**
* This will be removed soon; currently just returns false as no known JVM exibits this bug
* and is also able to run this version of Hibernate ORM.
* @deprecated removed as unneccessary
* @return false
*/
@Deprecated
public static boolean jvmHasTimestampBug() {
return false;
}
/**
* Should we use streams to bind binary types to JDBC IN parameters?
*
* @return True if streams should be used for binary data handling; false otherwise.
*
* @see #USE_STREAMS_FOR_BINARY
*
* @deprecated Deprecated to indicate that the method will be moved to
* {@link org.hibernate.boot.spi.SessionFactoryOptions} /
* {@link org.hibernate.boot.SessionFactoryBuilder} - probably in 6.0.
* See <a href="https://hibernate.atlassian.net/browse/HHH-12194">HHH-12194</a> and
* <a href="https://hibernate.atlassian.net/browse/HHH-12193">HHH-12193</a> for details
*/
@Deprecated
public static boolean useStreamsForBinary() {
return ENABLE_BINARY_STREAMS;
}
/**
* Should we use reflection optimization?
*
* @return True if reflection optimization should be used; false otherwise.
*
* @see #USE_REFLECTION_OPTIMIZER
* @see #getBytecodeProvider()
* @see BytecodeProvider#getReflectionOptimizer
*
* @deprecated Deprecated to indicate that the method will be moved to
* {@link org.hibernate.boot.spi.SessionFactoryOptions} /
* {@link org.hibernate.boot.SessionFactoryBuilder} - probably in 6.0.
* See <a href="https://hibernate.atlassian.net/browse/HHH-12194">HHH-12194</a> and
* <a href="https://hibernate.atlassian.net/browse/HHH-12193">HHH-12193</a> for details
*/
@Deprecated
public static boolean useReflectionOptimizer() {
return ENABLE_REFLECTION_OPTIMIZER;
}
/**
* @deprecated Deprecated to indicate that the method will be moved to
* {@link org.hibernate.boot.spi.SessionFactoryOptions} /
* {@link org.hibernate.boot.SessionFactoryBuilder} - probably in 6.0.
* See <a href="https://hibernate.atlassian.net/browse/HHH-12194">HHH-12194</a> and
* <a href="https://hibernate.atlassian.net/browse/HHH-12193">HHH-12193</a> for details
*/
@Deprecated
public static BytecodeProvider getBytecodeProvider() {
return BYTECODE_PROVIDER_INSTANCE;
}
/**
* Disallow instantiation
*/
private Environment() {
throw new UnsupportedOperationException();
}
/**
* Return <tt>System</tt> properties, extended by any properties specified
* in <tt>hibernate.properties</tt>.
* @return Properties
*/
public static Properties getProperties() {
Properties copy = new Properties();
copy.putAll(GLOBAL_PROPERTIES);
return copy;
}
/**
* @deprecated Use {@link ConnectionProviderInitiator#toIsolationNiceName} instead
*/
@Deprecated
public static String isolationLevelToString(int isolation) {
return ConnectionProviderInitiator.toIsolationNiceName( isolation );
}
public static final String BYTECODE_PROVIDER_NAME_JAVASSIST = "javassist";
public static final String BYTECODE_PROVIDER_NAME_BYTEBUDDY = "bytebuddy";
public static final String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_BYTEBUDDY;
public static BytecodeProvider buildBytecodeProvider(Properties properties) {
String provider = ConfigurationHelper.getString( BYTECODE_PROVIDER, properties, BYTECODE_PROVIDER_NAME_DEFAULT );
return buildBytecodeProvider( provider );
}
private static BytecodeProvider buildBytecodeProvider(String providerName) {
if ( BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( providerName ) ) {
return new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl();
}
if ( BYTECODE_PROVIDER_NAME_JAVASSIST.equals( providerName ) ) {
return new org.hibernate.bytecode.internal.javassist.BytecodeProviderImpl();
}
LOG.bytecodeProvider( providerName );
// todo : allow a custom class name - just check if the config is a FQN
// currently we assume it is only ever the Strings "javassist" or "bytebuddy"...
LOG.unknownBytecodeProvider( providerName, BYTECODE_PROVIDER_NAME_DEFAULT );
return new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl();
}
}