Debian Codegraph @

src/main/org/h2/engine/SysProperties.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import org.h2.util.MathUtils;
9 import org.h2.util.Utils;
10
11 /**
12 * The constants defined in this class are initialized from system properties.
13 * Some system properties are per machine settings, and others are as a last
14 * resort and temporary solution to work around a problem in the application or
15 * database engine. Also, there are system properties to enable features that
16 * are not yet fully tested or that are not backward compatible.
17 * <p>
18 * System properties can be set when starting the virtual machine:
19 * </p>
20 *
21 * <pre>
22 * java -Dh2.baseDir=/temp
23 * </pre>
24 *
25 * They can be set within the application, but this must be done before loading
26 * any classes of this database (before loading the JDBC driver):
27 *
28 * <pre>
29 * System.setProperty(&quot;h2.baseDir&quot;, &quot;/temp&quot;);
30 * </pre>
31 */
32 public class SysProperties {
33
34 /**
35 * INTERNAL
36 */
37 public static final String H2_SCRIPT_DIRECTORY = "h2.scriptDirectory";
38
39 /**
40 * INTERNAL
41 */
42 public static final String H2_BROWSER = "h2.browser";
43
44 /**
45 * System property <code>user.home</code> (empty string if not set).
46 * It is usually set by the system, and used as a replacement for ~ in file
47 * names.
48 */
49 public static final String USER_HOME =
50 Utils.getProperty("user.home", "");
51
52 /**
53 * System property <code>h2.allowedClasses</code> (default: *).
54 * Comma separated list of class names or prefixes.
55 */
56 public static final String ALLOWED_CLASSES =
57 Utils.getProperty("h2.allowedClasses", "*");
58
59 /**
60 * System property <code>h2.enableAnonymousTLS</code> (default: true).
61 * When using TLS connection, the anonymous cipher suites should be enabled.
62 */
63 public static final boolean ENABLE_ANONYMOUS_TLS =
64 Utils.getProperty("h2.enableAnonymousTLS", true);
65
66 /**
67 * System property <code>h2.bindAddress</code> (default: null).
68 * The bind address to use.
69 */
70 public static final String BIND_ADDRESS =
71 Utils.getProperty("h2.bindAddress", null);
72
73 /**
74 * System property <code>h2.check</code>
75 * (default: true for JDK/JRE, false for Android).
76 * Optional additional checks in the database engine.
77 */
78 public static final boolean CHECK =
79 Utils.getProperty("h2.check", !"0.9".equals(Utils.getProperty("java.specification.version", null)));
80
81 /**
82 * System property <code>h2.clientTraceDirectory</code> (default:
83 * trace.db/).
84 * Directory where the trace files of the JDBC client are stored (only for
85 * client / server).
86 */
87 public static final String CLIENT_TRACE_DIRECTORY =
88 Utils.getProperty("h2.clientTraceDirectory", "trace.db/");
89
90 /**
91 * System property <code>h2.collatorCacheSize</code> (default: 3
92 * 2000).
93 * The cache size for collation keys (in elements). Used when a collator has
94 * been set for the database.
95 */
96 public static final int COLLATOR_CACHE_SIZE =
97 Utils.getProperty("h2.collatorCacheSize", 32_000);
98
99 /**
100 * System property <code>h2.consoleTableIndexes</code>
101 * (default: 100).
102 * Up to this many tables, the column type and indexes are listed.
103 */
104 public static final int CONSOLE_MAX_TABLES_LIST_INDEXES =
105 Utils.getProperty("h2.consoleTableIndexes", 100);
106
107 /**
108 * System property <code>h2.consoleTableColumns</code>
109 * (default: 500).
110 * Up to this many tables, the column names are listed.
111 */
112 public static final int CONSOLE_MAX_TABLES_LIST_COLUMNS =
113 Utils.getProperty("h2.consoleTableColumns", 500);
114
115 /**
116 * System property <code>h2.consoleProcedureColumns</code>
117 * (default: 500).
118 * Up to this many procedures, the column names are listed.
119 */
120 public static final int CONSOLE_MAX_PROCEDURES_LIST_COLUMNS =
121 Utils.getProperty("h2.consoleProcedureColumns", 300);
122
123 /**
124 * System property <code>h2.consoleStream</code> (default: true).
125 * H2 Console: stream query results.
126 */
127 public static final boolean CONSOLE_STREAM =
128 Utils.getProperty("h2.consoleStream", true);
129
130 /**
131 * System property <code>h2.consoleTimeout</code> (default: 1800000).
132 * H2 Console: session timeout in milliseconds. The default is 30 minutes.
133 */
134 public static final int CONSOLE_TIMEOUT =
135 Utils.getProperty("h2.consoleTimeout", 30 * 60 * 1000);
136
137 /**
138 * System property <code>h2.dataSourceTraceLevel</code> (default: 1).
139 * The trace level of the data source implementation. Default is 1 for
140 * error.
141 */
142 public static final int DATASOURCE_TRACE_LEVEL =
143 Utils.getProperty("h2.dataSourceTraceLevel", 1);
144
145 /**
146 * System property <code>h2.delayWrongPasswordMin</code>
147 * (default: 250).
148 * The minimum delay in milliseconds before an exception is thrown for using
149 * the wrong user name or password. This slows down brute force attacks. The
150 * delay is reset to this value after a successful login. Unsuccessful
151 * logins will double the time until DELAY_WRONG_PASSWORD_MAX.
152 * To disable the delay, set this system property to 0.
153 */
154 public static final int DELAY_WRONG_PASSWORD_MIN =
155 Utils.getProperty("h2.delayWrongPasswordMin", 250);
156
157 /**
158 * System property <code>h2.delayWrongPasswordMax</code>
159 * (default: 4000).
160 * The maximum delay in milliseconds before an exception is thrown for using
161 * the wrong user name or password. This slows down brute force attacks. The
162 * delay is reset after a successful login. The value 0 means there is no
163 * maximum delay.
164 */
165 public static final int DELAY_WRONG_PASSWORD_MAX =
166 Utils.getProperty("h2.delayWrongPasswordMax", 4000);
167
168 /**
169 * System property <code>h2.javaSystemCompiler</code> (default: true).
170 * Whether to use the Java system compiler
171 * (ToolProvider.getSystemJavaCompiler()) if it is available to compile user
172 * defined functions. If disabled or if the system compiler is not
173 * available, the com.sun.tools.javac compiler is used if available, and
174 * "javac" (as an external process) is used if not.
175 */
176 public static final boolean JAVA_SYSTEM_COMPILER =
177 Utils.getProperty("h2.javaSystemCompiler", true);
178
179 /**
180 * System property <code>h2.lobCloseBetweenReads</code>
181 * (default: false).
182 * Close LOB files between read operations.
183 */
184 public static boolean lobCloseBetweenReads =
185 Utils.getProperty("h2.lobCloseBetweenReads", false);
186
187 /**
188 * System property <code>h2.lobClientMaxSizeMemory</code> (default:
189 * 1048576).
190 * The maximum size of a LOB object to keep in memory on the client side
191 * when using the server mode.
192 */
193 public static final int LOB_CLIENT_MAX_SIZE_MEMORY =
194 Utils.getProperty("h2.lobClientMaxSizeMemory", 1024 * 1024);
195
196 /**
197 * System property <code>h2.maxFileRetry</code> (default: 16).
198 * Number of times to retry file delete and rename. in Windows, files can't
199 * be deleted if they are open. Waiting a bit can help (sometimes the
200 * Windows Explorer opens the files for a short time) may help. Sometimes,
201 * running garbage collection may close files if the user forgot to call
202 * Connection.close() or InputStream.close().
203 */
204 public static final int MAX_FILE_RETRY =
205 Math.max(1, Utils.getProperty("h2.maxFileRetry", 16));
206
207 /**
208 * System property <code>h2.maxReconnect</code> (default: 3).
209 * The maximum number of tries to reconnect in a row.
210 */
211 public static final int MAX_RECONNECT =
212 Utils.getProperty("h2.maxReconnect", 3);
213
214 /**
215 * System property <code>h2.maxMemoryRows</code>
216 * (default: 40000 per GB of available RAM).
217 * The default maximum number of rows to be kept in memory in a result set.
218 */
219 public static final int MAX_MEMORY_ROWS =
220 getAutoScaledForMemoryProperty("h2.maxMemoryRows", 40_000);
221
222 /**
223 * System property <code>h2.maxTraceDataLength</code>
224 * (default: 65535).
225 * The maximum size of a LOB value that is written as data to the trace
226 * system.
227 */
228 public static final long MAX_TRACE_DATA_LENGTH =
229 Utils.getProperty("h2.maxTraceDataLength", 65535);
230
231 /**
232 * System property <code>h2.nioLoadMapped</code> (default: false).
233 * If the mapped buffer should be loaded when the file is opened.
234 * This can improve performance.
235 */
236 public static final boolean NIO_LOAD_MAPPED =
237 Utils.getProperty("h2.nioLoadMapped", false);
238
239 /**
240 * System property <code>h2.nioCleanerHack</code> (default: false).
241 * If enabled, use the reflection hack to un-map the mapped file if
242 * possible. If disabled, System.gc() is called in a loop until the object
243 * is garbage collected. See also
244 * https://bugs.openjdk.java.net/browse/JDK-4724038
245 */
246 public static final boolean NIO_CLEANER_HACK =
247 Utils.getProperty("h2.nioCleanerHack", false);
248
249 /**
250 * System property <code>h2.objectCache</code> (default: true).
251 * Cache commonly used values (numbers, strings). There is a shared cache
252 * for all values.
253 */
254 public static final boolean OBJECT_CACHE =
255 Utils.getProperty("h2.objectCache", true);
256
257 /**
258 * System property <code>h2.objectCacheMaxPerElementSize</code> (default:
259 * 4096).
260 * The maximum size (precision) of an object in the cache.
261 */
262 public static final int OBJECT_CACHE_MAX_PER_ELEMENT_SIZE =
263 Utils.getProperty("h2.objectCacheMaxPerElementSize", 4096);
264
265 /**
266 * System property <code>h2.objectCacheSize</code> (default: 1024).
267 * The maximum number of objects in the cache.
268 * This value must be a power of 2.
269 */
270 public static final int OBJECT_CACHE_SIZE;
271 static {
272 try {
273 OBJECT_CACHE_SIZE = MathUtils.nextPowerOf2(
274 Utils.getProperty("h2.objectCacheSize", 1024));
275 } catch (IllegalArgumentException e) {
276 throw new IllegalStateException("Invalid h2.objectCacheSize", e);
277 }
278 }
279
280 /**
281 * System property <code>h2.pgClientEncoding</code> (default: UTF-8).
282 * Default client encoding for PG server. It is used if the client does not
283 * sends his encoding.
284 */
285 public static final String PG_DEFAULT_CLIENT_ENCODING =
286 Utils.getProperty("h2.pgClientEncoding", "UTF-8");
287
288 /**
289 * System property <code>h2.prefixTempFile</code> (default: h2.temp).
290 * The prefix for temporary files in the temp directory.
291 */
292 public static final String PREFIX_TEMP_FILE =
293 Utils.getProperty("h2.prefixTempFile", "h2.temp");
294
295 /**
296 * System property <code>h2.forceAutoCommitOffOnCommit</code> (default: false).
297 * Throw error if transaction's auto-commit property is true when a commit is executed.
298 */
299 public static boolean FORCE_AUTOCOMMIT_OFF_ON_COMMIT =
300 Utils.getProperty("h2.forceAutoCommitOffOnCommit", false);
301
302 /**
303 * System property <code>h2.serverCachedObjects</code> (default: 64).
304 * TCP Server: number of cached objects per session.
305 */
306 public static final int SERVER_CACHED_OBJECTS =
307 Utils.getProperty("h2.serverCachedObjects", 64);
308
309 /**
310 * System property <code>h2.serverResultSetFetchSize</code>
311 * (default: 100).
312 * The default result set fetch size when using the server mode.
313 */
314 public static final int SERVER_RESULT_SET_FETCH_SIZE =
315 Utils.getProperty("h2.serverResultSetFetchSize", 100);
316
317 /**
318 * System property <code>h2.socketConnectRetry</code> (default: 16).
319 * The number of times to retry opening a socket. Windows sometimes fails
320 * to open a socket, see bug
321 * https://bugs.openjdk.java.net/browse/JDK-6213296
322 */
323 public static final int SOCKET_CONNECT_RETRY =
324 Utils.getProperty("h2.socketConnectRetry", 16);
325
326 /**
327 * System property <code>h2.socketConnectTimeout</code>
328 * (default: 2000).
329 * The timeout in milliseconds to connect to a server.
330 */
331 public static final int SOCKET_CONNECT_TIMEOUT =
332 Utils.getProperty("h2.socketConnectTimeout", 2000);
333
334 /**
335 * System property <code>h2.splitFileSizeShift</code> (default: 30).
336 * The maximum file size of a split file is 1L &lt;&lt; x.
337 */
338 public static final long SPLIT_FILE_SIZE_SHIFT =
339 Utils.getProperty("h2.splitFileSizeShift", 30);
340
341 /**
342 * System property <code>h2.traceIO</code> (default: false).
343 * Trace all I/O operations.
344 */
345 public static final boolean TRACE_IO =
346 Utils.getProperty("h2.traceIO", false);
347
348 /**
349 * System property <code>h2.threadDeadlockDetector</code>
350 * (default: false).
351 * Detect thread deadlocks in a background thread.
352 */
353 public static final boolean THREAD_DEADLOCK_DETECTOR =
354 Utils.getProperty("h2.threadDeadlockDetector", false);
355
356 /**
357 * System property <code>h2.urlMap</code> (default: null).
358 * A properties file that contains a mapping between database URLs. New
359 * connections are written into the file. An empty value in the map means no
360 * redirection is used for the given URL.
361 */
362 public static final String URL_MAP =
363 Utils.getProperty("h2.urlMap", null);
364
365 /**
366 * System property <code>h2.useThreadContextClassLoader</code>
367 * (default: false).
368 * Instead of using the default class loader when deserializing objects, the
369 * current thread-context class loader will be used.
370 */
371 public static final boolean USE_THREAD_CONTEXT_CLASS_LOADER =
372 Utils.getProperty("h2.useThreadContextClassLoader", false);
373
374 /**
375 * System property <code>h2.javaObjectSerializer</code>
376 * (default: null).
377 * The JavaObjectSerializer class name for java objects being stored in
378 * column of type OTHER. It must be the same on client and server to work
379 * correctly.
380 */
381 public static final String JAVA_OBJECT_SERIALIZER =
382 Utils.getProperty("h2.javaObjectSerializer", null);
383
384 /**
385 * System property <code>h2.authConfigFile</code>
386 * (default: null).
387 * authConfigFile define the URL of configuration file
388 * of {@link org.h2.security.auth.DefaultAuthenticator}
389 *
390 */
391 public static final String AUTH_CONFIG_FILE =
392 Utils.getProperty("h2.authConfigFile", null);
393
394 private static final String H2_BASE_DIR = "h2.baseDir";
395
396 private SysProperties() {
397 // utility class
398 }
399
400 /**
401 * INTERNAL
402 * @param dir base directory
403 */
404 public static void setBaseDir(String dir) {
405 if (!dir.endsWith("/")) {
406 dir += "/";
407 }
408 System.setProperty(H2_BASE_DIR, dir);
409 }
410
411 /**
412 * INTERNAL
413 * @return base directory
414 */
415 public static String getBaseDir() {
416 return Utils.getProperty(H2_BASE_DIR, null);
417 }
418
419 /**
420 * System property <code>h2.scriptDirectory</code> (default: empty
421 * string).
422 * Relative or absolute directory where the script files are stored to or
423 * read from.
424 *
425 * @return the current value
426 */
427 public static String getScriptDirectory() {
428 return Utils.getProperty(H2_SCRIPT_DIRECTORY, "");
429 }
430
431 /**
432 * This method attempts to auto-scale some of our properties to take
433 * advantage of more powerful machines out of the box. We assume that our
434 * default properties are set correctly for approx. 1G of memory, and scale
435 * them up if we have more.
436 */
437 private static int getAutoScaledForMemoryProperty(String key, int defaultValue) {
438 String s = Utils.getProperty(key, null);
439 if (s != null) {
440 try {
441 return Integer.decode(s);
442 } catch (NumberFormatException e) {
443 // ignore
444 }
445 }
446 return Utils.scaleForAvailableMemory(defaultValue);
447 }
448
449 }