Also, correct me if I'm wrong, but if txn.begin() raises an exception, unitOfWork.end() is never called, the EntityManager is never closed and removed from ThreadLocal in JpaPersistService.
On Sunday, January 18, 2015 at 7:19:36 PM UTC+11, [email protected] wrote: > > Hi all, > > I'll use Hibernate as an example for the underlying JPA implementation. > In JpaLocalTxnInterceptor.java, line 66, guice-persist starts a > transaction with txn.begin(). > > When Guice asks Hibernate to start a transaction, this is what Hibernates > does: > - Fetch a connection > - Tries to disable auto-commit on the connection if autocommit was set to > true. > > The problem is, sometimes this operation fails (loss of connectivity, > whatever...) and an Hibernate exception is thrown (the most curious one can > have a look at Hibernate source-code, JdbcTransaction.java, line 72). But > guice-persist doesn't catch this potential RuntimeException when it begins > a transaction. > > And because it doesn't catch it, it doesn't call rollback on the > transaction. You might think, why would I call rollback on a transaction > which didn't even start ? (keep in mind txn.begin() just failed). > Because the connection fetched by Hibernate is only released in case of > commit or rollback. By not calling rollback, the current thread gets stuck > in a stale state, and the next time this thread is used for JPA > transaction, Hibernate will throw an exception: "Already have an associated > managed connection". > > I had a quick look at the JPA interceptor source code, for Spring Data, > they do catch any potential exception, even when they begin a transaction, > and call rollback in case of problem. > > Any thoughts on this ? > > Thanks for your help guys > -- You received this message because you are subscribed to the Google Groups "google-guice-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice-dev. For more options, visit https://groups.google.com/d/optout.
