MyBatis (four): SqlSession factory class and its scope and life cycle

SqlSession factory class and its scope and life cycle

  • SqlSession creation process:

 

 

  1. Use SqlSessionFactoryBuilder create SqlSessionFactory.

  2. Use SqlSessionFactory create SqlSession.

  3. Sql Mapper.class SqlSession may be performed by database operation or directly input method SqlSession SqlMapper. Method () for database operations, the used to turn off.

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.

Guess you like

Origin www.cnblogs.com/renzhongpei/p/12571478.html