Example usage for org.apache.ibatis.session SqlSession rollback

List of usage examples for org.apache.ibatis.session SqlSession rollback

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession rollback.

Prototype

void rollback(boolean force);

Source Link

Document

Discards pending batch statements and rolls database connection back.

Usage

From source file:org.activiti.engine.impl.persistence.db.DbSqlSessionFactory.java

License:Apache License

public void dbSchemaCheckVersion() {
    /*/*from ww w.j  a  v  a2 s. c om*/
     * Not quite sure if this is the right setting? We do want multiple updates
     * to be batched for performance ...
     */
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    boolean success = false;
    try {
        String selectSchemaVersionStatement = mapStatement("selectDbSchemaVersion");
        String dbVersion = (String) sqlSession.selectOne(selectSchemaVersionStatement);
        if (!ProcessEngine.VERSION.equals(dbVersion)) {
            throw new ActivitiWrongDbException(ProcessEngine.VERSION, dbVersion);
        }

        success = true;

    } catch (Exception e) {
        String exceptionMessage = e.getMessage();
        if ((exceptionMessage.indexOf("Table") != -1) && (exceptionMessage.indexOf("not found") != -1)) {
            throw new ActivitiException(
                    "no activiti tables in db.  set property db.schema.strategy=create-drop in activiti.properties for automatic schema creation",
                    e);
        } else {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new ActivitiException("couldn't get db schema version", e);
            }
        }
    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema check successful");
}

From source file:org.activiti.engine.impl.persistence.db.DbSqlSessionFactory.java

License:Apache License

public static void executeSchemaResource(String operation, String databaseName,
        SqlSessionFactory sqlSessionFactory) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    boolean success = false;
    try {/*from   w ww  .  j  a va2s.  c o m*/
        Connection connection = sqlSession.getConnection();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        String resource = "org/activiti/db/" + operation + "/activiti." + databaseName + "." + operation
                + ".sql";
        InputStream inputStream = classLoader.getResourceAsStream(resource);
        if (inputStream == null) {
            throw new ActivitiException("resource '" + resource + "' is not available for creating the schema");
        }

        Exception exception = null;
        byte[] bytes = IoUtil.readInputStream(inputStream, resource);
        String ddlStatements = new String(bytes);
        StringTokenizer tokenizer = new StringTokenizer(ddlStatements, ";");
        while (tokenizer.hasMoreTokens()) {
            String ddlStatement = tokenizer.nextToken().trim();
            if (!ddlStatement.startsWith("#")) {
                Statement jdbcStatement = connection.createStatement();
                try {
                    log.finest("\n" + ddlStatement);
                    jdbcStatement.execute(ddlStatement);
                    jdbcStatement.close();
                } catch (Exception e) {
                    if (exception == null) {
                        exception = e;
                    }
                    log.log(Level.SEVERE, "problem during schema " + operation + ", statement '" + ddlStatement,
                            e);
                }
            }
        }

        if (exception != null) {
            throw exception;
        }

        success = true;

    } catch (Exception e) {
        throw new ActivitiException("couldn't create db schema", e);

    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema " + operation + " successful");
}

From source file:org.activiti.impl.db.Db.java

License:Apache License

public static void dbSchemaCheckVersion(SqlSessionFactory sqlSessionFactory) {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); // Not quite sure if this is the right setting? We do want multiple updates to be batched for performance ...
    boolean success = false;
    try {/*from w w  w  .j  a  v  a  2 s  .  c  o  m*/
        String dbVersion = (String) sqlSession.selectOne("selectDbSchemaVersion");
        if (!ProcessEngine.VERSION.equals(dbVersion)) {
            throw new ActivitiWrongDbException(ProcessEngine.VERSION, dbVersion);
        }

        success = true;

    } catch (Exception e) {
        String exceptionMessage = e.getMessage();
        if ((exceptionMessage.indexOf("Table") != -1) && (exceptionMessage.indexOf("not found") != -1)) {
            throw new ActivitiException(
                    "no activiti tables in db.  set property 'db.schema.strategy' to value 'create-drop' in activiti.properties for automatic schema creation",
                    e);
        } else {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new ActivitiException("couldn't get db schema version", e);
            }
        }
    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema check successful");
}

From source file:org.activiti.impl.db.Db.java

License:Apache License

public static void executeSchemaResource(String operation, SqlSessionFactory sqlSessionFactory) {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); // Not quite sure if this is the right setting? We do want multiple updates to be batched for performance ...
    boolean success = false;
    try {//from www .j ava 2  s . c om
        Connection connection = sqlSession.getConnection();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        String resource = "org/activiti/db/" + operation + "/activiti.h2." + operation + ".sql";
        InputStream inputStream = classLoader.getResourceAsStream(resource);
        if (inputStream == null) {
            throw new ActivitiException("resource '" + resource + "' is not available for creating the schema");
        }

        Exception exception = null;
        byte[] bytes = IoUtil.readInputStream(inputStream, resource);
        String ddlStatements = new String(bytes);
        StringTokenizer tokenizer = new StringTokenizer(ddlStatements, ";");
        while (tokenizer.hasMoreTokens()) {
            String ddlStatement = tokenizer.nextToken().trim();
            if (!ddlStatement.startsWith("#")) {
                Statement jdbcStatement = connection.createStatement();
                try {
                    log.fine("\n" + ddlStatement);
                    jdbcStatement.execute(ddlStatement);
                    jdbcStatement.close();
                } catch (Exception e) {
                    if (exception == null) {
                        exception = e;
                    }
                    log.log(Level.SEVERE, "problem during schema " + operation + ", statement '" + ddlStatement,
                            e);
                }
            }
        }

        if (exception != null) {
            throw exception;
        }

        success = true;

    } catch (Exception e) {
        throw new ActivitiException("couldn't create db schema", e);

    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema creation successful");
}

