List of usage examples for org.springframework.transaction.support DefaultTransactionDefinition setPropagationBehaviorName
public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException
From source file:org.mybatis.spring.transaction.SpringTransactionManagerTest.java
@Test public void shouldNoOpWithTx() throws Exception { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); SpringManagedTransactionFactory transactionFactory = new SpringManagedTransactionFactory(); SpringManagedTransaction transaction = (SpringManagedTransaction) transactionFactory .newTransaction(dataSource, null, false); transaction.getConnection();/*from w w w. jav a2 s . com*/ transaction.commit(); transaction.close(); assertEquals("should not call commit on Connection", 0, connection.getNumberCommits()); assertFalse("should not close the Connection", connection.isClosed()); txManager.commit(status); }
From source file:org.mybatis.spring.SqlSessionTemplateTest.java
@Test public void testWithTxRequired() { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); sqlSessionTemplate.getMapper(TestMapper.class).findTest(); txManager.commit(status);/* w ww .j av a 2 s .c o m*/ assertCommit(); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testWithTxSupports() { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_SUPPORTS"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); session.getMapper(TestMapper.class).findTest(); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.commit(status);/*from w w w.ja va2 s . c o m*/ // SUPPORTS should just activate tx synchronization but not commits assertNoCommit(); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testRollbackWithTxSupports() { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_SUPPORTS"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); session.getMapper(TestMapper.class).findTest(); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.rollback(status);//from w w w .j ava 2 s . c o m // SUPPORTS should just activate tx synchronization but not commits assertNoCommit(); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testWithTxRequired() { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); session.getMapper(TestMapper.class).findTest(); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.commit(status);//from w ww .ja va2 s . c o m assertCommit(); assertCommitSession(); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testWithJtaTxManager() { JtaTransactionManager jtaManager = new JtaTransactionManager(new MockUserTransaction()); DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = jtaManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); session.getMapper(TestMapper.class).findTest(); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); jtaManager.commit(status);/* w w w .j a v a 2 s.com*/ // assume a real JTA tx would enlist and commit the JDBC connection assertNoCommitJdbc(); assertCommitSession(); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test(expected = DataAccessException.class) public void testBatchInTxWithError() { setupBatchStatements();/*from w w w .ja v a 2s . c om*/ DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory, ExecutorType.BATCH, exceptionTranslator); session.getMapper(TestMapper.class).insertTest("test1"); session.getMapper(TestMapper.class).insertTest("test2"); session.update("org.mybatis.spring.TestMapper.insertFail"); session.getMapper(TestMapper.class).insertTest("test3"); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.commit(status); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testBatchInTx() { setupBatchStatements();//from www. java 2 s .c o m DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory, ExecutorType.BATCH, exceptionTranslator); session.getMapper(TestMapper.class).insertTest("test1"); session.getMapper(TestMapper.class).insertTest("test2"); session.getMapper(TestMapper.class).insertTest("test3"); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.commit(status); assertCommit(); assertSingleConnection(); assertExecuteCount(3); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testSqlSessionCommitWithTx() { DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(); txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED"); TransactionStatus status = txManager.getTransaction(txDef); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); session.getMapper(TestMapper.class).findTest(); // commit should no-op since there is an active transaction session.commit(true);/* w ww . j a v a 2s . c o m*/ SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.commit(status); // Connection should be committed once, but we explicitly called commit on the SqlSession, // so it should be committed twice assertEquals("should call commit on Connection", 1, connection.getNumberCommits()); assertEquals("should not call rollback on Connection", 0, connection.getNumberRollbacks()); assertEquals("should call commit on SqlSession", 2, executorInterceptor.getCommitCount()); assertEquals("should not call rollback on SqlSession", 0, executorInterceptor.getRollbackCount()); assertSingleConnection(); }
From source file:org.mybatis.spring.MyBatisSpringTest.java
@Test public void testChangeExecutorTypeInTxRequiresNew() throws Exception { try {/*from w w w . j av a 2s . c om*/ txManager.setDataSource(dataSource); TransactionStatus status = txManager.getTransaction(new DefaultTransactionDefinition()); session = SqlSessionUtils.getSqlSession(sqlSessionFactory); // start a new tx while the other is in progress DefaultTransactionDefinition txRequiresNew = new DefaultTransactionDefinition(); txRequiresNew.setPropagationBehaviorName("PROPAGATION_REQUIRES_NEW"); TransactionStatus status2 = txManager.getTransaction(txRequiresNew); SqlSession session2 = SqlSessionUtils.getSqlSession(sqlSessionFactory, ExecutorType.BATCH, exceptionTranslator); SqlSessionUtils.closeSqlSession(session2, sqlSessionFactory); txManager.rollback(status2); SqlSessionUtils.closeSqlSession(session, sqlSessionFactory); txManager.rollback(status); } finally { // reset the txManager; keep other tests from potentially failing txManager.setDataSource(dataSource); // null the connection since it was not used // this avoids failing in validateConnectionClosed() connection = null; } }