Search in sources :

Example 26 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 27 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 28 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)

Example 29 with ConnectionProvider

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

the class MockConnectionProviderTest method testGetConnection.

@Test
public void testGetConnection() throws Exception {
    assertNotNull(MockConnectionProvider.CONNECTION);
    assertNotNull(MockConnectionProvider.DATABASE_META_DATA);
    assertNotNull(MockConnectionProvider.EMPTY_RESULT_SET);
    ConnectionProvider connectionProvider = new MockConnectionProvider();
    Connection connection = connectionProvider.getConnection();
    assertSame(MockConnectionProvider.CONNECTION, connection);
    assertSame(MockConnectionProvider.DATABASE_META_DATA, connection.getMetaData());
    assertSame(MockConnectionProvider.EMPTY_RESULT_SET, connection.getMetaData().getTypeInfo());
}
Also used : Connection(java.sql.Connection) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.jupiter.api.Test)

Example 30 with ConnectionProvider

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

the class MockConnectionProviderTest method testGetConnection.

@Test
public void testGetConnection() throws Exception {
    assertNotNull(MockConnectionProvider.CONNECTION);
    assertNotNull(MockConnectionProvider.DATABASE_META_DATA);
    assertNotNull(MockConnectionProvider.EMPTY_RESULT_SET);
    assertNotNull(MockConnectionProvider.MOCK_STATEMENT);
    ConnectionProvider connectionProvider = new MockConnectionProvider();
    Connection connection = connectionProvider.getConnection();
    assertSame(MockConnectionProvider.CONNECTION, connection);
    assertSame(MockConnectionProvider.DATABASE_META_DATA, connection.getMetaData());
    assertSame(MockConnectionProvider.EMPTY_RESULT_SET, connection.getMetaData().getTypeInfo());
    assertSame(MockConnectionProvider.MOCK_STATEMENT, connection.createStatement());
}
Also used : Connection(java.sql.Connection) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.jupiter.api.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