/*
* Copyright 2010 The myBatis Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Test;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.mockrunner.mock.jdbc.MockDataSource;
/**
* @version $Id$
*/
public final class MyBatisSpringTest extends AbstractMyBatisSpringTest {
private SqlSession session;
@After
public void validateSessionClose() {
// assume if the Executor is closed, the Session is too
if ((session != null) && !executorInterceptor.isExecutorClosed()) {
session = null;
fail("SqlSession is not closed");
} else {
session = null;
}
}
// ensure MyBatis API still works with SpringManagedTransaction
@Test
public void testMyBatisAPI() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.close();
assertNoCommit();
assertSingleConnection();
}
@Test
public void testMyBatisAPIWithCommit() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.commit(true);
session.close();
assertCommit();
assertSingleConnection();
}
@Test
public void testMyBatisAPIWithRollback() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.rollback(true);
session.close();
assertRollback();
assertSingleConnection();
}
// basic tests using SqlSessionUtils instead of using the MyBatis API directly
@Test
public void testSpringAPI() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertNoCommit();
assertSingleConnection();
}
@Test
public void testSpringAPIWithCommit() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.commit(true);
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertCommit();
assertSingleConnection();
}
@Test
public void testSpringAPIWithRollback() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.rollback(true);
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertRollback();
assertSingleConnection();
}
@Test
public void testSpringAPIWithMyBatisClose() {
// This is a programming error and could lead to connection leak if there is a transaction
// in progress. But, the API allows it, so make sure it at least works without a tx.
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.close();
assertNoCommit();
assertSingleConnection();
}
@Test
public void testWithSameDataSource() {
// use the same DataSource the SqlSession is configured with
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertNoCommit();
assertSingleConnection();
}
// Spring API should work with a MyBatis TransactionFactories, as long as there is not a Spring
// TX is progress
@Test
public void testWithNonSpringTransactionFactory() {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
try {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
// users need to manually call commit, rollback and close, just like with normal MyBatis
// API usage
assertNoCommit();
assertSingleConnection();
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
@Test(expected = TransientDataAccessResourceException.class)
public void testNonSpringTxFactoryWithTx() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
TransactionStatus status = null;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
fail("should not be able to get an SqlSession using non-Spring tx manager when there is an active Spring tx");
} finally {
// rollback required to close connection
txManager.rollback(status);
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
// TODO should this pass?
/*
* this is an edge case - completely separate DataSource, non-Spring TXManager but with an
* existing Spring TX. Technically, this could be allowed, but the current implementation fails
* because DataSourceUtils.getConnection(DataSource) pulls _any_ Connection into the current tx.
* To fix, however, SqlSessionTemplate.execute() would need to run more checks
* (DataSourceUtils.isConnectionTransactional() and unwrapping TransactionAwareDataSourceProxy)
* which would increase the code path for all transactions.
*/
@Test(expected = TransientDataAccessResourceException.class)
public void testNonSpringTxFactoryNonSpringDSWithTx() {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
MockDataSource mockDataSource = new MockDataSource();
mockDataSource.setupConnection(createMockConnection());
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), mockDataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
TransactionStatus status = null;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
fail("should not be able to get an SqlSession using non-Spring tx manager when there is an active Spring tx");
} finally {
// rollback required to close connection
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
spring-mybatis-spring-1.0.0-RC3.tar.gz (65个子文件)
spring-mybatis-spring-1.0.0-RC3
pom.xml 9KB
src
docbkx
sample.xml 4KB
templates_support.xml 7KB
using_api.xml 2KB
introduction.xml 3KB
transactions.xml 4KB
mappers.xml 8KB
index.xml 2KB
getting_started.xml 5KB
factorybean.xml 5KB
changes
changes.xml 4KB
test
resources
log4j.properties 901B
org
mybatis
spring
sample
mapper
UserMapper.xml 1KB
applicationContext-infrastructure.xml 3KB
applicationContext-basic.xml 3KB
applicationContext-auto.xml 3KB
applicationContext-sqlsession.xml 3KB
mybatis-config.xml 1KB
applicationContext-batch.xml 3KB
db
database-test-data.sql 898B
database-schema.sql 833B
TestMapper.xml 1KB
mybatis-config.xml 1KB
java
org
mybatis
spring
TestMapper.java 744B
SqlSessionTemplateTest.java 6KB
MyBatisSpringTest.java 17KB
mapper
child
MapperChildInterface.java 970B
AnnotatedMapperZeroMethods.java 891B
MapperInterface.java 893B
MapperFactoryBeanTest.java 8KB
AnnotatedMapper.java 890B
MapperClass.java 810B
MapperSubinterface.java 865B
MapperScannerConfigurerTest.java 7KB
sample
SampleBatchTest.java 2KB
mapper
UserMapper.java 1KB
UserMapperImpl.java 1KB
SampleSqlSessionTest.java 2KB
SampleBasicTest.java 2KB
service
FooServiceImpl.java 1KB
FooService.java 1KB
SampleAutoTest.java 2KB
package-info.java 1KB
domain
User.java 1KB
AbstractMyBatisSpringTest.java 5KB
ExecutorInterceptor.java 3KB
SqlSessionFactoryBeanTest.java 7KB
PooledMockDataSource.java 2KB
main
java
org
mybatis
spring
SqlSessionTemplate.java 11KB
SqlSessionHolder.java 2KB
MyBatisExceptionTranslator.java 3KB
SqlSessionUtils.java 12KB
mapper
MapperScannerConfigurer.java 11KB
package-info.java 819B
MapperFactoryBean.java 4KB
SqlSessionFactoryBean.java 15KB
support
SqlSessionDaoSupport.java 2KB
package-info.java 745B
transaction
SpringManagedTransaction.java 5KB
package-info.java 768B
SpringManagedTransactionFactory.java 1KB
MyBatisSystemException.java 1KB
package-info.java 760B
LICENSE 11KB
NOTICE 1KB
共 65 条
- 1
资源评论
段子手-168
- 粉丝: 1550
- 资源: 1970
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功