Search in sources :

Example 11 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 12 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(properties);
        if (Startable.class.isInstance(provider)) {
            ((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 13 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(properties);
        if (Startable.class.isInstance(provider)) {
            ((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 14 with ConnectionProvider

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

the class BaseTransactionIsolationConfigTest method testSettingIsolationAsNumeric.

@Test
public void testSettingIsolationAsNumeric() throws Exception {
    Properties properties = Environment.getProperties();
    augmentConfigurationSettings(properties);
    properties.put(AvailableSettings.ISOLATION, Connection.TRANSACTION_SERIALIZABLE);
    ConnectionProvider provider = getConnectionProviderUnderTest();
    try {
        ((Configurable) provider).configure(properties);
        if (Startable.class.isInstance(provider)) {
            ((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 15 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)

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