4 Replies Latest reply on Feb 6, 2008 12:22 PM by Adam Warski

    Ehcache and 'manual' seam invocation

    Adam Warski Master

      Hello,

      I have setup a 2nd level cache for hibernate (ehcache) for some of my entities. And it works fine, if I access the site using a browser.

      However, I also need to access the Seam components from a daemon thread; however the thread and the "browser threads" seem to have separate caches - if I modify something from the browser (in the DB), the change is not visible in the daemon thread, because it uses old cache values. But the values displayed in the browser (which are also read from the cache) are correct.

      Here's what I do in the daemon thread:

      boolean createContexts = !Contexts.isEventContextActive() && !Contexts.isApplicationContextActive();
       if (createContexts) {
       Lifecycle.beginCall();
       }
      
       try {
       UserTransaction tx = null;
       boolean txStarted = false;
       try {
       tx = (UserTransaction) Component.getInstance("org.jboss.seam.transaction.transaction");
       if (tx.getStatus() != Status.STATUS_ACTIVE) {
       txStarted = true;
       tx.begin();
       }
      
       ((EntityManager) Component.getInstance("entityManager")).joinTransaction();
       ((UpdateHandler) Component.getInstance("updateHandler")).update();
      
       if (txStarted) {
       tx.commit();
       }
       } catch (Exception e) {
       try {
       if (txStarted) {
       tx.rollback();
       }
       } catch (SystemException e1) {
       Logging.getLog(UpdateManager.class).error("Exception when rolling back the transaction", e1);
       }
      
       e.printStackTrace();
       }
       } finally {
       if (createContexts) {
       Lifecycle.endCall();
       }
       }
      


      So basically I:
      * setup Seam contexts: Lifecycle.beginCall()
      * start a transcation, if necessary
      * invoke a method of a component (UpdateHandler.update())
      * commit the transaction
      * Lifecycle.endCall()

      Am I missing something?

      --
      Regards,
      Adam Warski