Search in sources :

Example 1 with CommandAcceptanceException

use of org.hibernate.tool.schema.spi.CommandAcceptanceException in project hibernate-orm by hibernate.

the class AbstractScriptTargetOutput method accept.

@Override
public void accept(String command) {
    try {
        writer().write(command);
        writer().write(System.lineSeparator());
        writer().flush();
    } catch (IOException e) {
        throw new CommandAcceptanceException("Could not write to target script file", e);
    }
}
Also used : IOException(java.io.IOException) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException)

Example 2 with CommandAcceptanceException

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

Example 3 with CommandAcceptanceException

use of org.hibernate.tool.schema.spi.CommandAcceptanceException in project hibernate-orm by hibernate.

the class GenerationTargetToDatabase method accept.

@Override
public void accept(String command) {
    ddlTransactionIsolator.getJdbcContext().getSqlStatementLogger().logStatement(command, DDLFormatterImpl.INSTANCE);
    try {
        final Statement jdbcStatement = jdbcStatement();
        jdbcStatement.execute(command);
        try {
            SQLWarning warnings = jdbcStatement.getWarnings();
            if (warnings != null) {
                ddlTransactionIsolator.getJdbcContext().getSqlExceptionHelper().logAndClearWarnings(jdbcStatement);
            }
        } catch (SQLException e) {
            log.unableToLogSqlWarnings(e);
        }
    } catch (SQLException e) {
        throw new CommandAcceptanceException("Error executing DDL via JDBC Statement", e);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException)

Aggregations

CommandAcceptanceException (org.hibernate.tool.schema.spi.CommandAcceptanceException)3 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 SQLWarning (java.sql.SQLWarning)1 Statement (java.sql.Statement)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 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