title: Mybatis "Creating a new SqlSession" & "Closing non transactional SqlSession" for *every* database query
body: <p>Mybatis seems to create and close a <code>SqlSession</code> for <em>every database query</em> (e.g. if in one http request we do two database queries, then two sessions are created and destroyed). Is this normal (will it lead to poor performance), and how shall I fix it?</p>
  
  <p>Details:</p>
  
  <p>The logs -</p>
  
  <pre><code>Creating a new SqlSession
  SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b7e4174] was not registered for synchronization because synchronization is not active
  JDBC Connection [HikariProxyConnection@163878081 wrapping com.mysql.cj.jdbc.ConnectionImpl@162e1fc3] will not be managed by Spring
  Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b7e4174]
  Creating a new SqlSession
  SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5d9714ab] was not registered for synchronization because synchronization is not active
  JDBC Connection [HikariProxyConnection@91618474 wrapping com.mysql.cj.jdbc.ConnectionImpl@162e1fc3] will not be managed by Spring
  Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5d9714ab]
  </code></pre>
  
  <p>The codes -</p>
  
  <pre><code>Article a1 = dao.selectById(123);
  Article a2 = dao.selectById(456);
  </code></pre>
  
  <p>where the Dao is a normal Mybatis <code>Mapper</code>. (More specifically, it is a <a href="https://mp.baomidou.com/en/guide/quick-start.html" rel="nofollow noreferrer">mybatis plus</a> <code>BaseMapper</code>)</p>
  
  <p>Thanks for any ideas!</p>
  
  <hr>
  
  <p>P.S. My naive thoughts - </p>
  
  <p>The <a href="https://mybatis.org/mybatis-3/getting-started.html" rel="nofollow noreferrer">official doc</a> says - </p>
  
  <blockquote>
    <p>If you're using a web framework of any sort, ...upon receiving an HTTP request, you can open a SqlSession, then upon returning the response, you can close it...</p>
  </blockquote>
  
  <p>Thus, my situation is abnormal. Since in one http request with two database queries, I see <em>two</em> SqlSessions instead of one.</p>