Search in sources :

Example 21 with ConnectionProvider

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

the class MultiTenancyTest method configureStandardServiceRegistryBuilder.

@Override
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
    super.configureStandardServiceRegistryBuilder(ssrb);
    ssrb.addService(MultiTenantConnectionProvider.class, new AbstractMultiTenantConnectionProvider() {

        @Override
        protected ConnectionProvider getAnyConnectionProvider() {
            return db1;
        }

        @Override
        protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) {
            if (DB1.equals(tenantIdentifier))
                return db1;
            if (DB2.equals(tenantIdentifier))
                return db2;
            throw new IllegalArgumentException();
        }
    });
}
Also used : AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) XaConnectionProvider(org.hibernate.test.cache.infinispan.tm.XaConnectionProvider) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider)

Example 22 with ConnectionProvider

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

the class TableGeneratorQuotingTest method testTableGeneratorQuoting.

@Test
@TestForIssue(jiraKey = "HHH-7927")
public void testTableGeneratorQuoting() {
    final Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(TestEntity.class).buildMetadata();
    final ConnectionProvider connectionProvider = serviceRegistry.getService(ConnectionProvider.class);
    final GenerationTarget target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider)));
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, false, target);
    try {
        new SchemaValidator().validate(metadata);
    } catch (HibernateException e) {
        fail("The identifier generator table should have validated.  " + e.getMessage());
    } finally {
        new SchemaDropperImpl(serviceRegistry).doDrop(metadata, false, target);
    }
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) HibernateException(org.hibernate.HibernateException) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SchemaValidator(org.hibernate.tool.hbm2ddl.SchemaValidator) GenerationTarget(org.hibernate.tool.schema.internal.exec.GenerationTarget) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 23 with ConnectionProvider

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

the class TestExtraPhysicalTableTypes method buildInformationExtractorJdbcDatabaseMetaDataImplTest.

private InformationExtractorJdbcDatabaseMetaDataImplTest buildInformationExtractorJdbcDatabaseMetaDataImplTest() throws SQLException {
    Database database = metadata.getDatabase();
    final ConnectionProvider connectionProvider = ssr.getService(ConnectionProvider.class);
    DatabaseInformation dbInfo = new DatabaseInformationImpl(ssr, database.getJdbcEnvironment(), new DdlTransactionIsolatorTestingImpl(ssr, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider)), database.getDefaultNamespace().getName());
    ExtractionContextImpl extractionContext = new ExtractionContextImpl(ssr, database.getJdbcEnvironment(), ssr.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess(), (ExtractionContext.DatabaseObjectAccess) dbInfo, database.getDefaultNamespace().getPhysicalName().getCatalog(), database.getDefaultNamespace().getPhysicalName().getSchema());
    return new InformationExtractorJdbcDatabaseMetaDataImplTest(extractionContext);
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) DatabaseInformation(org.hibernate.tool.schema.extract.spi.DatabaseInformation) ExtractionContext(org.hibernate.tool.schema.extract.spi.ExtractionContext) Database(org.hibernate.boot.model.relational.Database) DatabaseInformationImpl(org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl) ExtractionContextImpl(org.hibernate.tool.schema.extract.internal.ExtractionContextImpl) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

Example 24 with ConnectionProvider

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

the class JPADatabase method create0.

@SuppressWarnings("serial")
@Override
protected DSLContext create0() {
    if (connection == null) {
        String packages = getProperties().getProperty("packages");
        if (isBlank(packages)) {
            packages = "";
            log.warn("No packages defined", "It is highly recommended that you provide explicit packages to scan");
        }
        try {
            connection = DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", "");
            MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect").applySetting("javax.persistence.schema-generation-connection", connection).applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {

                @SuppressWarnings("rawtypes")
                @Override
                public boolean isUnwrappableAs(Class unwrapType) {
                    return false;
                }

                @Override
                public <T> T unwrap(Class<T> unwrapType) {
                    return null;
                }

                @Override
                public Connection getConnection() {
                    return connection;
                }

                @Override
                public void closeConnection(Connection conn) throws SQLException {
                }

                @Override
                public boolean supportsAggressiveRelease() {
                    return true;
                }
            }).build());
            ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
            scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
            // [#5845] Use the correct ClassLoader to load the jpa entity classes defined in the user project
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            for (String pkg : packages.split(",")) for (BeanDefinition def : scanner.findCandidateComponents(defaultIfBlank(pkg, "").trim())) metadata.addAnnotatedClass(Class.forName(def.getBeanClassName(), true, cl));
            // This seems to be the way to do this in idiomatic Hibernate 5.0 API
            // See also: http://stackoverflow.com/q/32178041/521799
            // SchemaExport export = new SchemaExport((MetadataImplementor) metadata.buildMetadata(), connection);
            // export.create(true, true);
            // Hibernate 5.2 broke 5.0 API again. Here's how to do this now:
            SchemaExport export = new SchemaExport();
            export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
        } catch (Exception e) {
            throw new DataAccessException("Error while exporting schema", e);
        }
    }
    return DSL.using(connection);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) MetadataSources(org.hibernate.boot.MetadataSources) Connection(java.sql.Connection) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) DataAccessException(org.jooq.exception.DataAccessException) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 25 with ConnectionProvider

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

the class SingleRegisteredProviderTest method testConnectionsRegistered.

@Test
public void testConnectionsRegistered() {
    final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final Collection<Class<? extends ConnectionProvider>> implementors = bsr.getService(StrategySelector.class).getRegisteredStrategyImplementors(ConnectionProvider.class);
    assertThat(implementors.size(), equalTo(0));
    bsr.getService(StrategySelector.class).registerStrategyImplementor(ConnectionProvider.class, "testing", DriverManagerConnectionProviderImpl.class);
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).build();
    final ConnectionProvider configuredProvider = ssr.getService(ConnectionProvider.class);
    assertThat(configuredProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
}
Also used : DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

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