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();

Source Link

Document

Discards pending batch statements and rolls database connection back.

Usage

From source file:net.landora.animeinfo.data.AnimeDBA.java

License:Open Source License

public static byte[] getAnimePicture(String filename) {
    SqlSession session = null;

    try {//from w w w.j av  a  2 s .c o m
        session = AnimeDataManager.getInstance().openSession();
        AnimeMapper mapper = session.getMapper(AnimeMapper.class);

        Map map = mapper.getAnimePicture(filename);
        if (map == null) {
            return null;
        } else {
            return (byte[]) map.get("image_data");
        }
    } catch (Exception e) {
        session.rollback();
        log.error("Error getting anime picture.", e);
        return null;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.animeinfo.data.AnimeDBA.java

License:Open Source License

public static void saveAnimePicture(String filename, byte[] data) {
    SqlSession session = null;

    try {//ww w  . ja va2 s .c o m
        session = AnimeDataManager.getInstance().openSession();
        AnimeMapper mapper = session.getMapper(AnimeMapper.class);

        mapper.insertAnimePicture(filename, data);

        session.commit();
    } catch (Exception e) {
        session.rollback();
        log.error("Error saving anime picture.", e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.video.filestate.data.SharedDirectoryDBA.java

License:Open Source License

public static void saveSharedDirectory(SharedDirectory dir) {
    SqlSession session = null;

    try {/*  w  w w.  j  a  v  a 2 s .  c  o m*/
        session = FileStateDataManager.getInstance().openSession();
        SharedDirectoryMapper mapper = session.getMapper(SharedDirectoryMapper.class);

        if (dir.getDirectoryId() < 1) {
            mapper.insertSharedDirectory(dir);
        } else {
            mapper.updateSharedDirectory(dir);
        }

        session.commit();
    } catch (Exception e) {
        log.error("Error saving SharedDirectory.", e);

        if (session != null) {
            session.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.video.filestate.data.SharedDirectoryDBA.java

License:Open Source License

public static void saveFileRecord(FileRecord file) {
    SqlSession session = null;

    try {//  w w  w  .  j  a  v  a 2  s  .co  m
        session = FileStateDataManager.getInstance().openSession();
        SharedDirectoryMapper mapper = session.getMapper(SharedDirectoryMapper.class);

        if (file.getFileId() < 1) {
            mapper.insertFileRecord(file);
        } else {
            mapper.updateFileRecord(file);
        }

        session.commit();
    } catch (Exception e) {
        log.error("Error saving FileRecord.", e);

        if (session != null) {
            session.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.video.filestate.data.SharedDirectoryDBA.java

License:Open Source License

public static void deleteFileRecord(FileRecord file) {
    SqlSession session = null;

    try {//  ww w  .ja  v  a 2  s  .  c o  m
        session = FileStateDataManager.getInstance().openSession();
        SharedDirectoryMapper mapper = session.getMapper(SharedDirectoryMapper.class);

        mapper.deleteFileRecord(file);

        session.commit();
    } catch (Exception e) {
        log.error("Error deleting FileRecord.", e);

        if (session != null) {
            session.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.video.preferences.PreferencesDBA.java

License:Open Source License

public static void setExtensions(Collection<String> extensions) {
    SqlSession session = null;

    try {/*from  ww w .  j av a2  s  . c om*/
        session = PreferencesDataManager.getInstance().openSession();
        PreferencesMapper mapper = session.getMapper(PreferencesMapper.class);

        mapper.deleteVideoExtensions();
        for (String extension : extensions) {
            mapper.insertVideoExtension(extension);
        }

        session.commit();
    } catch (Exception e) {
        log.error("Error saving extensions.", e);
        session.rollback();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:net.landora.video.preferences.PreferencesDBA.java

License:Open Source License

public static void setPreferenceValue(String content, String prefName, String prefValue) {
    SqlSession session = null;

    try {//from ww w. j a v a  2 s.c o  m
        session = PreferencesDataManager.getInstance().openSession();
        PreferencesMapper mapper = session.getMapper(PreferencesMapper.class);

        if (mapper.updatePreference(content, prefName, prefValue) == 0) {
            mapper.insertPreference(content, prefName, prefValue);
        }

        session.commit();
    } catch (Exception e) {
        log.error("Error saving preferences.", e);
        session.rollback();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.activiti.standalone.initialization.ProcessEngineInitializationTest.java

License:Apache License

public void testVersionMismatch() {
    // first create the schema
    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngineConfiguration
            .createProcessEngineConfigurationFromResource(
                    "org/activiti/standalone/initialization/notables.activiti.cfg.xml")
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP)
            .buildProcessEngine();//from w  w w  . ja v a  2s  .co m

    // then update the version to something that is different to the library
    // version
    DbSqlSessionFactory dbSqlSessionFactory = (DbSqlSessionFactory) processEngine
            .getProcessEngineConfiguration().getSessionFactories().get(DbSqlSession.class);
    SqlSessionFactory sqlSessionFactory = dbSqlSessionFactory.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", 1);
        parameters.put("newRevision", 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();
        } else {
            sqlSession.rollback();
        }
        sqlSession.close();
    }

    try {
        // now we can see what happens if when a process engine is being
        // build with a version mismatch between library and db tables
        ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource(
                        "org/activiti/standalone/initialization/notables.activiti.cfg.xml")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
                .buildProcessEngine();

        fail("expected exception");
    } catch (ActivitiWrongDbException e) {
        assertTextPresent("version mismatch", e.getMessage());
        assertEquals("25.7", e.getDbVersion());
        assertEquals(ProcessEngine.VERSION, e.getLibraryVersion());
    }

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

From source file:org.activiti5.standalone.initialization.ProcessEngineInitializationTest.java

License:Apache License

public void testVersionMismatch() {
    // first create the schema
    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngineConfiguration
            .createProcessEngineConfigurationFromResource(
                    "org/activiti5/standalone/initialization/notables.activiti.cfg.xml")
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP)
            .buildProcessEngine();//from  www .  j  a v  a 2  s  .c  o m

    // then update the version to something that is different to the library
    // version
    DbSqlSessionFactory dbSqlSessionFactory = (DbSqlSessionFactory) processEngine
            .getProcessEngineConfiguration().getSessionFactories().get(DbSqlSession.class);
    SqlSessionFactory sqlSessionFactory = dbSqlSessionFactory.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", 1);
        parameters.put("newRevision", 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();
        } else {
            sqlSession.rollback();
        }
        sqlSession.close();
    }

    try {
        // now we can see what happens if when a process engine is being
        // build with a version mismatch between library and db tables
        ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource(
                        "org/activiti5/standalone/initialization/notables.activiti.cfg.xml")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
                .buildProcessEngine();

        fail("expected exception");
    } catch (ActivitiWrongDbException e) {
        assertTextPresent("version mismatch", e.getMessage());
        assertEquals("25.7", e.getDbVersion());
        assertEquals(ProcessEngine.VERSION, e.getLibraryVersion());
    }

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

From source file:org.apache.camel.component.mybatis.DefaultMyBatisProcessingStrategy.java

License:Apache License

public void commit(MyBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatements)
        throws Exception {
    SqlSession session = endpoint.getSqlSessionFactory().openSession();
    String[] statements = consumeStatements.split(",");
    try {/*  www.ja va 2  s  .c o m*/
        for (String statement : statements) {
            session.update(statement.trim(), data);
        }
        session.commit();
    } catch (Exception e) {
        session.rollback();
        throw e;
    } finally {
        session.close();
    }
}