0 Replies Latest reply on Feb 17, 2008 1:11 PM by Karen Greene

    entitymanager flush is not flushing

    Karen Greene Newbie

      Hi,

      I'm new to jboss and ejb. I am trying to have the EntityManager update the database immediately versus waiting until the code completes. I tried using flush, but the database does not get updated until after the code completes. Any ideas?


      Watcher.java

      @Stateless
      @Clustered
      public class Watcher
      implements WatcherIntLocal, WatcherIntRemote
      {

      @Resource
      private SessionContext context;
      @PersistenceContext (unitName="WatcherDB")
      private EntityManager em;


      public void checkForNewFiles() throws WatcherException
      {
      try
      {
      List validFiles = findValidFiles();
      addToQueue(validFiles);

      }
      catch (Throwable e)
      {
      e.printStackTrace();
      throw new WatcherException(e);
      }
      }

      public List findValidFiles() throws WatcherException
      {
      //iterate thru a list of files and search for certain extensions
      //if the file name matches those extensions, call insert
      }

      public void insert(File file)
      {
      em.persist(file);
      em.flush();
      }

      public void addToQueue(List validFiles) throws WatcherException
      {
      // iterate thru files
      //create a TextMessage using the file name
      //send the message
      }
      }


      ==================================
      Fetcher.java

      public class Fetcher
      implements MessageListener
      {

      @Resource
      private MessageDrivenContext context;

      @PersistenceContext (unitName="WatcherDB")
      private EntityManager em;

      public void onMessage(Message arg0) {

      String fileName = ((TextMessage)arg0).getText();
      File remoteFile = (File)em.createQuery("from File where fileName=:filename").setParameter("filename",fileName).getSingleResult();

      //check disk space to make sure there is enough space to put the file
      transfer(remotefile,localfile);

      remoteFile.setStatus("COMPLETE");
      em.merge(remoteFile);
      em.flush();


      }

      public void transfer(File remoteFile, File localFile)
      {
      remoteFile.setStatus("RUNNING");

      em.merge(remoteFile);
      em.flush();
      }

      }