title: How to not persist sqlSession.selectOne returned value?
body: <p>My goal is to not persist the SqlSession.selectOne returned value. The problem is, if you do a sqlSession.selectOne (using an API endpoint) and then you edit the database directly for example using the MySQL workbench. After you edit the database and do another API endpoint request, the returned value will not change.</p>
  <p>This is how you recreate the problem:</p>
  <ol>
  <li>Do get request to the endpoint. The returned value is false.</li>
  <li>Update the database so if you do a get request again, the returned value should be true. Instead it returned false.</li>
  </ol>
  <p>The only way to fix this is to restart the server, which is not feasible.</p>
  <p>This is how I do a sqlSession.selectOne:</p>
  <ol>
  <li>boolean value = sqlSession.selectOne(params);</li>
  <li>sqlSession.commit();</li>
  <li>return value;</li>
  </ol>
  <pre><code>@Repository
  public class Admin_LoginImpl {
    private SqlSession sqlSession;
  
    public Admin_LoginImpl() {
      Reader reader = null;
      try {
        reader = Resources.getResourceAsReader(&quot;mybatis-config.xml&quot;);
      } catch (IOException e) {
        e.printStackTrace();
      }
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
      sqlSession = sqlSessionFactory.openSession();
    }
  
    public void createTableIfNotExists() {
      sqlSession.update(&quot;Admin_Login.createTableIfNotExists&quot;);
      sqlSession.commit();
    }
  
    public boolean exist(String User_ID) {
      boolean isExist = sqlSession.selectOne(&quot;Admin_Login.exist&quot;, User_ID);
      sqlSession.commit();
      return isExist;
    }
  }
  </code></pre>
  <p>mybatis-config.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;!DOCTYPE configuration
          PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot;
          &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;
  &lt;configuration&gt;
      &lt;properties resource=&quot;application.properties&quot;/&gt;
      &lt;environments default=&quot;development&quot;&gt;
          &lt;environment id=&quot;development&quot;&gt;
              &lt;transactionManager type=&quot;JDBC&quot;/&gt;
              &lt;dataSource type=&quot;POOLED&quot;&gt;
                  &lt;property name=&quot;driver&quot; value=&quot;${spring.datasource.driver-class-name}&quot;/&gt;
                  &lt;property name=&quot;url&quot; value=&quot;${spring.datasource.url}&quot;/&gt;
                  &lt;property name=&quot;username&quot; value=&quot;${spring.datasource.username}&quot;/&gt;
                  &lt;property name=&quot;password&quot; value=&quot;${spring.datasource.password}&quot;/&gt;
              &lt;/dataSource&gt;
          &lt;/environment&gt;
      &lt;/environments&gt;
      &lt;mappers&gt;
          &lt;mapper resource=&quot;builder/User_LoginMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/User_InfoMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/User_DetailMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/TransactionsMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/Transactions_ProgressMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/StatementsMapper.xml&quot;/&gt;
  
          &lt;mapper resource=&quot;builder/admin/Admin_LoginMapper.xml&quot;/&gt;
          &lt;mapper resource=&quot;builder/admin/Admin_TransactionsMapper.xml&quot;/&gt;
      &lt;/mappers&gt;
  &lt;/configuration&gt;
  </code></pre>
  <p>build/admin/Admin_LogginMapper.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;!DOCTYPE mapper
          PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
          &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;
  &lt;mapper namespace=&quot;Admin_Login&quot;&gt;
      &lt;update id=&quot;createTableIfNotExists&quot;&gt;
          CREATE TABLE IF NOT EXISTS `admin_login` (
          `ID` int NOT NULL AUTO_INCREMENT,
          `User_ID` char(12) NOT NULL,
          PRIMARY KEY (`ID`),
          UNIQUE KEY `User_ID_UNIQUE` (`User_ID`)
          ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
      &lt;/update&gt;
      &lt;select id=&quot;exist&quot; parameterType=&quot;String&quot; resultType=&quot;boolean&quot;&gt;
          SELECT EXISTS(SELECT 1 FROM admin_login WHERE User_ID=#{User_ID})
      &lt;/select&gt;
  &lt;/mapper&gt;
  </code></pre>
  <p>What I've did:</p>
  <ol>
  <li>tried to do sqlSession.close(); // this will lead to error.</li>
  <li>tried to do sqlSession.clearCache(); // does not help.</li>
  </ol>
  <p>I can't afford to:</p>
  <ol>
  <li><strong>Remove the @Repository and manually create new object Admin_LoginImpl for every rest endpoint request. This will significantly increase the response time.</strong></li>
  </ol>