Java Code Examples for org.apache.ibatis.session.ExecutorType

The following examples show how to use org.apache.ibatis.session.ExecutorType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mybatis   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitSimple() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 2
Source Project: mybatis   Source File: BatchKeysTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 3
@Bean
@Qualifier("batch-operations")
@SuppressWarnings("SpringJavaAutowiringInspection")
public SqlSession myBatisBatchOperationsSession(DataSource dataSource) throws Exception {
    /*
        NOTE: Unfortunately, in MyBatis it's not possible to execute batch and non-batch operations in single SqlSession.
        To support this scenario, we have to create completely new SqlSessionFactoryBean and completely new
        SqlSession. Surprisingly, this does not necessarily mean that the batch and non-batch operations will be
        executed in different transactions (as we would expect) - we tested this configuration using scenario 8.
        and it turned out that the bot non-batch and batch operations were run using same connection and in same transaction.
        I guess this has something to do with how connection is obtained by MyBatis from dataSource...
    */

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));

    return new SqlSessionTemplate(sqlSessionFactoryBean.getObject(), ExecutorType.BATCH);
}
 
Example 4
Source Project: mybatis   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitReuse() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 5
Source Project: mybaties   Source File: DefaultSqlSessionFactory.java    License: Apache License 2.0 6 votes vote down vote up
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  Transaction tx = null;
  try {
    final Environment environment = configuration.getEnvironment();
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    //通过事务工厂来产生一个事务
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    //生成一个执行器(事务包含在执行器里)
    final Executor executor = configuration.newExecutor(tx, execType);
    //然后产生一个DefaultSqlSession
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    //如果打开事务出错,则关闭它
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    //最后清空错误上下文
    ErrorContext.instance().reset();
  }
}
 
Example 6
Source Project: mybaties   Source File: ForceFlushOnSelectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateShouldFlushLocalCache() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    Person person = personMapper.selectByIdNoFlush(1);
    person.setLastName("Perez"); //it is ignored in update
    personMapper.update(person);
    Person updatedPerson = personMapper.selectByIdNoFlush(1);
    assertEquals("Smith", updatedPerson.getLastName());
    assertNotSame(person, updatedPerson);
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example 7
Source Project: mybaties   Source File: NoParamTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 8
Source Project: mybaties   Source File: InsertTest.java    License: Apache License 2.0 6 votes vote down vote up
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    mapper.insertNameMapped(name);
    Name name2 = new Name();
    name2.setFirstName("Wilma");
    name2.setLastName("Flintstone");
    mapper.insertNameMapped(name2);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    assertNotNull(name.getId());
    assertNotNull(name2.getId());
    assertEquals(1, batchResults.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 9
Source Project: mybaties   Source File: BatchKeysTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 10
Source Project: mybatis   Source File: NoParamTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 11
Source Project: mybaties   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitSimple() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 12
Source Project: mybaties   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitReuse() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 13
Source Project: mybaties   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitBatch() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 14
Source Project: live-chat-engine   Source File: BaseDb.java    License: Apache License 2.0 6 votes vote down vote up
private CommitOnCloseSession openCommitOnCloseSession(boolean batch){
	
	ExecutorType executorType = batch? ExecutorType.BATCH : ExecutorType.SIMPLE;
	if( ! isSingleTxMode()){
		return new CommitOnCloseSession(sessionFactory.openSession(executorType));
	}

	//SINGLE CONN MODE
	Environment env = sessionFactory.getConfiguration().getEnvironment();
	DataSource ds = env.getDataSource();
	
	Connection conn = null;
	try {
		conn = getSingleOrNewConnection(ds);
	}catch (Exception e) {
		throw new IllegalStateException("can't get conneciton", e);
	}
	
	return new CommitOnCloseSession(sessionFactory.openSession(executorType, conn));


}
 
Example 15
Source Project: mybatis   Source File: FlushStatementNpeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSameUpdateAfterCommitBatch() {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    try {
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person = personMapper.selectById(1);
        person.setFirstName("Simone");
        
        // Execute first update then commit.
        personMapper.update(person);
        sqlSession.commit();
        
        // Execute same update a second time. This used to raise an NPE.
        personMapper.update(person);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}
 
Example 16
Source Project: mybatis   Source File: ForceFlushOnSelectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateShouldFlushLocalCache() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    Person person = personMapper.selectByIdNoFlush(1);
    person.setLastName("Perez"); //it is ignored in update
    personMapper.update(person);
    Person updatedPerson = personMapper.selectByIdNoFlush(1);
    assertEquals("Smith", updatedPerson.getLastName());
    assertNotSame(person, updatedPerson);
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example 17
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 18
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 19
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 20
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 21
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 22
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 23
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 24
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 25
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 26
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 27
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 28
private void settingsElement(XNode context) throws Exception {
    if (context != null) {
        Properties props = context.getChildrenAsProperties();
        // Check that all settings are known to the configuration class
        MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
        for (Object key : props.keySet()) {
            if (!metaConfig.hasSetter(String.valueOf(key))) {
                throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
            }
        }
        configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
        configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
        configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
        configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
        configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));
        configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
        configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
        configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
        configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
        configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
        configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
        configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
        configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
        configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
        configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
        configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
        configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
        configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
        configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
        configuration.setLogPrefix(props.getProperty("logPrefix"));
        configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
    }
}
 
Example 29
Source Project: mapper-boot-starter   Source File: MapperAutoConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 30
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
	ExecutorType executorType = this.properties.getExecutorType();
	if (executorType != null) {
		return new SqlSessionTemplate(sqlSessionFactory, executorType);
	} else {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}