Re: Guice persitence - EntityManager used by multiple Threads

662 views
Skip to first unread message

Aekold

unread,
Aug 27, 2012, 8:34:17 AM8/27/12
to google...@googlegroups.com
I made it work using wrapped EntityManager, to get thread local entity manager from factory. See attached files as example. It looks like a bit overcomplicated, but I think that Spring is doing something similar deep inside.

On Sunday, August 26, 2012 2:40:43 AM UTC+3, Markus Fischboeck wrote:
Hi!

I've been integrating Guice 3.0 into a desktop application.
To access my database I use JPA together with EclipseLink. Everything is configured straight forward, using the JpaPersistModule guice provides. 
The application runs multiple threads and each thread is using the same data access object to read and write data in which the EntityManager is injected using constructor injection.
However it appears that using the DAO within a thread, transactions are not commited and entities do not appear in the database. 

After inspecting that issue, I've seen that the EntityManager get's injected as a singleton, which is not suitable as it is not thread-safe. I then tried to inject the EntityManagerFactory instead, which bypasses the transaction interception somehow.

Is there a general approach to get this working? Unfortunately I haven't found much about this topic especially in conjunction with guice on the web. Any help is greatly appreciated!



TransactionalInterceptor.java
EntityManagerFlowFilter.java
EntityManagerProvider.java
EntityManagerWrapper.java

Stephan Classen

unread,
Aug 27, 2012, 8:33:41 AM8/27/12
to google...@googlegroups.com
You should not inject the EntityManager directly.
Instead you should inject a Provoder<EntityManager> and then use get()
to obtain the EntityManager just when you need it.
The implementation of Provider<EntityManager> uses a ThreadLocal to
ensure that every thread has its own instance of EntityManager.


On 08/26/2012 01:40 AM, Markus Fischboeck wrote:
> Hi!
>
> I've been integrating Guice 3.0 into a desktop application.
> To access my database I use JPA together with EclipseLink. Everything
> is configured straight forward, using the JpaPersistModule guice
> provides.
> The application runs multiple threads and each thread is using the
> same data access object to read and write data in which the
> EntityManager is injected using constructor injection.
> However it appears that using the DAO within a thread, transactions
> are not commited and entities do not appear in the database.
>
> After inspecting that issue, I've seen that the EntityManager get's
> injected as a singleton, which is not suitable as it is not
> thread-safe. I then tried to inject the EntityManagerFactory instead,
> which bypasses the transaction interception somehow.
>
> Is there a general approach to get this working? Unfortunately I
> haven't found much about this topic especially in conjunction with
> guice on the web. Any help is greatly appreciated!
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/KlKbRlNE_TQJ.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.

Reply all
Reply to author
Forward
0 new messages