Search in sources :

Example 51 with SqlSessionFactory

use of org.apache.ibatis.session.SqlSessionFactory in project Activiti by Activiti.

the class ProcessEngineInitializationTest method testVersionMismatch.

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();
    // 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();
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ActivitiWrongDbException(org.activiti.engine.ActivitiWrongDbException) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession) SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) ActivitiException(org.activiti.engine.ActivitiException) ActivitiWrongDbException(org.activiti.engine.ActivitiWrongDbException) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession) DbSqlSessionFactory(org.activiti.engine.impl.db.DbSqlSessionFactory) DbSqlSessionFactory(org.activiti.engine.impl.db.DbSqlSessionFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl)

Example 52 with SqlSessionFactory

use of org.apache.ibatis.session.SqlSessionFactory in project Movie by batsqd.

the class TestMybatis method main.

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    String resource = "SqlMapConfig.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    MovieMapper movieMapper = sqlSession.getMapper(MovieMapper.class);
    Movie movie = movieMapper.selectMovieById(3);
    System.out.println(movie.toString());
}
Also used : Movie(com.po.Movie) SqlSession(org.apache.ibatis.session.SqlSession) InputStream(java.io.InputStream) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) MovieMapper(com.mapper.MovieMapper)

Aggregations

SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)52 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)42 Reader (java.io.Reader)39 Test (org.junit.Test)30 SqlSession (org.apache.ibatis.session.SqlSession)26 Connection (java.sql.Connection)18 Configuration (org.apache.ibatis.session.Configuration)13 Properties (java.util.Properties)8 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)4 HashMap (java.util.HashMap)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)3 InputStream (java.io.InputStream)2 List (java.util.List)2 DbSqlSessionFactory (org.activiti.engine.impl.db.DbSqlSessionFactory)2 Before (org.junit.Before)2 MovieMapper (com.mapper.MovieMapper)1 Movie (com.po.Movie)1 StringReader (java.io.StringReader)1 Statement (java.sql.Statement)1