1.SqlSessionFactory
每个ibatis应用都应该只有一个SqlSessionFactory的实例对象,所以一般设置为static属性或者使用spring管理时返回singleton类型,与spring集成时其实也是写一个怎样构建SqlSessionFactory的Bean,
构建SqlSessionFactory一般是SqlSessionFactoryBuild通过读取ibatis的配置文件而进行build的:
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuild().build(reader);
2.SqlSession
用于执行sql命令,获取Mappers并管理事务.
可以从SqlSessionFactory得到SqlSession: sessionFactory.openSession();
SqlSession是一切Sql相关数据库操作的中心,insert,select,update,delete...
SqlSession不是线程安全的(也就是有状态的),所以它的作用域最好是在一个Thread下,每个Thread有自己的SqlSession对象实例,彼此不相关.
Never keep references to a SqlSession instance in a static field or even an instance field of a class. Never keep references to a
SqlSession in any sort of managed scope, such as HttpSession of of the Servlet framework.
默认sessionFacory.openSession()拿到的SqlSession不是自动commit的,所以如果是更新操作必须自己执行session.commit()
关闭SqlSession很重要,必须保证在线程结束时关闭这个SqlSession,可以在finally中
session.close();
那跟Spring集成是怎样做到这一点的呢,因为dataSource是由spring管理的,所以他可以保证在一个Thread的每个方法中拿到的Connection是同一个对象,
虽然每个方法从sessionFactory.openSession()拿到的SqlSession对象是不同的,但是sqlSession对象中的connection是相同的,所以spring就可以在service层的方法结束之前将这个connection commit跟close,这样就实现了事务控制.
我们往往在dao层是一个方法对应一个sql语句的,不在这里控制事务,控制事务应该在service层, dao的每个方法拿到的sqlsession对象都是不相同的(尽管它的connection可能相同).
那我们应该怎样在没有spring的情况下实现ibatis的事务控制呢?还要保持dao的结构,以保持能跟spring随时切换?
看来ThreadLocal要派上用场了
SqlSessionManager实现了SqlSessionFactory, SqlSession接口.
类中的实例变量如下:
private final SqlSessionFactory sqlSessionFactory;
private final SqlSession sqlSessionProxy;
private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>();