title: MyBatis: how to bypass a local cache and directly hit the DB on specific select
body: <p>I use MyBatis 3.1. <br/>
  I have two use cases when I need to bypass MyBatis local cache and directly hit the DB. <br/>
  Since MyBatis configuration file only have global settings, it is not applicable to my case, because I need it as an exception, not as a default. Attributes of MyBatis &lt;select&gt; XML statement do not seem to include this option. <br/></p>
  
  <p><strong>Use case 1</strong>: 'select sysdate from dual'. <br/>
  MyBatis caching causes this one to always return the same value within a MyBatis session. This causes an issue in my integration test, when I try to replicate a situation of an outdated entry. <br/>
  My workaround was just to use a plain JDBC call. <br/></p>
  
  <p><strong>Use case 2</strong>: 'select' from one thread does not always see the value written by another thread. <br/>
  Thread 1: <br/></p>
  
  <pre><code>SomeObject stored = dao.insertSomeObject(obj);
  runInAnotherThread(stored.getId());
  //complete and commit
  </code></pre>
  
  <p>Thread 2: <br/></p>
  
  <pre><code>//'id' received as an argument provided to 'runInAnotherThread(...)'
  SomeObject stored = dao.findById(id);
  int count = 0;
  while(stored == null &amp;&amp; count &lt; 300) {
      ++count;
      Thread.sleep(1000);
      stored = dao.findById(id);
  }
  if (stored == null) {
      throw new MyException("There is no SomeObject with id="+id);
  }
  </code></pre>
  
  <p>I occasionally receive MyException errors on a server, but can't reproduce on my local machine. In all cases the object is always in the DB. So I guess the error depends on whether the stored object was in MyBatis local cache at the first time, and waiting for 5 minutes does not help, since it never checks the actual DB. </p>
  
  <p>So my question is how to solve the above use cases within MyBatis without falling back to the plain JDBC? <br/>
  Being able just to somehow signal MyBatis not to use a cached value in a specific call (the best) or in all calls to a specific query would be the preferred option, but I will consider any workaround as well.</p>