SqlSessionFactory

SqlSessionFactory has six methods that are used to create SqlSession instances. In general, the decisions you’ll be making when selecting one of these methods are:

  • Transaction: Do you want to use a transaction scope for the session, or use auto-commit (usually means no transaction with most databases and/or JDBC drivers)?
  • Connection: Do you want MyBatis to acquire a Connection from the configured DataSource for you, or do you want to provide your own?
  • Execution: Do you want MyBatis to reuse PreparedStatements and/or batch updates (including inserts and deletes)?

The set of overloaded openSession() method signatures allow you to choose any combination of these options that makes sense.

  1. SqlSession openSession()
  2. SqlSession openSession(boolean autoCommit)
  3. SqlSession openSession(Connection connection)
  4. SqlSession openSession(TransactionIsolationLevel level)
  5. SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level)
  6. SqlSession openSession(ExecutorType execType)
  7. SqlSession openSession(ExecutorType execType, boolean autoCommit)
  8. SqlSession openSession(ExecutorType execType, Connection connection)
  9. Configuration getConfiguration();

The default openSession() method that takes no parameters will create a SqlSession with the following characteristics:

  • A transaction scope will be started (i.e. NOT auto-commit).
  • A Connection object will be acquired from the DataSource instance configured by the active environment.
  • The transaction isolation level will be the default used by the driver or data source.
  • No PreparedStatements will be reused, and no updates will be batched.

Most of the methods are pretty self explanatory. To enable auto-commit, pass a value of true to the optional autoCommit parameter. To provide your own connection, pass an instance of Connection to the connection parameter. Note that there’s no override to set both the Connection and autoCommit, because MyBatis will use whatever setting the provided connection object is currently using. MyBatis uses a Java enumeration wrapper for transaction isolation levels, called TransactionIsolationLevel, but otherwise they work as expected and have the 5 levels supported by JDBC (NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE).

The one parameter that might be new to you is ExecutorType. This enumeration defines 3 values:

  • ExecutorType.SIMPLE: This type of executor does nothing special. It creates a new PreparedStatement for each execution of a statement.
  • ExecutorType.REUSE: This type of executor will reuse PreparedStatements.
  • ExecutorType.BATCH: This executor will batch all update statements and demarcate them as necessary if SELECTs are executed between them, to ensure an easy-to-understand behavior.

NOTE There’s one more method on the SqlSessionFactory that we didn’t mention, and that is getConfiguration(). This method will return an instance of Configuration that you can use to introspect upon the MyBatis configuration at runtime.

NOTE If you’ve used a previous version of MyBatis, you’ll recall that sessions, transactions and batches were all something separate. This is no longer the case. All three are neatly contained within the scope of a session. You need not deal with transactions or batches separately to get the full benefit of them.