Search in sources :

Example 16 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)

Example 17 with ConnectionProvider

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

the class PostgreSQLMultipleSchemaSequenceTest method test.

@Test
@TestForIssue(jiraKey = "HHH-5538")
public void test() {
    StandardServiceRegistry ssr1 = new StandardServiceRegistryBuilder().build();
    final String extraSchemaName = "extra_schema_sequence_validation";
    try {
        final MetadataImplementor metadata1 = (MetadataImplementor) new MetadataSources(ssr1).addAnnotatedClass(Box.class).buildMetadata();
        try {
            new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata1);
            final ConnectionProvider connectionProvider1 = ssr1.getService(ConnectionProvider.class);
            DdlTransactionIsolatorTestingImpl ddlTransactionIsolator1 = new DdlTransactionIsolatorTestingImpl(ssr1, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider1));
            try (Statement statement = ddlTransactionIsolator1.getIsolatedConnection().createStatement()) {
                statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                statement.execute(String.format("CREATE SCHEMA %s", extraSchemaName));
                try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                    while (resultSet.next()) {
                        Long sequenceValue = resultSet.getLong(1);
                        assertEquals(Long.valueOf(1L), sequenceValue);
                    }
                }
            } catch (SQLException e) {
                fail(e.getMessage());
            }
            StandardServiceRegistry ssr2 = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.URL, Environment.getProperties().get(AvailableSettings.URL) + "?currentSchema=" + extraSchemaName).build();
            try {
                final MetadataImplementor metadata2 = (MetadataImplementor) new MetadataSources(ssr2).addAnnotatedClass(Box.class).buildMetadata();
                try {
                    new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata2);
                } finally {
                    final ConnectionProvider connectionProvider2 = ssr2.getService(ConnectionProvider.class);
                    DdlTransactionIsolatorTestingImpl ddlTransactionIsolator2 = new DdlTransactionIsolatorTestingImpl(ssr2, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider2));
                    try (Statement statement = ddlTransactionIsolator2.getIsolatedConnection().createStatement()) {
                        try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                            while (resultSet.next()) {
                                Long sequenceValue = resultSet.getLong(1);
                                assertEquals(Long.valueOf(1L), sequenceValue);
                            }
                        }
                        statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                    } catch (SQLException e) {
                        fail(e.getMessage());
                    }
                    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata2);
                }
            } finally {
                StandardServiceRegistryBuilder.destroy(ssr2);
            }
        } finally {
            // clean up
            new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata1);
        }
        final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
        assertEquals(2, sqlLines.stream().filter(s -> s.equalsIgnoreCase("create sequence SEQ_TEST start 1 increment 1")).count());
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr1);
    }
}
Also used : JdbcEnvironmentInitiator(org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) Statement(java.sql.Statement) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IOException(java.io.IOException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) ResultSet(java.sql.ResultSet) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with ConnectionProvider

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

the class SchemaBasedMultiTenancyTest method testUnwrappingConnectionProvider.

@Test
@TestForIssue(jiraKey = "HHH-11651")
public void testUnwrappingConnectionProvider() {
    final MultiTenantConnectionProvider multiTenantConnectionProvider = serviceRegistry.getService(MultiTenantConnectionProvider.class);
    final ConnectionProvider connectionProvider = multiTenantConnectionProvider.unwrap(ConnectionProvider.class);
    assertThat(connectionProvider, is(notNullValue()));
}
Also used : AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with ConnectionProvider

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

the class SessionFactoryFactory method build.

public SessionFactory build(HibernateBundle<?> bundle, Environment environment, PooledDataSourceFactory dbConfig, ManagedDataSource dataSource, List<Class<?>> entities) {
    final ConnectionProvider provider = buildConnectionProvider(dataSource, dbConfig.getProperties());
    final SessionFactory factory = buildSessionFactory(bundle, dbConfig, provider, dbConfig.getProperties(), entities);
    final SessionFactoryManager managedFactory = new SessionFactoryManager(factory, dataSource);
    environment.lifecycle().manage(managedFactory);
    return factory;
}
Also used : SessionFactory(org.hibernate.SessionFactory) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

Aggregations

ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)19 Test (org.junit.Test)11 Connection (java.sql.Connection)8 Properties (java.util.Properties)5 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)3 Map (java.util.Map)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 Configurable (org.hibernate.service.spi.Configurable)3 Startable (org.hibernate.service.spi.Startable)3 Stoppable (org.hibernate.service.spi.Stoppable)3 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)3 TestForIssue (org.hibernate.testing.TestForIssue)3 Method (java.lang.reflect.Method)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 HibernateException (org.hibernate.HibernateException)2