[jboss-user] [EJB 3.0] - Re: EntityManager is null

kgreene do-not-reply at jboss.com
Wed Feb 20 09:42:12 EST 2008


This code works except for the flush.  The flush does not commit the updates to the database. Specifically, I want to update the status in the Fetcher.transfer method. However, the status does not get updated to "RUNNING". It only gets updated to "COMPLETE" once the fetcher is done.  I thought maybe moving the entity manager outside the Watcher and Fetcher might fix this issue. So, I created a ConversionFileDAO which I tried to use in the code below, but I get a namenotfoundexception. I of course commented out the em method calls when I did this.

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) 
{ 
////  THIS DOES NOT WORK. get a namenotfoundexception
//InitialContext ctx = new InitialContext();
//ConversionFileDAORemote dao = (ConversionFileDAORemote) ctx.lookup("ConversionFileDAO/remote");
//dao.insert(file);

em.persist(file); 

//FLUSH DOES NOT WORK. the file is persisted to the db, but only after the watcher is done. this causes the fetcher to get an exception b/c sometimes it can't find the entry in the db
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(); 

transfer(remotefile,localfile); 

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


} 

public void transfer(File remoteFile, File localFile) 
{ 

////  THIS DOES NOT WORK. get a namenotfoundexception
//InitialContext ctx = new InitialContext();
//ConversionFileDAORemote dao = (ConversionFileDAORemote) ctx.lookup("ConversionFileDAO/remote");
//dao.update(file);

remoteFile.setStatus("RUNNING"); 

em.merge(remoteFile); 
//THIS DOES NOT WORK - status of running does not get updated to the db
em.flush(); 
} 

}

=============================
public class ConversionFileDAO implements ConversionFileDAORemote
{

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

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

public update(ConversionFile file)
{
em.merge(file);
em.flush();
}

public getConversionFile(String fileName)
{
ConversionFile currFile = null;

 try {
	currFile = (ConversionFile)em.createQuery("from ConversionFile where fileName=:filename").setParameter("filename", fileName).getSingleResult();
} 
catch (NoResultException e1) 
{
				
}

return file;
}
}
		


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

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



More information about the jboss-user mailing list