Skip to content

Commit bc4cd37

Browse files
committed
improve rendering of catalog/schema log message
use information about whether the database actually supports schemas
1 parent 652b430 commit bc4cd37

File tree

10 files changed

+148
-5
lines changed

10 files changed

+148
-5
lines changed

hibernate-agroal/src/main/java/org/hibernate/agroal/internal/AgroalConnectionProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
4343
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
4444
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getSchema;
45+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasCatalog;
46+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasSchema;
4547
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;
4648

4749
/**
@@ -179,6 +181,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
179181
: extractDriverNameFromMetadata(),
180182
dialect.getClass(),
181183
dialect.getVersion(),
184+
hasSchema( agroalDataSource),
185+
hasCatalog( agroalDataSource),
182186
getSchema( agroalDataSource ),
183187
getCatalog( agroalDataSource ),
184188
Boolean.toString( connectionConfig.autoCommit() ),

hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
4848
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
4949
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getSchema;
50+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasCatalog;
51+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasSchema;
5052
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
5153
import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger;
5254

@@ -173,6 +175,8 @@ public void configure(Map<String, Object> properties) {
173175
jdbcDriverClass,
174176
dialect.getClass(),
175177
dialect.getVersion(),
178+
hasSchema( dataSource ),
179+
hasCatalog( dataSource ),
176180
schema,
177181
catalog,
178182
Boolean.toString( autocommit ),

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
3838
protected final String jdbcDriver;
3939
private final Class<? extends Dialect> dialectClass;
4040
protected final DatabaseVersion dialectVersion;
41+
private final boolean hasSchema;
42+
private final boolean hasCatalog;
4143
protected final String schema;
4244
protected final String catalog;
4345
protected final String autoCommitMode;
@@ -51,6 +53,8 @@ public DatabaseConnectionInfoImpl(
5153
String jdbcUrl,
5254
String jdbcDriver,
5355
Class<? extends Dialect> dialectClass, DatabaseVersion dialectVersion,
56+
boolean hasSchema,
57+
boolean hasCatalog,
5458
String schema,
5559
String catalog,
5660
String autoCommitMode,
@@ -62,6 +66,8 @@ public DatabaseConnectionInfoImpl(
6266
this.jdbcUrl = nullIfEmpty( jdbcUrl );
6367
this.jdbcDriver = nullIfEmpty( jdbcDriver );
6468
this.dialectVersion = dialectVersion;
69+
this.hasSchema = hasSchema;
70+
this.hasCatalog = hasCatalog;
6571
this.schema = schema;
6672
this.catalog = catalog;
6773
this.autoCommitMode = nullIfEmpty( autoCommitMode );
@@ -79,6 +85,8 @@ public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect)
7985
determineDriver( settings ),
8086
dialect.getClass(),
8187
dialect.getVersion(),
88+
true,
89+
true,
8290
null,
8391
null,
8492
determineAutoCommitMode( settings ),
@@ -91,7 +99,40 @@ public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect)
9199
}
92100

93101
public DatabaseConnectionInfoImpl(Dialect dialect) {
94-
this( null, null, null, dialect.getClass(), dialect.getVersion(), null, null, null, null, null, null, null );
102+
this(
103+
null,
104+
null,
105+
null,
106+
dialect.getClass(),
107+
dialect.getVersion(),
108+
true,
109+
true,
110+
null,
111+
null,
112+
null,
113+
null,
114+
null,
115+
null,
116+
null
117+
);
118+
}
119+
120+
public static boolean hasSchema(DataSource dataSource) {
121+
try ( var conn = dataSource.getConnection() ) {
122+
return conn.getMetaData().supportsSchemasInDataManipulation();
123+
}
124+
catch ( SQLException ignored ) {
125+
return true;
126+
}
127+
}
128+
129+
public static boolean hasCatalog(DataSource dataSource) {
130+
try ( var conn = dataSource.getConnection() ) {
131+
return conn.getMetaData().supportsCatalogsInDataManipulation();
132+
}
133+
catch ( SQLException ignored ) {
134+
return true;
135+
}
95136
}
96137

97138
public static String getSchema(DataSource dataSource) {
@@ -112,6 +153,24 @@ public static String getCatalog(DataSource dataSource) {
112153
}
113154
}
114155

156+
static boolean hasSchema(ConnectionCreator creator) {
157+
try ( var conn = creator.createConnection() ) {
158+
return conn.getMetaData().supportsSchemasInTableDefinitions();
159+
}
160+
catch ( SQLException ignored ) {
161+
return true;
162+
}
163+
}
164+
165+
static boolean hasCatalog(ConnectionCreator creator) {
166+
try ( var conn = creator.createConnection() ) {
167+
return conn.getMetaData().supportsCatalogsInDataManipulation();
168+
}
169+
catch ( SQLException ignored ) {
170+
return true;
171+
}
172+
}
173+
115174
static String getSchema(ConnectionCreator creator) {
116175
try ( var conn = creator.createConnection() ) {
117176
return conn.getSchema();
@@ -220,6 +279,16 @@ public Integer getPoolMaxSize() {
220279
return catalog;
221280
}
222281

282+
@Override
283+
public boolean hasSchema() {
284+
return hasSchema;
285+
}
286+
287+
@Override
288+
public boolean hasCatalog() {
289+
return hasCatalog;
290+
}
291+
223292
@Override
224293
public String toInfoString() {
225294
return """
@@ -239,8 +308,8 @@ public String toInfoString() {
239308
handleEmpty( jdbcDriver ),
240309
handleEmpty( dialectClass ),
241310
handleEmpty( dialectVersion ),
242-
handleEmpty( catalog ),
243-
handleEmpty( schema ),
311+
handleEmpty( catalog, hasCatalog ),
312+
handleEmpty( schema, hasSchema ),
244313
handleEmpty( autoCommitMode ),
245314
handleEmpty( isolationLevel ),
246315
handleFetchSize( fetchSize ),
@@ -254,6 +323,10 @@ private static String handleEmpty(String value) {
254323
return isNotEmpty( value ) ? value : DEFAULT;
255324
}
256325

326+
private static String handleEmpty(String value, boolean defined) {
327+
return isNotEmpty( value ) ? value : (defined ? "unknown" : "undefined");
328+
}
329+
257330
private static String handleEmpty(DatabaseVersion dialectVersion) {
258331
return dialectVersion != null ? dialectVersion.toString() : ZERO_VERSION.toString();
259332
}

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, Extract
163163
metaData == null ? null : metaData.getDriver(),
164164
dialect.getClass(),
165165
dialect.getVersion(),
166+
metaData == null || metaData.supportsSchemas(),
167+
metaData == null || metaData.supportsCatalogs(),
166168
metaData == null ? null : metaData.getConnectionSchemaName(),
167169
metaData == null ? null : metaData.getConnectionCatalogName(),
168170
null,

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
4747
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
4848
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getSchema;
49+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasCatalog;
50+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasSchema;
4951
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
5052
import static org.hibernate.internal.util.config.ConfigurationHelper.getInt;
5153
import static org.hibernate.internal.util.config.ConfigurationHelper.getLong;
@@ -167,6 +169,8 @@ private static ConnectionCreator buildCreator(
167169
driverList,
168170
null,
169171
SimpleDatabaseVersion.ZERO_VERSION,
172+
hasSchema( connectionCreator ),
173+
hasCatalog( connectionCreator ),
170174
getSchema( connectionCreator ),
171175
getCatalog( connectionCreator ),
172176
Boolean.toString( autoCommit ),
@@ -306,6 +310,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
306310
dbInfo.getJdbcDriver(),
307311
dialect.getClass(),
308312
dialect.getVersion(),
313+
dbInfo.hasSchema(),
314+
dbInfo.hasCatalog(),
309315
dbInfo.getSchema(),
310316
dbInfo.getCatalog(),
311317
dbInfo.getAutoCommitMode(),

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
123123
null,
124124
dialect.getClass(),
125125
dialect.getVersion(),
126+
true,
127+
true,
126128
null,
127129
null,
128130
null,

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DatabaseConnectionInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.engine.jdbc.connections.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.dialect.DatabaseVersion;
89
import org.hibernate.dialect.Dialect;
910

@@ -79,6 +80,12 @@ public interface DatabaseConnectionInfo {
7980
@Nullable
8081
Integer getJdbcFetchSize();
8182

83+
@Internal
84+
boolean hasSchema();
85+
86+
@Internal
87+
boolean hasCatalog();
88+
8289
/**
8390
* Collects the information available here as a single String with the intent of using it in logging.
8491
*/

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/ExtractedDatabaseMetaDataImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
3535
private final JdbcEnvironment jdbcEnvironment;
3636
private final JdbcConnectionAccess connectionAccess;
3737

