I could have written the same questions two days ago, and even started
drafting the message to warp-core. Pardon some irrelevant details in
this message; my goal is partially to document the knowledge that
would have helped me. :)
First, it's important to differentiate session open/close from
transaction begin/end. The PersistenceFilter and WorkManager open and
close *sessions*. They should be used with Hibernate's
hibernate.current_session_context_class=managed). The common
(hibernate.current_session_context_class=thread) will automatically
create a session when SessionFactory.getCurrentSession() is called,
requires that a transaction is started, and automatically closes the
session when that transaction is ended. That is clearly not the
session lifecycle expected with UnitOfWork.REQUEST and
PersistenceFilter or WorkManager. If you want
ThreadLocalSessionContext, you should be using UnitOfWork.TRANSACTION
without PersistenceFilter or WorkManager.
As for *transaction* management, that's where the PersistenceService
and those @Transactional annotations come in. Note that Hibernate
itself does not require you to put session.beginTransaction() and
session.endTransaction() around your operations. If you omit these,
then Dhanji's comment applies: with MySQL, an implicit transaction
gets used for each operation, and the operations get sent on flush.
I can't explain the difference in behavior between your unit tests and
servlet, except to suggest that some Hibernate or database
configuration is different (flush mode?). After all, the
PersistenceFilter just uses WorkManager behind the scenes. Also, check
that you're instantiating the @Transaction annotated classes through
Guice so the PersistenceService's interceptor can take effect.
Hope that helps.
On Aug 20, 2010 6:21 AM, "Michael Day" <blake...@gmail.com> wrote: