Search in sources :

Example 16 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class SchemaToolTransactionHandlingTest method testDropCreateDropInExistingJtaTransaction.

// for each case we want to run these tool delegates in a matrix of:
// 1) JTA versus JDBC transaction handling
// 2) existing transaction versus not
// 
// cases:
// 1) create-drop
// 2) update
// 3) validate
// 
// so:
// 1) create-drop
// 1.1) JTA transaction handling
// 1.1.1) inside an existing transaction
// 1.1.2) outside any transaction
// 1.1) JDBC transaction handling
// - there really cannot be an "existing transaction" case...
@Test
public void testDropCreateDropInExistingJtaTransaction() {
    // start a JTA transaction...
    try {
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    } catch (Exception e) {
        throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
    }
    // hold reference to Transaction object
    final Transaction jtaTransaction;
    try {
        jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
    } catch (SystemException e) {
        throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
    }
    final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
    // perform the test...
    try {
        final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
        final SchemaDropper schemaDropper = smt.getSchemaDropper(Collections.emptyMap());
        final SchemaCreator schemaCreator = smt.getSchemaCreator(Collections.emptyMap());
        final Metadata mappings = buildMappings(registry);
        try {
            try {
                schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (CommandAcceptanceException e) {
            // ignore may happen if sql drop does not support if exist
            }
            schemaCreator.doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
        } finally {
            try {
                schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (Exception ignore) {
            // ignore
            }
        }
    } finally {
        try {
            jtaTransaction.commit();
            ((StandardServiceRegistryImpl) registry).destroy();
        } catch (Exception e) {
        // not much we can do...
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SchemaCreator(org.hibernate.tool.schema.spi.SchemaCreator) Metadata(org.hibernate.boot.Metadata) SchemaDropper(org.hibernate.tool.schema.spi.SchemaDropper) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SystemException(javax.transaction.SystemException) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

StandardServiceRegistryImpl (org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)16 Test (org.junit.Test)10 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 Metadata (org.hibernate.boot.Metadata)4 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)4 Properties (java.util.Properties)3 SessionFactory (org.hibernate.SessionFactory)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 H2Dialect (org.hibernate.dialect.H2Dialect)3 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)3 ConnectionProviderJdbcConnectionAccess (org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess)3 File (java.io.File)2 Connection (java.sql.Connection)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 SystemException (javax.transaction.SystemException)2 Transaction (javax.transaction.Transaction)2 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)2 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 Configuration (org.hibernate.cfg.Configuration)2