From source file:org.activiti.impl.persistence.IbatisPersistenceSessionFactory.java

License:Apache License

public void dbSchemaCheckVersion() {
    /*/*from   ww w  . j  a  va2s  .c  o m*/
     * Not quite sure if this is the right setting? We do want multiple updates
     * to be batched for performance ...
     */
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    boolean success = false;
    try {
        String dbVersion = (String) sqlSession.selectOne(statement("selectDbSchemaVersion"));
        if (!ProcessEngine.VERSION.equals(dbVersion)) {
            throw new ActivitiWrongDbException(ProcessEngine.VERSION, dbVersion);
        }

        success = true;

    } catch (Exception e) {
        String exceptionMessage = e.getMessage();
        if ((exceptionMessage.indexOf("Table") != -1) && (exceptionMessage.indexOf("not found") != -1)) {
            throw new ActivitiException(
                    "no activiti tables in db.  set property db.schema.strategy=create-drop in activiti.properties for automatic schema creation",
                    e);
        } else {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new ActivitiException("couldn't get db schema version", e);
            }
        }
    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema check successful");
}

From source file:org.activiti.impl.persistence.IbatisPersistenceSessionFactory.java

License:Apache License

public void executeSchemaResource(String operation, SqlSessionFactory sqlSessionFactory) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    boolean success = false;
    try {//from  w  w  w.  ja  va 2  s .  c om
        Connection connection = sqlSession.getConnection();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        String resource = "org/activiti/db/" + operation + "/activiti." + databaseName + "." + operation
                + ".sql";
        InputStream inputStream = classLoader.getResourceAsStream(resource);
        if (inputStream == null) {
            throw new ActivitiException("resource '" + resource + "' is not available for creating the schema");
        }

        Exception exception = null;
        byte[] bytes = IoUtil.readInputStream(inputStream, resource);
        String ddlStatements = new String(bytes);
        StringTokenizer tokenizer = new StringTokenizer(ddlStatements, ";");
        while (tokenizer.hasMoreTokens()) {
            String ddlStatement = tokenizer.nextToken().trim();
            if (!ddlStatement.startsWith("#")) {
                Statement jdbcStatement = connection.createStatement();
                try {
                    log.fine("\n" + ddlStatement);
                    jdbcStatement.execute(ddlStatement);
                    jdbcStatement.close();
                } catch (Exception e) {
                    if (exception == null) {
                        exception = e;
                    }
                    log.log(Level.SEVERE, "problem during schema " + operation + ", statement '" + ddlStatement,
                            e);
                }
            }
        }

        if (exception != null) {
            throw exception;
        }

        success = true;

    } catch (Exception e) {
        throw new ActivitiException("couldn't create db schema", e);

    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    log.fine("activiti db schema " + operation + " successful");
}

From source file:org.activiti.test.db.ProcessEngineInitializationTest.java

License:Apache License

@Test
public void testVersionMismatch() {
    // first create the schema
    ProcessEngineImpl processEngine = (ProcessEngineImpl) new DbProcessEngineBuilder()
            .configureFromPropertiesResource("org/activiti/test/db/activiti.properties")
            .setDbSchemaStrategy(DbSchemaStrategy.CREATE_DROP).buildProcessEngine();

    // then update the version to something that is different to the library
    // version//from   www . j  a v  a 2  s.co m
    PersistenceSessionFactory persistenceSessionFactory = processEngine.getPersistenceSessionFactory();
    SqlSessionFactory sqlSessionFactory = ((IbatisPersistenceSessionFactory) persistenceSessionFactory)
            .getSqlSessionFactory();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    boolean success = false;
    try {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("name", "schema.version");
        parameters.put("value", "25.7");
        parameters.put("revision", new Integer(1));
        parameters.put("newRevision", new Integer(2));
        sqlSession.update("updateProperty", parameters);
        success = true;
    } catch (Exception e) {
        throw new ActivitiException("couldn't update db schema version", e);
    } finally {
        if (success) {
            sqlSession.commit(true);
        } else {
            sqlSession.rollback(true);
        }
        sqlSession.close();
    }

    exception.expect(ActivitiWrongDbException.class);
    exception.expect(new TypeSafeMatcher<ActivitiWrongDbException>() {

        @Override
        public boolean matchesSafely(ActivitiWrongDbException e) {
            return e.getMessage().contains("version mismatch") && "25.7".equals(e.getDbVersion())
                    && ProcessEngine.VERSION == e.getLibraryVersion();
        }

        public void describeTo(Description description) {
            description.appendText("'version mismatch' with dbVersion=25.7 and libraryVersion=")
                    .appendValue(ProcessEngine.VERSION);
        }
    });

    // now we can see what happens if when a process engine is being
    // build with a version mismatch between library and db tables
    new DbProcessEngineBuilder().configureFromPropertiesResource("org/activiti/test/db/activiti.properties")
            .setDbSchemaStrategy(DbSchemaStrategy.CHECK_VERSION).buildProcessEngine();

    // closing the original process engine to drop the db tables
    processEngine.close();
}

From source file:org.jessma.dao.mybatis.MyBatisSessionMgr.java

License:Apache License

/**
 * //from  w w w  .ja v  a2  s. c om
 * 
 * 
 */
public final void rollback(boolean force) {
    SqlSession session = localSession.get();

    if (session != null)
        session.rollback(force);
}

From source file:org.mybatis.guice.session.DbSessionManager.java

License:Apache License

public void rollback(boolean force) {
    final SqlSession sqlSession = localSqlSession.get();
    if (sqlSession == null)
        throw new SqlSessionException("Error:  Cannot rollback.  No managed session is started.");
    sqlSession.rollback(force);
}