title: Wildfly - Infinispan Transactions configuration
body: <p>I am using Wildfly 8.2 with its included Infinispan (6.0.2) and I am trying to cache all values from some Oracle database table in an Infispan cache. In most cases, it seems to work, but sometimes it does not. When accessing the <code>cache.values()</code> (which also may not be a good idea for performance, but is an example) it appears sometime to be empty, sometimes it contains the values correctly. Therefore I think it might a problem with the configuration of the transactions. When making the Infinispan cache non-transactional, the problem disappears.</p>
  
  <p>The service which accesses the cache and the DB is an EJB bean with container-managed transactions. On initialzation of the Service, all data is loaded from the DB (it does not contain many entries).
  According to <a href="https://blogs.oracle.com/arungupta/entry/what_s_new_in_ejb" rel="nofollow">what's new in ejb 3.2</a> it should be possible to access the DB transactionally in a EJB <code>Singleton</code> bean.</p>
  
  <p>Is the configuration of the data source and the Infinispan cache correct? Can I use a non-XA datasource with Infinispan and expect it work consistently? According to the <a href="https://github.com/infinispan/infinispan/wiki/Consistency-guarantees-in-Infinispan" rel="nofollow">Infinispan doc</a>, <code>NON_XA</code> means that Infinispan is registering as a Synchronization, which should be ok, shouldn't it?</p>
  
  <p>The cache is configured in the standalone-full.xml as follows (when removing <code>&lt;transaction mode="NON_XA" locking="PESSIMISTIC"/&gt;</code> the problem disappears, at the price of having no transactional cache):</p>
  
  <pre><code>        &lt;cache-container name="cacheContainer" start="EAGER"&gt;
              &lt;local-cache name="my_table_cache"&gt;
                  &lt;locking isolation="REPEATABLE_READ"/&gt;
                  &lt;transaction mode="NON_XA" locking="PESSIMISTIC"/&gt;
              &lt;/local-cache&gt;
          &lt;/cache-container&gt;
  </code></pre>
  
  <p>The Oracle DS is defined as follows</p>
  
  <pre><code>           &lt;datasource jndi-name="java:jboss/datasources/myDataSource" pool-name="dataSource" enabled="true"&gt;
                  &lt;connection-url&gt;jdbc:oracle:thin:@127.0.0.1:1523:orcl&lt;/connection-url&gt;
                  &lt;driver&gt;ojdbc7.jar&lt;/driver&gt;
                  &lt;pool&gt;
                      &lt;max-pool-size&gt;25&lt;/max-pool-size&gt;
                  &lt;/pool&gt;
                  &lt;security&gt;
                      &lt;user-name&gt;myuser&lt;/user-name&gt;
                      &lt;password&gt;myuser&lt;/password&gt;
                  &lt;/security&gt;
                  &lt;timeout&gt;
                      &lt;blocking-timeout-millis&gt;5000&lt;/blocking-timeout-millis&gt;
                  &lt;/timeout&gt;
              &lt;/datasource&gt;
  </code></pre>
  
  <p>My Service (the dao is using simple JDBC operations, not Hibernate or similar)</p>
  
  <pre><code>@Singleton
  @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
  public class MyService {
  
  @Resource(lookup = "java:jboss/infinispan/container/cacheContainer")
  protected EmbeddedCacheManager cm;
  
  protected Cache&lt;String, MyEntity&gt; cache;
  
  @PostConstruct
  private void init() {
      try {
          cache = getCache();
      } catch (SQLException ex) {
          log.fatal("could not initialize caches", ex);
          throw new IllegalStateException(ex);
      }
  }
  
  public Cache&lt;String, MyEntity&gt; getCache() {
      Cache&lt;String, MyEntity&gt; cache = cm.getCache(getCacheName(), true);
      fillCache(cache);
      return cache;
  }
  
  protected void fillCache(Cache&lt;String, MyEntity&gt; cache) {
      List&lt;MyEntity&gt; entities = myDao.getEntities();
      for (MyEntity e : entities) {
          cache.put(e.getKey, e);
      }
  }
  
  public MyEntity getEntity(String key) {
      return cache.get(key);
  }
  public void insert(MyEntity entity) {
      myDao.insert(entity);
      cache.put(entity.getKey(), entity);
  }
  public void debug() {
      log.debug(cache.values());
  }
  }
  </code></pre>