Hello,
I have a Seam+Hibernate app and I have setup a 2nd level cache for, using ehcache (1.2.3), for some of my entities. And it works fine, if I access the site using a browser.
However, I also need to access the Seam components from a daemon thread; but the thread and the "browser threads" seem to have separate caches - if I modify something from the browser (in the DB), the change is not visible in the daemon thread, because it uses old cached values. The values displayed in the browser (which are also read from the cache) are correct, so on the "browser threads" side, the cache is invalidated as expected.
Here's what I do in the daemon thread:
Code:
boolean createContexts = !Contexts.isEventContextActive() && !Contexts.isApplicationContextActive();
if (createContexts) {
Lifecycle.beginCall();
}
try {
UserTransaction tx = null;
boolean txStarted = false;
try {
tx = (UserTransaction) Component.getInstance("org.jboss.seam.transaction.transaction");
if (tx.getStatus() != Status.STATUS_ACTIVE) {
txStarted = true;
tx.begin();
}
((EntityManager) Component.getInstance("entityManager")).joinTransaction();
((UpdateHandler) Component.getInstance("updateHandler")).update();
if (txStarted) {
tx.commit();
}
} catch (Exception e) {
try {
if (txStarted) {
tx.rollback();
}
} catch (SystemException e1) {
Logging.getLog(UpdateManager.class).error("Exception when rolling back the transaction", e1);
}
e.printStackTrace();
}
} finally {
if (createContexts) {
Lifecycle.endCall();
}
}
So basically I:
* setup Seam contexts: Lifecycle.beginCall()
* start a transcation, if necessary
* invoke a method of a component (UpdateHandler.update())
* commit the transaction
* Lifecycle.endCall()
Am I missing something? Of course, I'd like to have only one cache :).
--
Regards,
Adam Warski