跟我一起学Mybatis之(三)----Scope and Lifecycle
更喜欢原味的英语,所以就用Scope and Lifecycle,没有翻译。下面是文档中关于几个重要的类实例的Scope and Lifecycle介绍,英语水平有限,仅仅大概理解一下:
- SqlSessionFactoryBuilder
This class can be instantiated, used and thrown away.There is no need to keep it around once you've created your SqlSessionFactory. Therefore the best scope for instances of SqlSessionFactoryBuilder is method scope (i.e. a local method variable). You can reuse the SqlSessionFactoryBuilder to build multiple SqlSessionFactory instances, but it's still best not to keep it round to ensure that all of the XML parsing resources are freed up for more important things.
上面是文档中的原话,我大概理解了一下:这个类可以被实例化,使用和抛出。一旦你创建了你的SqlSessionFactory 没有必要保持它存在。因此对于SqlSessionFactoryBuilder 实例的最好的范围是方法级别的范围(比如一个方法内的局部变量)。你可以再次使用SqlSessionFactoryBuilder 来创建多个SqlSessionFactory 的实例,但是最好不要保持它还存在以保证所有的XML转换器资源被释放以用于更加重要的事情。
因此SqlSessionFactoryBuilder实例的范围是一个方法内,其最好是被定义为一个方法级别的局部变量。
2. SqlSessionFactory
Once created, the SqlSessionFactory should exist for the duration of your application execution. There
should be little or no reason to ever dispose of it or recreate it. It's a best practice to not rebuild the
SqlSessionFactory multiple times in an application run. Doing so should be considered a “bad smell”.
Therefore the best scope of SqlSessionFactory is application scope. This can be achieved a number of
ways. The simplest is to use a Singleton pattern or Static Singleton pattern.
一旦被创建,SqlSessionFactory 应该在你的应用程序执行期间一直存在着。很少有理由设置没有理由释放或者重新创建它。不在一个应用程序里面创建两次SqlSessionFactory是最好的实践经验。做这个的时候应该考虑到有坏的结果的可能性。因此最好的SqlSessionFactory范围是应用程序级别的。这个可以有多重实现的方式。最简单的一个方法是使用单例模式或者使用静态单例模式。
因此SqlSessionFactory最好的scope是application级别的。
3. SqlSession
Each thread should have its own instance of SqlSession. Instances of SqlSession are not to be shared
每一个线程应该有一个自己的SqlSession实例。 SqlSession的实例不期望于被共享并且不是线程安
and are not thread safe. Therefore the best scope is request or method scope. Never keep references
全的。 因此最好的范围是请求或者方法级别的。 绝对不要在一个
to a SqlSession instance in a static field or even an instance field of a class. Never keep references
静态的字段甚至一个类的实例字段内保持一个SqlSession的引用。 绝对不要在任何一种
to a SqlSession in any sort of managed scope, such as HttpSession of of the Servlet framework. If
管理范围持有SqlSession的引用,比如Servlet框架中的HttpSession中。
you're using a web framework of any sort, consider the SqlSession to follow a similar scope to that
如果你正在使用任何种类的web框架,考虑Sqlsession的范围与一个HTTP请求的方位类似。
of an HTTP request. In other words, upon receiving an HTTP request, you can open a SqlSession,
也就是说,当接收到一个HTTP请求,你便可以打开一个SqlSession,
then upon returning the response, you can close it. Closing the session is very important. You should
然后当你返回相应的时候,你便可以关闭它了。关闭session是非常重要的。 你应该
always ensure that it's closed within a finally block. The following is the standard pattern for ensuring
总是保证在一个finally块中关闭它。 下面是一个保证SqlSession是关闭的一个标准
that SqlSessions are closed:
模型:
SqlSession session = sqlSessionFactory.openSession();
try {
// do work
} finally {
session.close();
}
Using this pattern consistently throughout your code will ensure that all database resources are
在你的代码中保持使用这种模型将保证所有的数据库资源是被正确的关闭的。
properly closed.
因此,SqlSession的范围是一个请求级别的,而且是线程不安全的。
4. Mapper Instances
Mappers are interfaces that you create to bind to your mapped statements. Instances of the mapper
interfaces are acquired from the SqlSession. As such, technically the broadest scope of any mapper
instance is the same as the SqlSession from which they were requested. However, the best scope for
mapper instances is method scope. That is, they should be requested within the method that they are
used, and then be discarded. They do not need to be closed explicitly. While it's not a problem to keep
them around throughout a request, similar to the SqlSession, you might find that managing too many
resources at this level will quickly get out of hand. Keep it simple, keep Mappers in the method scope.
The following example demonstrates this practice.
SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
// do work
} finally {
session.close();
}
简洁总结一下吧:上面说的Mapper的范围与创建Mapper的Session的范围应该是一样的,而且它应该是在一个方法内被请求创建,具体使用如上面的例子。
刚刚开始学习翻译英文文档,如果有不准确的地方请各位大牛支出改正。
上一篇: IPVS收发数据相关的速率计算