Mybatis Can I reuse a session after commit?

My question is simple. Is it possible to reuse a session after a transaction like this?

try (SqlSession session = sqlSessionFactory.openSession()) {
// following 3 lines pseudocode for "doing some work"
  session.insert(...);
  session.update(...);
  session.delete(...);
  session.commit();

  session.insert();
  session.commit();
}

Or is it better to close the session after committing and open a new session?

+4
source share
1 answer

I am just starting out with MyBatis and I had a similar question about session management. Here is what I have learned so far:

  • Sessions should be opened as long as it is necessary to complete your transactions, and then closed.
  • MyBatis , . , , , , , . (MyBatis .)

. MyBatis DAO .

: https://mybatis.imtqy.com/mybatis-3/java-api.html#sqlSessions

+2

Source: https://habr.com/ru/post/1608087/


All Articles