5 Replies Latest reply on Jun 20, 2011 3:18 PM by fjkjava.febinjk.gmail.com

    Dirty read Issue - @Asynchronous

    fjkjava.febinjk.gmail.com

      Hi All,



      I have a Asynchronous in one of my utility class and I am calling it multiple times. Each time it is executing a stored procedure, I am passing the name of the stored procedure as a parameter. I have a counter that can tell how many times the Asynchronous method is called.


      Is there any way I know that all the methods are finished? - Please suggest!


      I want to update some values of an Entity after all the Asynchronous methods are executed successfully(I mean all the stored procedures are executed successfully).


      I will explain what I did, I created a counter attribute in my Entity and incremented it in Asynchronous method based on the return value of stored procedure. Even though all the Asynchronous methods are successful the counter is not incremented correctly. Because when a Asynchronous method reads the value of the counter from the database it is not getting the updated value always.




      try {                              
           CallableStatement statement = null;
           int result=0;                              
           try {
                connection = datasource.getConnection();     
                statement = connection.prepareCall(procName);
                statement.registerOutParameter(1, Types.NUMERIC);                    
                statement.execute();                    
                result = statement.getInt(1);                    
                connection.close();                    
           } catch (Exception e) {
                log.debug("--Stored Procedure Call Error--", e.getMessage());                         
                e.printStackTrace();          
                result=0;
           }               
           MyBean bean = entityManager.find(MyBean.class, id);                    
           entityManager.refresh(bean);
           entityManager.lock(bean, LockModeType.READ);               
           if(result==1){                    
                bean.setCount(bean.getCount()+1);//getCount() is not retrieving update value
           }
           entityManager.persist(bean);
           entityManager.flush();               
           if(sourceCount==bean.getCount()){                    
                //Update bean here
           }               
      } catch (Exception e) {
           log.debug("--Error--", e.getMessage());
           e.printStackTrace();
      } 



      Please suggest how can I avoid the dirty reading here. Or I can achieve this in any other way.


      Please Help.


      Thanks