[jboss-user] [EJB 3.0] - Entities not synchronizing

galo.navarro do-not-reply at jboss.com
Fri Sep 15 11:11:13 EDT 2006


I have some problems synchronizing Entities between different parts of my application.

Basically, I have one thread waking up once in a while, it loads a few entity beans, performs actions, and goes back to sleep. 


  | @Service(objectName="jboss:custom=NOCManagerService")
  | public class NOCManagerService implements Runnable, NOCManagementService {
  | 	
  | 	/*
  | 	@Resource 
  | 	private SessionContext ctx;
  | 	*/
  | 	@EJB(beanName="NOCAccessBean") 
  | 	private NOCAccess nocAccessBean;
  | 
  | 	public void start() throws Exception {
  | 	    Thread thread = new Thread(this);
  | 	    thread.start();		
  | 	}
  | 
  | 	public void run() {
  | 		running=true;
  | 		while(this.running) {
  | 			loadNOCs();
  | 			if(nocs!=null) {
  | 				for(NOC noc:nocs) {
  | 					this.nocPhaseProcessor.process(noc);
  | 				}
  | 			} 
  | 			try {
  | 				synchronized (this) {
  | 					nextRun=System.currentTimeMillis() + this.processingInterval;
  | 					wait(this.processingInterval);					
  | 				}
  | 			} catch (Exception e) {
  | 			}
  | 		}
  | 	}
  | [...]
  | }
  | 

The following EJB is called for each of those entities and is the one that performs some updates 


  | @Stateless
  | @TransactionAttribute (TransactionAttributeType.REQUIRES_NEW)
  | public class NOCPhaseProcessorBean implements NOCPhaseProcessor {
  | 
  | 	@PersistenceContext(unitName="NOCBeans")
  | 	private EntityManager em;
  | 	
  | 	/**
  | 	 * Interceptors plug into this method and send the NOC to queues or do whatever
  | 	 * they have to do with them
  | 	 */
  | 	@Interceptors({NOCCommonFilter.class, Phase2Filter.class})
  | 	public void process(NOC noc) {
  | 		System.out.println("Processing" + noc.toString());
  | 		try {
  |                        /* [do stuff] */
  | 			em.merge(noc);			
  | 		} catch (Exception e) {
  | 			NOCManagerService.getLog().error(e.getMessage());
  | 		}				
  | 	} 
  | }
  | 
  | 

I'm using this bean to access my Entities, save them etc.


  | 
  | @Stateful
  | @TransactionAttribute (TransactionAttributeType.NOT_SUPPORTED)
  | public class NOCAccessBean implements NOCAccess {
  | 	@PersistenceContext(unitName="NOCBeans", type=PersistenceContextType.EXTENDED)
  | 	private EntityManager em;
  | 
  | 	public List<NOC> listNoCs() {
  | 		List<NOC> nocs = em.createNamedQuery("findAllNOCs").getResultList();
  | 		for(NOC noc:nocs) {
  | 			NOCManagerService.getLog().info("LOADED " + noc);
  | 		}
  | 		return nocs;
  | 	}
  | 
  | 	@TransactionAttribute (TransactionAttributeType.REQUIRED)
  | 	public boolean saveNOC(NOC noc) {
  | 		noc.setDtTmStp(new Date());
  | 		em.merge(noc);
  | 		em.flush();
  | 		return true;
  | 	}
  | [...]
  | }
  | 


OK, I have a web interface that uses that NOCAccessBean and allows me to edit those Entities (via saveNOC in the AccessBean). If change some of the properties, I can see the values are saved in my database, and my web interface is synchronized showing all the new values. Also, when the thread kicks in and performs some updates, the web pages also shows them. 

BUT! if I do some updates on the Entities from the webpage, the next time service kicks in it still has the OLD versions of my entities. 

I would expect that I don't even need to reload the list of entities, but even doing it the entities are NOT updated. I've tried running em.refresh() for each Entity in the ProcessorBean but still doesn't work.

May anybody illuminate me?





View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3971945#3971945

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3971945



More information about the jboss-user mailing list