Search in sources :

Example 36 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() {
    try (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 37 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 38 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());
            }
            String existingUrl = (String) Environment.getProperties().get(AvailableSettings.URL);
            if (existingUrl.indexOf('?') == -1) {
                existingUrl += "?";
            } else {
                existingUrl += "&";
            }
            StandardServiceRegistry ssr2 = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.URL, existingUrl + "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 with 1 increment by 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.orm.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 39 with ConnectionProvider

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

the class BaseTransactionIsolationConfigTest method testSettingIsolationAsNumericString.

@Test
public void testSettingIsolationAsNumericString() throws Exception {
    Properties properties = Environment.getProperties();
    augmentConfigurationSettings(properties);
    properties.put(AvailableSettings.ISOLATION, Integer.toString(Connection.TRANSACTION_SERIALIZABLE));
    ConnectionProvider provider = getConnectionProviderUnderTest();
    try {
        ((Configurable) provider).configure(PropertiesHelper.map(properties));
        if (provider instanceof Startable) {
            ((Startable) provider).start();
        }
        Connection connection = provider.getConnection();
        assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());
        provider.closeConnection(connection);
    } finally {
        ((Stoppable) provider).stop();
    }
}
Also used : Startable(org.hibernate.service.spi.Startable) Connection(java.sql.Connection) Stoppable(org.hibernate.service.spi.Stoppable) Configurable(org.hibernate.service.spi.Configurable) Properties(java.util.Properties) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 40 with ConnectionProvider

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

the class BaseTransactionIsolationConfigTest method testSettingIsolationAsNameAlt.

@Test
public void testSettingIsolationAsNameAlt() throws Exception {
    Properties properties = Environment.getProperties();
    augmentConfigurationSettings(properties);
    properties.put(AvailableSettings.ISOLATION, "SERIALIZABLE");
    ConnectionProvider provider = getConnectionProviderUnderTest();
    try {
        ((Configurable) provider).configure(PropertiesHelper.map(properties));
        if (provider instanceof Startable) {
            ((Startable) provider).start();
        }
        Connection connection = provider.getConnection();
        assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());
        provider.closeConnection(connection);
    } finally {
        ((Stoppable) provider).stop();
    }
}
Also used : Startable(org.hibernate.service.spi.Startable) Connection(java.sql.Connection) Stoppable(org.hibernate.service.spi.Stoppable) Configurable(org.hibernate.service.spi.Configurable) Properties(java.util.Properties) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) 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