diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java index 4d15ca7951..0098dc90be 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java @@ -23,14 +23,13 @@ import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.beans.BeanInfoHelper; import org.hibernate.service.spi.ServiceRegistryImplementor; -import org.jboss.logging.Logger; - /** * Instantiates and configures an appropriate {@link ConnectionProvider}. * @@ -39,10 +38,7 @@ import org.jboss.logging.Logger; * @author Brett Meyer */ public class ConnectionProviderInitiator implements StandardServiceInitiator { - private static final CoreMessageLogger LOG = Logger.getMessageLogger( - CoreMessageLogger.class, - ConnectionProviderInitiator.class.getName() - ); + private static final CoreMessageLogger LOG = CoreLogging.messageLogger( ConnectionProviderInitiator.class ); /** * Singleton access @@ -104,15 +100,41 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator use it.. + if ( explicitSetting instanceof ConnectionProvider ) { + return (ConnectionProvider) explicitSetting; + } + else if ( explicitSetting instanceof Class ) { + final Class providerClass = (Class) explicitSetting; + LOG.instantiatingExplicitConnectionProvider( providerClass.getName() ); + return instantiateExplicitConnectionProvider( providerClass ); + } + else { + String providerName = explicitSetting.toString(); + if ( LEGACY_CONNECTION_PROVIDER_MAPPING.containsKey( providerName ) ) { + final String actualProviderName = LEGACY_CONNECTION_PROVIDER_MAPPING.get( providerName ); + DeprecationLogger.DEPRECATION_LOGGER.connectionProviderClassDeprecated( providerName, actualProviderName ); + providerName = actualProviderName; + } + + LOG.instantiatingExplicitConnectionProvider( providerName ); + final Class providerClass = strategySelector.selectStrategyImplementor( ConnectionProvider.class, providerName ); + try { + return instantiateExplicitConnectionProvider( providerClass ); + } + catch (Exception e) { + throw new HibernateException( "Could not instantiate connection provider [" + providerName + "]", e ); + } + } + } + + if ( configurationValues.get( AvailableSettings.DATASOURCE ) != null ) { + return new DatasourceConnectionProviderImpl(); + } ConnectionProvider connectionProvider = null; - final String providerName = getConfiguredConnectionProviderName( configurationValues ); - if ( providerName != null ) { - connectionProvider = instantiateExplicitConnectionProvider( providerName, strategySelector ); - } - else if ( configurationValues.get( AvailableSettings.DATASOURCE ) != null ) { - connectionProvider = new DatasourceConnectionProviderImpl(); - } if ( connectionProvider == null ) { if ( c3p0ConfigDefined( configurationValues ) ) { @@ -170,29 +192,13 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator. + */ +package org.hibernate.test.connections; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +/** + * @author Steve Ebersole + */ +public class ExplicitConnectionProviderInstanceTest extends BaseUnitTestCase { + @Test + public void testPassingConnectionProviderInstanceToBootstrap() { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( AvailableSettings.CONNECTION_PROVIDER, TestingConnectionProviderImpl.INSTANCE ) + .build(); + try { + assert ssr.getService( ConnectionProvider.class ) == TestingConnectionProviderImpl.INSTANCE; + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + public static class TestingConnectionProviderImpl implements ConnectionProvider { + /** + * Singleton access + */ + public static final TestingConnectionProviderImpl INSTANCE = new TestingConnectionProviderImpl(); + + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public void closeConnection(Connection conn) throws SQLException { + + } + + @Override + public boolean supportsAggressiveRelease() { + return false; + } + + @Override + public boolean isUnwrappableAs(Class unwrapType) { + return false; + } + + @Override + public T unwrap(Class unwrapType) { + return null; + } + } +}