The different scopes and life cycle categories are critical because the wrong use can cause serious concurrency problems. The SqlSessionFactoryBuilder is:

  • This class can be instantiated, used, and discarded; once the SqlSessionFactory has been created, it is no longer needed.
  • A local variable

SqlSessionFactory:

  • In plain English, you can think of it as: database connection pooling
  • Once created, the SqlSessionFactory should exist for the duration of the application, and there is no reason to discard it or recreate another instance.
  • So the best scope for SqlSessionFactory is the application scope.
  • The simplest is to use singletons or static singletons.

SqlSession:

  • A request to connect to the connection pool
  • Instances of a SQLSession are not thread-safe and therefore cannot be shared, so its optimal scope is the request or method scope.
  • Must be closed after use, otherwise cause a large memory resource overhead!



Every mapper here represents every specific business!