I wanted to read from a remote Sybase and persist to a local Derby database,
so I tried code like this in a Statelsess EJB:
@PersistenceContext(unitName="RemoteSybasePU")
private EntityManager em;
@PersistenceContext(unitName="LocalStoragePU")
private EntityManager localEm;
public java.util.List<Dfsite> getStations()
{
List<Dfsite> entityDfsList = null;
if (em != null)
try
{
Logger.getLogger(getClass().getName()).log(Level.INFO, "calling station query");
Query q = em.createQuery("select object(s) from Dfsite s where s.decid < 200 and s.time > :earliest");
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, -24);
q.setParameter("earliest", c.getTime());
q.setMaxResults(200);
entityDfsList = q.getResultList();
Logger.getLogger(getClass().getName()).log(Level.INFO, "query returned " + entityDfsList.size() + " rows");
em.flush();
if (localEm != null)
{
// convert beans & store locally
for (Dfsite s: entityDfsList)
{
DisplayBean db = new DisplayBean();
db.setDisplayText(s.getName());
db.setLatitude(s.getLatitude());
db.setLongitude(s.getLongitude());
Logger.getLogger(getClass().getName()).log(Level.INFO, "persisting displayBean " + db.getDisplayText());
localEm.persist(db);
}
localEm.flush();
//localEm.close();
}
else
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "local EntityManager is not ready");
}
catch(Exception e)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE,"Exception whilst trying to run query: ", e);
//throw new RuntimeException(e);
}
else
System.err.println("Entity manager isn't ready");
return entityDfsList;
}
When I run this code, the table is created in local Derby database OK, objects are selected from remote Sybase OK, but I get an exception
Caused by: java.sql.SQLException: Error in allocating a connection. Cause: java.lang.IllegalStateException: Local transaction already has 1 non-XA Resource: cannot add more resources.
If I try em.close() I get a different exception. em.flush() doesn't help either.
I get the feeling that there should be a simple solution, but I haven't found it yet ;-)
Any suggestions very welcome, thanks.
[Message sent by forum member 'freddiefishcake' (freddiefishcake)]
http://forums.java.net/jive/thread.jspa?messageID=226677