diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java index 14859f5f75..f8f24f8203 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java @@ -12,13 +12,14 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; import org.hibernate.internal.util.NullnessHelper; -import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.config.ConfigurationHelper; import static org.hibernate.dialect.SimpleDatabaseVersion.ZERO_VERSION; import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.interpretIsolation; import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName; +import static org.hibernate.internal.util.StringHelper.isNotEmpty; import static org.hibernate.internal.util.StringHelper.nullIfEmpty; +import static org.hibernate.internal.util.config.ConfigurationHelper.getString; /** * Standard implementation of {@link DatabaseConnectionInfo} @@ -122,7 +123,7 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo { } private static String handleEmpty(String value) { - return StringHelper.isNotEmpty( value ) ? value : DEFAULT; + return isNotEmpty( value ) ? value : DEFAULT; } private static String handleEmpty(DatabaseVersion dialectVersion) { @@ -141,23 +142,23 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo { @SuppressWarnings("deprecation") private static String determineUrl(Map settings) { return NullnessHelper.coalesceSuppliedValues( - () -> ConfigurationHelper.getString( JdbcSettings.JAKARTA_JDBC_URL, settings ), - () -> ConfigurationHelper.getString( JdbcSettings.URL, settings ), - () -> ConfigurationHelper.getString( JdbcSettings.JPA_JDBC_URL, settings ) + () -> getString( JdbcSettings.JAKARTA_JDBC_URL, settings ), + () -> getString( JdbcSettings.URL, settings ), + () -> getString( JdbcSettings.JPA_JDBC_URL, settings ) ); } @SuppressWarnings("deprecation") private static String determineDriver(Map settings) { return NullnessHelper.coalesceSuppliedValues( - () -> ConfigurationHelper.getString( JdbcSettings.JAKARTA_JDBC_DRIVER, settings ), - () -> ConfigurationHelper.getString( JdbcSettings.DRIVER, settings ), - () -> ConfigurationHelper.getString( JdbcSettings.JPA_JDBC_DRIVER, settings ) + () -> getString( JdbcSettings.JAKARTA_JDBC_DRIVER, settings ), + () -> getString( JdbcSettings.DRIVER, settings ), + () -> getString( JdbcSettings.JPA_JDBC_DRIVER, settings ) ); } private static String determineAutoCommitMode(Map settings) { - return ConfigurationHelper.getString( JdbcSettings.AUTOCOMMIT, settings ); + return getString( JdbcSettings.AUTOCOMMIT, settings ); } private static String determineIsolationLevel(Map settings) { diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java index 310a6a84ba..f2d9b8b118 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java @@ -16,6 +16,7 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; +import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jndi.spi.JndiService; import org.hibernate.internal.log.ConnectionInfoLogger; import org.hibernate.service.UnknownUnwrapTypeException; @@ -24,6 +25,7 @@ import org.hibernate.service.spi.InjectService; import org.hibernate.service.spi.Stoppable; import static org.hibernate.cfg.JdbcSettings.DATASOURCE; +import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName; /** * A {@link ConnectionProvider} that manages connections from an underlying {@link DataSource}. @@ -40,6 +42,7 @@ import static org.hibernate.cfg.JdbcSettings.DATASOURCE; * @author Steve Ebersole */ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Configurable, Stoppable { + private DataSource dataSource; private String user; private String pass; @@ -149,21 +152,41 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con @Override public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) { + return getDatabaseConnectionInfo( dialect, null ); + } + + @Override + public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, ExtractedDatabaseMetaData metaData) { + final String url; + final String driver; + final String isolationLevel; + if ( metaData != null ) { + url = metaData.getUrl(); + driver = metaData.getDriver(); + isolationLevel = toIsolationNiceName( metaData.getTransactionIsolation() ) + + " [default " + toIsolationNiceName( metaData.getDefaultTransactionIsolation() ) + "]"; + } + else { + url = null; + driver = null; + isolationLevel = null; + } + return new DatabaseConnectionInfoImpl( DatasourceConnectionProviderImpl.class, - null, - null, + url, + driver, dialect.getVersion(), null, - null, + isolationLevel, null, null ) { @Override public String toInfoString() { return dataSourceJndiName != null - ? "\tDatasource JNDI name [" + dataSourceJndiName + "]" - : "\tProvided DataSource"; + ? "\tDataSource JNDI name [" + dataSourceJndiName + "]\n" + super.toInfoString() + : super.toInfoString(); } }; } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java index bff9f5e5c8..7febd4a1ea 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java @@ -38,6 +38,7 @@ import org.hibernate.service.spi.Stoppable; import org.hibernate.internal.log.ConnectionInfoLogger; import static org.hibernate.cfg.JdbcSettings.JAKARTA_JDBC_URL; +import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName; import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; import static org.hibernate.internal.util.config.ConfigurationHelper.getInt; import static org.hibernate.internal.util.config.ConfigurationHelper.getLong; @@ -148,7 +149,7 @@ public class DriverManagerConnectionProviderImpl driverList, SimpleDatabaseVersion.ZERO_VERSION, Boolean.toString( autoCommit ), - isolation != null ? ConnectionProviderInitiator.toIsolationNiceName( isolation ) : null, + isolation != null ? toIsolationNiceName( isolation ) : null, getInt( MIN_SIZE, configurationValues, 1 ), getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/ConnectionProvider.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/ConnectionProvider.java index 8267c069f7..d993c8f500 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/ConnectionProvider.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/ConnectionProvider.java @@ -9,6 +9,7 @@ import java.sql.SQLException; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl; +import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.service.Service; import org.hibernate.service.spi.Wrapped; @@ -71,8 +72,21 @@ public interface ConnectionProvider extends Service, Wrapped { */ boolean supportsAggressiveRelease(); + /** + * @return an informative instance of {@link DatabaseConnectionInfo} for logging. + * + * @since 6.6 + */ default DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) { return new DatabaseConnectionInfoImpl( dialect ); } + /** + * @return an informative instance of {@link DatabaseConnectionInfo} for logging. + * + * @since 7.0 + */ + default DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, ExtractedDatabaseMetaData metaData) { + return getDatabaseConnectionInfo( dialect ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/ExtractedDatabaseMetaDataImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/ExtractedDatabaseMetaDataImpl.java index 872ce33e47..09357e475e 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/ExtractedDatabaseMetaDataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/ExtractedDatabaseMetaDataImpl.java @@ -44,6 +44,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData private final boolean supportsDataDefinitionInTransaction; private final boolean doesDataDefinitionCauseTransactionCommit; private final SQLStateType sqlStateType; + private final int transactionIsolation; + private final int defaultTransactionIsolation; + private final String url; + private final String driver; private final boolean jdbcMetadataAccessible; @@ -65,6 +69,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData boolean supportsDataDefinitionInTransaction, boolean doesDataDefinitionCauseTransactionCommit, SQLStateType sqlStateType, + int transactionIsolation, + int defaultTransactionIsolation, + String url, + String driver, boolean jdbcMetadataIsAccessible) { this.jdbcEnvironment = jdbcEnvironment; this.connectionAccess = connectionAccess; @@ -78,6 +86,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData this.supportsDataDefinitionInTransaction = supportsDataDefinitionInTransaction; this.doesDataDefinitionCauseTransactionCommit = doesDataDefinitionCauseTransactionCommit; this.sqlStateType = sqlStateType; + this.transactionIsolation = transactionIsolation; + this.defaultTransactionIsolation = defaultTransactionIsolation; + this.url = url; + this.driver = driver; this.jdbcMetadataAccessible = jdbcMetadataIsAccessible; } @@ -136,6 +148,26 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData return connectionSchemaName; } + @Override + public String getUrl() { + return url; + } + + @Override + public String getDriver() { + return driver; + } + + @Override + public int getTransactionIsolation() { + return transactionIsolation; + } + + @Override + public int getDefaultTransactionIsolation() { + return defaultTransactionIsolation; + } + @Override public synchronized List getSequenceInformationList() { if ( jdbcMetadataAccessible ) { @@ -175,6 +207,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData private boolean supportsDataDefinitionInTransaction; private boolean doesDataDefinitionCauseTransactionCommit; private SQLStateType sqlStateType; + private String url; + private String driver; + private int defaultTransactionIsolation; + private int transactionIsolation; public Builder(JdbcEnvironment jdbcEnvironment, boolean jdbcMetadataIsAccessible, JdbcConnectionAccess connectionAccess) { this.jdbcEnvironment = jdbcEnvironment; @@ -193,6 +229,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData supportsDataDefinitionInTransaction = !databaseMetaData.dataDefinitionIgnoredInTransactions(); doesDataDefinitionCauseTransactionCommit = databaseMetaData.dataDefinitionCausesTransactionCommit(); sqlStateType = SQLStateType.interpretReportedSQLStateType( databaseMetaData.getSQLStateType() ); + url = databaseMetaData.getURL(); + driver = databaseMetaData.getDriverName(); + defaultTransactionIsolation = databaseMetaData.getDefaultTransactionIsolation(); + transactionIsolation = databaseMetaData.getConnection().getTransactionIsolation(); return this; } @@ -260,6 +300,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData supportsDataDefinitionInTransaction, doesDataDefinitionCauseTransactionCommit, sqlStateType, + transactionIsolation, + defaultTransactionIsolation, + url, + driver, jdbcMetadataIsAccessible ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java index e0a57cef88..8629b579dd 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java @@ -86,12 +86,12 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { final IdentifierHelperBuilder identifierHelperBuilder = identifierHelperBuilder( cfgService, nameQualifierSupport ); - final ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder = + final ExtractedDatabaseMetaDataImpl.Builder metaDataBuilder = new ExtractedDatabaseMetaDataImpl.Builder( this, false, null ); - this.identifierHelper = identifierHelper( dialect, identifierHelperBuilder, dbMetaDataBuilder );; + this.identifierHelper = identifierHelper( dialect, identifierHelperBuilder, metaDataBuilder );; - this.extractedMetaDataSupport = dbMetaDataBuilder.build(); + this.extractedMetaDataSupport = metaDataBuilder.build(); this.currentCatalog = identifierHelper.toIdentifier( cfgService.getSetting( AvailableSettings.DEFAULT_CATALOG, StandardConverters.STRING ) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java index 52442048c8..54d54ecdda 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java @@ -34,7 +34,6 @@ import org.hibernate.event.monitor.internal.EmptyEventMonitor; import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.log.ConnectionInfoLogger; -import org.hibernate.internal.util.StringHelper; import org.hibernate.jdbc.AbstractReturningWork; import org.hibernate.jpa.internal.MutableJpaComplianceImpl; import org.hibernate.jpa.spi.JpaCompliance; @@ -50,6 +49,7 @@ import org.hibernate.stat.spi.StatisticsImplementor; import org.jboss.logging.Logger; +import static java.lang.Integer.parseInt; import static org.hibernate.cfg.AvailableSettings.CONNECTION_HANDLING; import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_MAJOR_VERSION; import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_MINOR_VERSION; @@ -69,6 +69,7 @@ import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.isMulti import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER; import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues; import static org.hibernate.internal.util.StringHelper.isNotEmpty; +import static org.hibernate.internal.util.StringHelper.split; import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; import static org.hibernate.internal.util.config.ConfigurationHelper.getBooleanWrapper; import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger; @@ -100,8 +101,6 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator configurationValues, ServiceRegistryImplementor registry) { - final DialectFactory dialectFactory = registry.requireService( DialectFactory.class ); - final String explicitDatabaseName = getExplicitDatabaseName( configurationValues ); Integer explicitDatabaseMajorVersion = getExplicitDatabaseMajorVersion( configurationValues ); Integer explicitDatabaseMinorVersion = getExplicitDatabaseMinorVersion( configurationValues ); @@ -110,11 +109,11 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator 1 ) { - explicitDatabaseMinorVersion = Integer.parseInt( parts[1] ); + explicitDatabaseMinorVersion = parseInt( parts[1] ); } explicitDatabaseMajorVersion = potentialMajor; } @@ -123,8 +122,27 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator configurationValues, + ServiceRegistryImplementor registry, + String explicitDatabaseName, + Integer explicitDatabaseMajorVersion, + Integer explicitDatabaseMinorVersion, + String explicitDatabaseVersion) { + final DialectFactory dialectFactory = registry.requireService( DialectFactory.class ); + final JdbcEnvironment jdbcEnvironment; - DatabaseConnectionInfo databaseConnectionInfo; + final DatabaseConnectionInfo databaseConnectionInfo; if ( allowJdbcMetadataAccess( configurationValues ) ) { jdbcEnvironment = getJdbcEnvironmentUsingJdbcMetadata( configurationValues, @@ -133,8 +151,9 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator mcp = - registry.requireService( MultiTenantConnectionProvider.class ); - return mcp.getDatabaseConnectionInfo( dialect ); + return registry.requireService( ConnectionProvider.class ) + .getDatabaseConnectionInfo( environment.getDialect(), environment.getExtractedDatabaseMetaData() ); } } - private DatabaseConnectionInfo buildDbInfo(Map configurationValues, Dialect dialect) { - return new DatabaseConnectionInfoImpl( configurationValues, dialect ); + private DatabaseConnectionInfo buildInfo(Map configurationValues, JdbcEnvironment environment) { + return new DatabaseConnectionInfoImpl( configurationValues, environment.getDialect() ); } private static JdbcEnvironmentImpl getJdbcEnvironmentWithDefaults( @@ -372,7 +389,7 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator configValues) { + delegate.configure(configValues); + setDataSource( new DataSource() { + PrintWriter logWriter = new PrintWriter( System.out ); + @Override + public Connection getConnection() throws SQLException { + return delegate.getConnection(); + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return delegate.getConnection(); + } + + @Override + public PrintWriter getLogWriter() { + return logWriter; + } + + @Override + public void setLogWriter(PrintWriter out) { + this.logWriter = out; + } + + @Override + public void setLoginTimeout(int seconds) { + + } + + @Override + public int getLoginTimeout() { + return -1; + } + + @Override + public T unwrap(Class iface) throws SQLException { + throw new SQLFeatureNotSupportedException(); + } + + @Override + public boolean isWrapperFor(Class iface) { + return false; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + throw new SQLFeatureNotSupportedException(); + } + } ); + super.configure( configValues ); + } + + @Override + public void injectServices(ServiceRegistryImplementor serviceRegistry) { + delegate.injectServices( serviceRegistry ); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/jdbc/ConnectionProviderDelegate.java b/hibernate-testing/src/main/java/org/hibernate/testing/jdbc/ConnectionProviderDelegate.java index f068d87b59..e9e23e76d3 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/jdbc/ConnectionProviderDelegate.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/jdbc/ConnectionProviderDelegate.java @@ -14,6 +14,7 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; +import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryImplementor; @@ -108,6 +109,11 @@ public class ConnectionProviderDelegate implements return connectionProvider.getDatabaseConnectionInfo( dialect ); } + @Override + public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, ExtractedDatabaseMetaData metaData) { + return connectionProvider.getDatabaseConnectionInfo( dialect, metaData ); + } + @Override public boolean isUnwrappableAs(Class unwrapType) { return connectionProvider.isUnwrappableAs( unwrapType ); diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/jta/JtaAwareConnectionProviderImpl.java b/hibernate-testing/src/main/java/org/hibernate/testing/jta/JtaAwareConnectionProviderImpl.java index 3678dfa5bd..5587ac5e5e 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/jta/JtaAwareConnectionProviderImpl.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/jta/JtaAwareConnectionProviderImpl.java @@ -56,6 +56,7 @@ import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiato import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; +import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.internal.build.AllowSysOut; import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.ServiceRegistryAwareService; @@ -183,6 +184,11 @@ public class JtaAwareConnectionProviderImpl implements ConnectionProvider, Confi return delegate.getDatabaseConnectionInfo( dialect ); } + @Override + public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, ExtractedDatabaseMetaData metaData) { + return delegate.getDatabaseConnectionInfo( dialect, metaData ); + } + protected Transaction findCurrentTransaction() { try { return TestingJtaPlatformImpl.transactionManager().getTransaction();