org.apache.ibatis.session.ExecutorType Java Examples

The following examples show how to use org.apache.ibatis.session.ExecutorType. 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 File: BatchKeysTest.java    From mybaties with 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 #2
Source File: FlushStatementNpeTest.java    From mybaties with 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 #3
Source File: BatchKeysTest.java    From mybatis with 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 #4
Source File: FlushStatementNpeTest.java    From mybaties with 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 File: FlushStatementNpeTest.java    From mybaties with 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 #6
Source File: BaseDb.java    From live-chat-engine with 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 #7
Source File: ForceFlushOnSelectTest.java    From mybaties with 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 #8
Source File: NoParamTypeTest.java    From mybatis with 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 #9
Source File: FlushStatementNpeTest.java    From mybatis with 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 #10
Source File: FlushStatementNpeTest.java    From mybatis with 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 #11
Source File: InsertTest.java    From mybaties with 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 #12
Source File: NoParamTypeTest.java    From mybaties with 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 #13
Source File: DefaultSqlSessionFactory.java    From mybaties with 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 #14
Source File: DbTestsApplication.java    From java-persistence-frameworks-comparison with MIT License 6 votes vote down vote up
@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 #15
Source File: ForceFlushOnSelectTest.java    From mybatis with 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 #16
Source File: FlushStatementNpeTest.java    From mybatis with 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 #17
Source File: ForceFlushOnSelectTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectShouldFlushLocalCacheIfFlushLocalCacheAtferEachStatementIsTrue() throws SQLException {
  sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.STATEMENT);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllNoFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllFlush();
    assertEquals("Simone", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #18
Source File: ApiBootMyBatisEnhanceAutoConfiguration.java    From beihu-boot with Apache License 2.0 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 #19
Source File: MapperAutoConfiguration.java    From Mapper with 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 #20
Source File: ForceFlushOnSelectTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFlushLocalSessionCacheOnQueryForList() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllNoFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllNoFlush();
    assertEquals("John", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #21
Source File: ForceFlushOnSelectTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldFlushLocalSessionCacheOnQueryForList() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllFlush();
    assertEquals("Simone", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #22
Source File: ResultLoader.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Executor newExecutor() {
  final Environment environment = configuration.getEnvironment();
  if (environment == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  Environment was not configured.");
  }
  final DataSource ds = environment.getDataSource();
  if (ds == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  DataSource was not configured.");
  }
  final TransactionFactory transactionFactory = environment.getTransactionFactory();
  final Transaction tx = transactionFactory.newTransaction(ds, null, false);
  //如果executor已经被关闭了,则创建一个新的SimpleExecutor
  return configuration.newExecutor(tx, ExecutorType.SIMPLE);
}
 
Example #23
Source File: MapperAutoConfiguration.java    From mapper-boot-starter with 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 #24
Source File: ForceFlushOnSelectTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFlushLocalSessionCacheOnQuery() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    personMapper.selectByIdNoFlush(1);
    updateDatabase(sqlSession.getConnection());
    Person updatedPerson = personMapper.selectByIdNoFlush(1);
    assertEquals("John", updatedPerson.getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #25
Source File: ForceFlushOnSelectTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectShouldFlushLocalCacheIfFlushLocalCacheAtferEachStatementIsTrue() throws SQLException {
  sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.STATEMENT);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllNoFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllFlush();
    assertEquals("Simone", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #26
Source File: ForceFlushOnSelectTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFlushLocalSessionCacheOnQueryForList() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllNoFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllNoFlush();
    assertEquals("John", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #27
Source File: ForceFlushOnSelectTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldFlushLocalSessionCacheOnQueryForList() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> people = personMapper.selectAllFlush();
    updateDatabase(sqlSession.getConnection());
    people = personMapper.selectAllFlush();
    assertEquals("Simone", people.get(0).getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #28
Source File: ForceFlushOnSelectTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFlushLocalSessionCacheOnQuery() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    personMapper.selectByIdNoFlush(1);
    updateDatabase(sqlSession.getConnection());
    Person updatedPerson = personMapper.selectByIdNoFlush(1);
    assertEquals("John", updatedPerson.getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #29
Source File: ForceFlushOnSelectTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldFlushLocalSessionCacheOnQuery() throws SQLException {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    personMapper.selectByIdFlush(1);
    updateDatabase(sqlSession.getConnection());
    Person updatedPerson = personMapper.selectByIdFlush(1);
    assertEquals("Simone", updatedPerson.getFirstName());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}
 
Example #30
Source File: OrderPrefixRemoved.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testOrderPrefixNotRemoved() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
  try {
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

    Person person = personMapper.select(new String("slow"));

    assertNotNull(person);
    
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }
}