Search in sources :

Example 1 with SchemaDropper

use of org.hibernate.tool.schema.spi.SchemaDropper 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

SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 SystemException (javax.transaction.SystemException)1 Transaction (javax.transaction.Transaction)1 Metadata (org.hibernate.boot.Metadata)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryImpl (org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)1 CommandAcceptanceException (org.hibernate.tool.schema.spi.CommandAcceptanceException)1 SchemaCreator (org.hibernate.tool.schema.spi.SchemaCreator)1 SchemaDropper (org.hibernate.tool.schema.spi.SchemaDropper)1 SchemaManagementTool (org.hibernate.tool.schema.spi.SchemaManagementTool)1 Test (org.junit.Test)1