Search in sources :

Example 31 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class ConnectionProviderInitiator method initiateService.

@Override
public ConnectionProvider initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) {
    if (configurationValues.containsKey(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER)) {
        // nothing to do, but given the separate hierarchies have to handle this here.
        return null;
    }
    final StrategySelector strategySelector = registry.getService(StrategySelector.class);
    final Object explicitSetting = configurationValues.get(AvailableSettings.CONNECTION_PROVIDER);
    if (explicitSetting != null) {
        // if we are explicitly supplied a ConnectionProvider to use (in some form) -> 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 = StringHelper.nullIfEmpty(explicitSetting.toString());
            if (providerName != null) {
                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 Class<? extends ConnectionProvider> singleRegisteredProvider = getSingleRegisteredProvider(strategySelector);
    if (singleRegisteredProvider != null) {
        try {
            connectionProvider = singleRegisteredProvider.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            throw new HibernateException("Could not instantiate singular-registered ConnectionProvider", e);
        }
    }
    if (connectionProvider == null) {
        if (c3p0ConfigDefined(configurationValues)) {
            connectionProvider = instantiateC3p0Provider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (proxoolConfigDefined(configurationValues)) {
            connectionProvider = instantiateProxoolProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (hikariConfigDefined(configurationValues)) {
            connectionProvider = instantiateHikariProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (viburConfigDefined(configurationValues)) {
            connectionProvider = instantiateViburProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (agroalConfigDefined(configurationValues)) {
            connectionProvider = instantiateAgroalProvider(strategySelector);
        }
    }
    if (connectionProvider == null) {
        if (configurationValues.get(AvailableSettings.URL) != null) {
            connectionProvider = new DriverManagerConnectionProviderImpl();
        }
    }
    if (connectionProvider == null) {
        LOG.noAppropriateConnectionProvider();
        connectionProvider = new UserSuppliedConnectionProviderImpl();
    }
    final Map<?, ?> injectionData = (Map<?, ?>) configurationValues.get(INJECTION_DATA);
    if (injectionData != null && injectionData.size() > 0) {
        final ConnectionProvider theConnectionProvider = connectionProvider;
        new BeanInfoHelper(connectionProvider.getClass()).applyToBeanInfo(connectionProvider, beanInfo -> {
            final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor descriptor : descriptors) {
                final String propertyName = descriptor.getName();
                if (injectionData.containsKey(propertyName)) {
                    final Method method = descriptor.getWriteMethod();
                    method.invoke(theConnectionProvider, injectionData.get(propertyName));
                }
            }
        });
    }
    return connectionProvider;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) HibernateException(org.hibernate.HibernateException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) BeanInfoHelper(org.hibernate.internal.util.beans.BeanInfoHelper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 32 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class EntityManagerFactoryBuilderImpl method cleanup.

private void cleanup() {
    // Stop and de-register the ConnectionProvider to prevent connections lying around
    if (standardServiceRegistry instanceof ServiceRegistryImplementor && standardServiceRegistry instanceof ServiceBinding.ServiceLifecycleOwner) {
        final ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) standardServiceRegistry;
        final ServiceBinding.ServiceLifecycleOwner lifecycleOwner = (ServiceBinding.ServiceLifecycleOwner) serviceRegistry;
        final ServiceBinding<ConnectionProvider> binding = serviceRegistry.locateServiceBinding(ConnectionProvider.class);
        if (binding != null && binding.getService() instanceof Stoppable) {
            lifecycleOwner.stopService(binding);
            binding.setService(null);
        }
    }
}
Also used : ServiceBinding(org.hibernate.service.spi.ServiceBinding) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) Stoppable(org.hibernate.service.spi.Stoppable) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

Example 33 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaJdbcOverridesForJtaDataSourceElement.

@Test
public // )
void testPassingIntegrationJpaJdbcOverridesForJtaDataSourceElement() {
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                // inject a JPA JTA DataSource setting into the PU
                final DataSource puDataSource = new DataSourceStub("puDataSource");

                @Override
                public DataSource getJtaDataSource() {
                    return puDataSource;
                }
            }, integrationOverrides);
        }
    };
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), // a JTA DataSource (nor the reverse).  However, that is a useful thing to support
    ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2"));
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object hibernateJdbcDriver = properties.get(AvailableSettings.URL);
        assertThat(hibernateJdbcDriver, notNullValue());
        final Object jpaJdbcDriver = properties.get(AvailableSettings.JPA_JDBC_URL);
        assertThat((String) jpaJdbcDriver, containsString("db2"));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 34 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class PersistenceUnitInfoTests method testJtaDataExposedAsProperty.

@Test
@TestForIssue(jiraKey = "HHH-13432")
public void testJtaDataExposedAsProperty() {
    final DataSource puDataSource = new DataSourceStub("puDataSource");
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        @Override
        public DataSource getJtaDataSource() {
            return puDataSource;
        }
    };
    final PersistenceProvider provider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(info, Collections.emptyMap());
    // first let's check the DataSource used in the EMF...
    final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
    assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
    final DatasourceConnectionProviderImpl dsCp = (DatasourceConnectionProviderImpl) connectionProvider;
    assertThat(dsCp.getDataSource(), is(puDataSource));
    // now let's check that it is exposed via the EMF properties
    // - again, the spec does not indicate that this should work, but
    // it worked this way in previous versions
    final Map<String, Object> properties = emf.getProperties();
    final Object o = properties.get(AvailableSettings.JPA_JTA_DATASOURCE);
    assertEquals(puDataSource, o);
}
Also used : DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with ConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider in project hibernate-orm by hibernate.

the class PersistenceUnitInfoTests method testNonJtaDataExposedAsProperty.

@Test
@TestForIssue(jiraKey = "HHH-13432")
public void testNonJtaDataExposedAsProperty() {
    final DataSource puDataSource = new DataSourceStub("puDataSource");
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        @Override
        public DataSource getNonJtaDataSource() {
            return puDataSource;
        }
    };
    final PersistenceProvider provider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(info, Collections.emptyMap());
    // first let's check the DataSource used in the EMF...
    final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
    assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
    final DatasourceConnectionProviderImpl dsCp = (DatasourceConnectionProviderImpl) connectionProvider;
    assertThat(dsCp.getDataSource(), is(puDataSource));
    // now let's check that it is exposed via the EMF properties
    // - note : the spec does not indicate that this should work, but
    // it worked this way in previous versions
    final Object o = emf.getProperties().get(AvailableSettings.JPA_NON_JTA_DATASOURCE);
    assertThat(o, is(puDataSource));
}
Also used : DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)41 Test (org.junit.Test)23 Connection (java.sql.Connection)13 TestForIssue (org.hibernate.testing.TestForIssue)11 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)10 HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)10 DataSource (javax.sql.DataSource)9 DataSourceStub (org.hibernate.testing.jdbc.DataSourceStub)9 PersistenceUnitInfoAdapter (org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter)9 PersistenceProvider (jakarta.persistence.spi.PersistenceProvider)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Properties (java.util.Properties)6 MetadataSources (org.hibernate.boot.MetadataSources)6 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)6 Test (org.junit.jupiter.api.Test)6 SQLException (java.sql.SQLException)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)5 DatasourceConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl)5