Session

The org.mybatis.scala.session package provides service classes to execute your mapped statements against a database connection.

The main class in this package is SessionManager which its instances are provided by the configuration object. You should have only one instance of the SessionManager in your application. Just like SqlSessionFactory. This class is disconnected, it only create connections when you call a connected method.

readOnly

This method opens a connection, executes a code block, calls rollback and close.

manager.readOnly { implicit session =>

    val list = findUsers("a%")
    for (user <- list) {
        // Do something with user
    }

}

transaction

This method opens a connection, executes a code block, and calls commit if no exceptions are thrown, else calls rollback, then close.

manager.transaction { implicit session =>

    val user = new User
    user.name = "John"
    user.username = "john1"
    user.password = "12345"

    insertNewUser(user)

}

managed

This method opens a connection, executes a code block and closes the connection. The Transaction lifecycle must be managed externally by the container or manually by the developer.

manager.managed { implicit session =>

    val user = new User
    user.name = "John"
    user.username = "john1"
    user.password = "12345"

    insertNewUser(user)

}