-
1. Re: EntityManager is null
alrubinger Feb 14, 2008 4:45 PM (in response to kgreene)How is the calling class accessing an instance of VpInfoDAOBean?
S,
ALR -
2. Re: EntityManager is null
kgreene Feb 15, 2008 10:19 AM (in response to kgreene)Currently, I am using:
VPInfoDAOImpl vpdao = VPInfoDAOImpl.getInstance();
I also tried:
InitialContext ctx = new InitialContext();
VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local");
I tried VPInfoDAORemote also. That did not work either. -
3. Re: EntityManager is null
alrubinger Feb 15, 2008 11:26 AM (in response to kgreene)VPInfoDAOImpl vpdao = VPInfoDAOImpl.getInstance();
This won't work; here you are constructing a plain object, not accessing an EJB instance from the container.InitialContext ctx = new InitialContext(); VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local");
What's not working here? Are you able to perform the lookup, cast, and invoke?
S,
ALR -
4. Re: EntityManager is null
kgreene Feb 17, 2008 12:41 PM (in response to kgreene)When I tried the below, the dao object is null and I cannot call methods on it.
InitialContext ctx = new InitialContext();
VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local"); -
5. Re: EntityManager is null
kgreene Feb 19, 2008 2:16 PM (in response to kgreene)Ok, I am very lost. Forgive me, I am new to jboss and ejbs. I've been looking for a solution for several hours and still have not found the answer. I misspoke about the actual issue. The dao object is not null. I get NameNotFoundException because it can't find it. When I bring up my jndi console it's not there, which is why I'm assuming it can't find it.
I'm calling the lookup from a message-driven bean. I didn't think this is considered client code and didn't think lookup would even work.
If I move the entity manager to the message-driven bean, then it works. However, I get an additional issue in that flush does not commit the updates to the database. I thought perhaps moving the entity manager to a class outside the message-driven bean would solve this issue; however, I have not gotten this to work. -
6. Re: EntityManager is null
alrubinger Feb 19, 2008 3:05 PM (in response to kgreene)Please paste the code that works, the code you'd like to work, and the relevant part of the JNDIView?
S,
ALR -
7. Re: EntityManager is null
kgreene Feb 20, 2008 9:42 AM (in response to kgreene)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;
}
}