|
6 | 6 | */
|
7 | 7 | package org.hibernate.engine.jdbc.connections.internal;
|
8 | 8 |
|
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.hibernate.cfg.JdbcSettings; |
9 | 12 | import org.hibernate.dialect.DatabaseVersion;
|
| 13 | +import org.hibernate.dialect.Dialect; |
10 | 14 | import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
| 15 | +import org.hibernate.internal.util.NullnessHelper; |
11 | 16 | import org.hibernate.internal.util.StringHelper;
|
| 17 | +import org.hibernate.internal.util.config.ConfigurationHelper; |
12 | 18 |
|
13 | 19 | import static org.hibernate.dialect.SimpleDatabaseVersion.ZERO_VERSION;
|
14 |
| -import static org.hibernate.dialect.SimpleDatabaseVersion.NO_VERSION; |
| 20 | +import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.interpretIsolation; |
| 21 | +import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName; |
15 | 22 |
|
16 | 23 | /**
|
| 24 | + * Standard implementation of DatabaseConnectionInfo |
| 25 | + * |
17 | 26 | * @author Jan Schatteman
|
18 | 27 | */
|
19 | 28 | public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
|
20 |
| - |
21 |
| - // Means either the value was not explicitly set, or simply not offered by the connection provider |
22 | 29 | public static final String DEFAULT = "undefined/unknown";
|
23 | 30 |
|
24 |
| - protected String dbUrl = DEFAULT; |
25 |
| - protected String dbDriverName = DEFAULT; |
26 |
| - protected DatabaseVersion dbVersion = ZERO_VERSION; |
27 |
| - protected String dbAutoCommitMode = DEFAULT; |
28 |
| - protected String dbIsolationLevel = DEFAULT; |
29 |
| - protected String dbMinPoolSize = DEFAULT; |
30 |
| - protected String dbMaxPoolSize = DEFAULT; |
| 31 | + protected final String jdbcUrl; |
| 32 | + protected final String jdbcDriver; |
| 33 | + protected final DatabaseVersion dialectVersion; |
| 34 | + protected final String autoCommitMode; |
| 35 | + protected final String isolationLevel; |
| 36 | + protected final Integer poolMinSize; |
| 37 | + protected final Integer poolMaxSize; |
| 38 | + |
| 39 | + public DatabaseConnectionInfoImpl( |
| 40 | + String jdbcUrl, |
| 41 | + String jdbcDriver, |
| 42 | + DatabaseVersion dialectVersion, |
| 43 | + String autoCommitMode, |
| 44 | + String isolationLevel, |
| 45 | + Integer poolMinSize, |
| 46 | + Integer poolMaxSize) { |
| 47 | + this.jdbcUrl = jdbcUrl; |
| 48 | + this.jdbcDriver = jdbcDriver; |
| 49 | + this.dialectVersion = dialectVersion; |
| 50 | + this.autoCommitMode = autoCommitMode; |
| 51 | + this.isolationLevel = isolationLevel; |
| 52 | + this.poolMinSize = poolMinSize; |
| 53 | + this.poolMaxSize = poolMaxSize; |
| 54 | + } |
31 | 55 |
|
32 |
| - public DatabaseConnectionInfoImpl() { |
| 56 | + public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect) { |
| 57 | + this( |
| 58 | + determineUrl( settings ), |
| 59 | + determineDriver( settings ), |
| 60 | + dialect.getVersion(), |
| 61 | + determineAutoCommitMode( settings ), |
| 62 | + determineIsolationLevel( settings ), |
| 63 | + // No setting for min. pool size |
| 64 | + null, |
| 65 | + determinePoolMaxSize( settings ) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public DatabaseConnectionInfoImpl(Dialect dialect) { |
| 70 | + this( null, null, dialect.getVersion(), null, null, null, null ); |
33 | 71 | }
|
34 | 72 |
|
35 | 73 | @Override
|
36 |
| - public DatabaseConnectionInfo setDBUrl(String dbUrl) { |
37 |
| - this.dbUrl = dbUrl; |
38 |
| - return this; |
| 74 | + public String getJdbcUrl() { |
| 75 | + return jdbcUrl; |
39 | 76 | }
|
40 | 77 |
|
41 | 78 | @Override
|
42 |
| - public DatabaseConnectionInfo setDBDriverName(String dbDriverName) { |
43 |
| - if ( checkValidString(dbDriverName) && isDefaultStringValue(this.dbDriverName) ) { |
44 |
| - this.dbDriverName = dbDriverName; |
45 |
| - } |
46 |
| - return this; |
| 79 | + public String getJdbcDriver() { |
| 80 | + return jdbcDriver; |
47 | 81 | }
|
48 | 82 |
|
49 | 83 | @Override
|
50 |
| - public DatabaseConnectionInfo setDBVersion(DatabaseVersion dbVersion) { |
51 |
| - if ( checkValidVersion(dbVersion) && ZERO_VERSION.equals(this.dbVersion) ) { |
52 |
| - this.dbVersion = dbVersion; |
53 |
| - } |
54 |
| - return this; |
| 84 | + public DatabaseVersion getDialectVersion() { |
| 85 | + return dialectVersion; |
55 | 86 | }
|
56 | 87 |
|
57 | 88 | @Override
|
58 |
| - public DatabaseConnectionInfo setDBAutoCommitMode(String dbAutoCommitMode) { |
59 |
| - if ( checkValidString(dbAutoCommitMode) && isDefaultStringValue(this.dbAutoCommitMode) ) { |
60 |
| - this.dbAutoCommitMode = dbAutoCommitMode; |
61 |
| - } |
62 |
| - return this; |
| 89 | + public String getAutoCommitMode() { |
| 90 | + return autoCommitMode; |
63 | 91 | }
|
64 | 92 |
|
65 | 93 | @Override
|
66 |
| - public DatabaseConnectionInfo setDBIsolationLevel(String dbIsolationLevel) { |
67 |
| - if ( checkValidString(dbIsolationLevel) && isDefaultStringValue(this.dbIsolationLevel) ) { |
68 |
| - this.dbIsolationLevel = dbIsolationLevel; |
69 |
| - } |
70 |
| - return this; |
| 94 | + public String getIsolationLevel() { |
| 95 | + return isolationLevel; |
71 | 96 | }
|
72 | 97 |
|
73 | 98 | @Override
|
74 |
| - public DatabaseConnectionInfo setDBMinPoolSize(String minPoolSize) { |
75 |
| - if ( checkValidInteger(minPoolSize) ) { |
76 |
| - this.dbMinPoolSize = minPoolSize; |
77 |
| - } |
78 |
| - return this; |
| 99 | + public Integer getPoolMinSize() { |
| 100 | + return poolMinSize; |
79 | 101 | }
|
80 | 102 |
|
81 | 103 | @Override
|
82 |
| - public DatabaseConnectionInfo setDBMaxPoolSize(String maxPoolSize) { |
83 |
| - if ( checkValidInteger(maxPoolSize) ) { |
84 |
| - this.dbMaxPoolSize = maxPoolSize; |
85 |
| - } |
86 |
| - return this; |
| 104 | + public Integer getPoolMaxSize() { |
| 105 | + return poolMaxSize; |
87 | 106 | }
|
88 | 107 |
|
89 |
| - private boolean checkValidInteger(String integerString) { |
90 |
| - try { |
91 |
| - return checkValidString( integerString ) && Integer.parseInt( integerString, 10 ) >= 0; |
92 |
| - } |
93 |
| - catch (NumberFormatException e) { |
94 |
| - return false; |
95 |
| - } |
| 108 | + @Override |
| 109 | + public String toInfoString() { |
| 110 | + return "\tDatabase JDBC URL [" + handleEmpty( jdbcUrl ) + ']' + |
| 111 | + "\n\tDatabase driver: " + handleEmpty( jdbcDriver ) + |
| 112 | + "\n\tDatabase version: " + handleEmpty( dialectVersion ) + |
| 113 | + "\n\tAutocommit mode: " + handleEmpty( autoCommitMode ) + |
| 114 | + "\n\tIsolation level: " + handleEmpty( isolationLevel ) + |
| 115 | + "\n\tMinimum pool size: " + handleEmpty( poolMinSize ) + |
| 116 | + "\n\tMaximum pool size: " + handleEmpty( poolMaxSize ); |
96 | 117 | }
|
97 | 118 |
|
98 |
| - private boolean checkValidString(String value) { |
99 |
| - return !( StringHelper.isBlank( value ) || "null".equalsIgnoreCase( value ) ); |
| 119 | + private static String handleEmpty(String value) { |
| 120 | + return StringHelper.isNotEmpty( value ) ? value : DEFAULT; |
100 | 121 | }
|
101 | 122 |
|
102 |
| - private boolean checkValidVersion(DatabaseVersion version) { |
103 |
| - return version != null && !( version.isSame(ZERO_VERSION) || version.isSame(NO_VERSION) ); |
| 123 | + private static String handleEmpty(DatabaseVersion dialectVersion) { |
| 124 | + return dialectVersion != null ? dialectVersion.toString() : ZERO_VERSION.toString(); |
104 | 125 | }
|
105 | 126 |
|
106 |
| - private boolean isDefaultStringValue(String value) { |
107 |
| - return DEFAULT.equalsIgnoreCase( value ); |
| 127 | + private static String handleEmpty(Integer value) { |
| 128 | + return value != null ? value.toString() : DEFAULT; |
108 | 129 | }
|
109 | 130 |
|
110 |
| - @Override |
111 |
| - public String getDBInfoAsString() { |
112 |
| - StringBuilder sb = new StringBuilder(); |
113 |
| - sb.append( "\tDatabase JDBC URL [" ).append( dbUrl ).append(']'); |
114 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Database driver: " ).append( dbDriverName ); |
115 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Database version: " ).append( dbVersion ); |
116 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Autocommit mode: " ).append( dbAutoCommitMode ); |
117 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Isolation level: " ).append( dbIsolationLevel ); |
118 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Minimum pool size: " ).append( dbMinPoolSize ); |
119 |
| - sb.append(sb.length() > 0 ? "\n\t" : "" ).append( "Maximum pool size: " ).append( dbMaxPoolSize ); |
120 |
| - return sb.toString(); |
| 131 | + |
| 132 | + @SuppressWarnings("deprecation") |
| 133 | + private static String determineUrl(Map<String, Object> settings) { |
| 134 | + return NullnessHelper.coalesceSuppliedValues( |
| 135 | + () -> ConfigurationHelper.getString( JdbcSettings.JAKARTA_JDBC_URL, settings ), |
| 136 | + () -> ConfigurationHelper.getString( JdbcSettings.URL, settings ), |
| 137 | + () -> ConfigurationHelper.getString( JdbcSettings.JPA_JDBC_URL, settings ) |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @SuppressWarnings("deprecation") |
| 142 | + private static String determineDriver(Map<String, Object> settings) { |
| 143 | + return NullnessHelper.coalesceSuppliedValues( |
| 144 | + () -> ConfigurationHelper.getString( JdbcSettings.JAKARTA_JDBC_DRIVER, settings ), |
| 145 | + () -> ConfigurationHelper.getString( JdbcSettings.DRIVER, settings ), |
| 146 | + () -> ConfigurationHelper.getString( JdbcSettings.JPA_JDBC_DRIVER, settings ) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + private static String determineAutoCommitMode(Map<String, Object> settings) { |
| 151 | + return ConfigurationHelper.getString( JdbcSettings.AUTOCOMMIT, settings ); |
| 152 | + } |
| 153 | + |
| 154 | + private static String determineIsolationLevel(Map<String, Object> settings) { |
| 155 | + return toIsolationNiceName( interpretIsolation( settings.get(JdbcSettings.ISOLATION ) ) ); |
| 156 | + } |
| 157 | + |
| 158 | + private static Integer determinePoolMaxSize(Map<String, Object> settings) { |
| 159 | + return ConfigurationHelper.getInteger( JdbcSettings.POOL_SIZE, settings ); |
121 | 160 | }
|
122 | 161 | }
|
0 commit comments