38+
private final boolean supportsSchemas;
39+
private final boolean supportsCatalogs;
3840
private final String connectionCatalogName;
3941
private final String connectionSchemaName;
4042

@@ -67,6 +69,8 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
6769
jdbcMetadataAccessible = false;
6870
connectionSchemaName = null;
6971
connectionCatalogName = null;
72+
supportsSchemas = true;
73+
supportsCatalogs = true;
7074
databaseProductName = null;
7175
databaseProductVersion = null;
7276
supportsRefCursors = false;
@@ -94,6 +98,8 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
9498
jdbcMetadataAccessible = true;
9599
final Dialect dialect = environment.getDialect();
96100
final Connection connection = metaData.getConnection();
101+
supportsSchemas = metaData.supportsSchemasInDataManipulation();
102+
supportsCatalogs = metaData.supportsCatalogsInDataManipulation();
97103
connectionSchemaName = dialect.getSchemaNameResolver().resolveSchemaName( connection, dialect );
98104
connectionCatalogName = connection.getCatalog();
99105
databaseProductName = metaData.getDatabaseProductName();
@@ -123,6 +129,16 @@ public JdbcEnvironment getJdbcEnvironment() {
123129
return jdbcEnvironment;
124130
}
125131

132+
@Override
133+
public boolean supportsSchemas() {
134+
return supportsSchemas;
135+
}
136+
137+
@Override
138+
public boolean supportsCatalogs() {
139+
return supportsCatalogs;
140+
}
141+
126142
@Override
127143
public boolean supportsNamedParameters() {
128144
return supportsNamedParameters;

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/ExtractedDatabaseMetaData.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,34 @@ public interface ExtractedDatabaseMetaData {
2626
*/
2727
JdbcEnvironment getJdbcEnvironment();
2828

29+
/**
30+
* The name of the database, according to the JDBC driver.
31+
*/
2932
String getDatabaseProductName();
3033

34+
/**
35+
* The version of the database, according to the JDBC driver.
36+
*/
3137
String getDatabaseProductVersion();
3238

39+
/**
40+
* Does this driver support named schemas in DML?
41+
*
42+
* @return {@code false} indicates the driver reported false;
43+
* {@code true} indicates the driver reported true or that
44+
* the driver could not be asked.
45+
*/
46+
boolean supportsSchemas();
47+
48+
/**
49+
* Does this driver support named catalogs in DML?
50+
*
51+
* @return {@code false} indicates the driver reported false;
52+
* {@code true} indicates the driver reported true or that
53+
* the driver could not be asked.
54+
*/
55+
boolean supportsCatalogs();
56+
3357
/**
3458
* Retrieve the name of the catalog in effect when we connected to the database.
3559
*
@@ -59,8 +83,9 @@ public interface ExtractedDatabaseMetaData {
5983
/**
6084
* Does the driver report supporting {@link java.sql.Types#REF_CURSOR}?
6185
*
62-
* @return {@code true} indicates the driver reported true; {@code false} indicates the driver reported false
63-
* or that the driver could not be asked.
86+
* @return {@code true} indicates the driver reported true;
87+
* {@code false} indicates the driver reported false or that
88+
* the driver could not be asked.
6489
*
6590
* @see java.sql.DatabaseMetaData#supportsRefCursors()
6691
* @see org.hibernate.dialect.Dialect#supportsRefCursors

hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
2828
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
2929
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
30+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasCatalog;
31+
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.hasSchema;
3032
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;
3133
import static org.hibernate.hikaricp.internal.HikariConfigurationUtil.loadConfiguration;
3234
import static org.hibernate.internal.util.StringHelper.isBlank;
@@ -108,6 +110,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
108110
: hikariConfig.getDriverClassName(),
109111
dialect.getClass(),
110112
dialect.getVersion(),
113+
hasSchema( hikariDataSource ),
114+
hasCatalog( hikariDataSource ),
111115
hikariConfig.getSchema(),
112116
hikariConfig.getCatalog(),
113117
Boolean.toString( hikariConfig.isAutoCommit() ),

0 commit comments

Comments
 (0)