Search in sources :

Example 1 with DbUpgradeStep

use of org.activiti.engine.impl.db.upgrade.DbUpgradeStep in project Activiti by Activiti.

the class DbSqlSession method executeSchemaResource.

private void executeSchemaResource(String operation, String component, String resourceName, InputStream inputStream) {
    log.info("performing {} on {} with resource {}", operation, component, resourceName);
    String sqlStatement = null;
    String exceptionSqlStatement = null;
    try {
        Connection connection = sqlSession.getConnection();
        Exception exception = null;
        byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
        String ddlStatements = new String(bytes);
        String databaseType = dbSqlSessionFactory.getDatabaseType();
        // Special DDL handling for certain databases
        try {
            if ("mysql".equals(databaseType)) {
                DatabaseMetaData databaseMetaData = connection.getMetaData();
                int majorVersion = databaseMetaData.getDatabaseMajorVersion();
                int minorVersion = databaseMetaData.getDatabaseMinorVersion();
                log.info("Found MySQL: majorVersion=" + majorVersion + " minorVersion=" + minorVersion);
                // Special care for MySQL < 5.6
                if (majorVersion <= 5 && minorVersion < 6) {
                    ddlStatements = updateDdlForMySqlVersionLowerThan56(ddlStatements);
                }
            }
        } catch (Exception e) {
            log.info("Could not get database metadata", e);
        }
        BufferedReader reader = new BufferedReader(new StringReader(ddlStatements));
        String line = readNextTrimmedLine(reader);
        boolean inOraclePlsqlBlock = false;
        while (line != null) {
            if (line.startsWith("# ")) {
                log.debug(line.substring(2));
            } else if (line.startsWith("-- ")) {
                log.debug(line.substring(3));
            } else if (line.startsWith("execute java ")) {
                String upgradestepClassName = line.substring(13).trim();
                DbUpgradeStep dbUpgradeStep = null;
                try {
                    dbUpgradeStep = (DbUpgradeStep) ReflectUtil.instantiate(upgradestepClassName);
                } catch (ActivitiException e) {
                    throw new ActivitiException("database update java class '" + upgradestepClassName + "' can't be instantiated: " + e.getMessage(), e);
                }
                try {
                    log.debug("executing upgrade step java class {}", upgradestepClassName);
                    dbUpgradeStep.execute(this);
                } catch (Exception e) {
                    throw new ActivitiException("error while executing database update java class '" + upgradestepClassName + "': " + e.getMessage(), e);
                }
            } else if (line.length() > 0) {
                if ("oracle".equals(databaseType) && line.startsWith("begin")) {
                    inOraclePlsqlBlock = true;
                    sqlStatement = addSqlStatementPiece(sqlStatement, line);
                } else if ((line.endsWith(";") && inOraclePlsqlBlock == false) || (line.startsWith("/") && inOraclePlsqlBlock == true)) {
                    if (inOraclePlsqlBlock) {
                        inOraclePlsqlBlock = false;
                    } else {
                        sqlStatement = addSqlStatementPiece(sqlStatement, line.substring(0, line.length() - 1));
                    }
                    Statement jdbcStatement = connection.createStatement();
                    try {
                        // no logging needed as the connection will log it
                        log.debug("SQL: {}", sqlStatement);
                        jdbcStatement.execute(sqlStatement);
                        jdbcStatement.close();
                    } catch (Exception e) {
                        if (exception == null) {
                            exception = e;
                            exceptionSqlStatement = sqlStatement;
                        }
                        log.error("problem during schema {}, statement {}", operation, sqlStatement, e);
                    } finally {
                        sqlStatement = null;
                    }
                } else {
                    sqlStatement = addSqlStatementPiece(sqlStatement, line);
                }
            }
            line = readNextTrimmedLine(reader);
        }
        if (exception != null) {
            throw exception;
        }
        log.debug("activiti db schema {} for component {} successful", operation, component);
    } catch (Exception e) {
        throw new ActivitiException("couldn't " + operation + " db schema: " + exceptionSqlStatement, e);
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Statement(java.sql.Statement) Connection(java.sql.Connection) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) DatabaseMetaData(java.sql.DatabaseMetaData) DbUpgradeStep(org.activiti.engine.impl.db.upgrade.DbUpgradeStep) ActivitiException(org.activiti.engine.ActivitiException) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) ActivitiWrongDbException(org.activiti.engine.ActivitiWrongDbException) IOException(java.io.IOException)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 Statement (java.sql.Statement)1 ActivitiException (org.activiti.engine.ActivitiException)1 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)1 ActivitiWrongDbException (org.activiti.engine.ActivitiWrongDbException)1 DbUpgradeStep (org.activiti.engine.impl.db.upgrade.DbUpgradeStep)1