-
SqlSession creation process:
-
Use SqlSessionFactoryBuilder create SqlSessionFactory.
-
Use SqlSessionFactory create SqlSession.
-
SqlSessionFactoryBuilder
-
SqlSessionFactory is a builder, a typical factory pattern.
-
After creation no longer needed, because the tool is to create SqlSessionFactory class.
-
Scope: local variables
SqlSessionFactory
-
Similar DBCP connection pool. Creating should always existed, there is no reason to discard it or re-create another instance. Therefore, you should use the Singleton pattern or Static Singleton pattern is created.
-
Scope: The scope (Application) program runs created, the program shut down before release.
SqlSession
-
JDBC is similar to a Connection object. Therefore, each use should be shut down, so as to take advantage of the recovery to continue SqlSessionFactory, and SqlSession not thread-safe and can not be shared.
-
Scope: The method or request.
-
Try to use a finally block.