On Tuesday, February 5, 2013 8:48:35 AM UTC+1, Manuel wrote:
Hey,
I had a look and used this to implement a servlet-module: http://code.google.com/p/google-guice/wiki/ ServletModule
My Module looks like the example from your session per request link: http://code.google.com/p/google-guice/wiki/JPA#Web_ Environments_%28session-per- http-request%29
In the web.xml I put a Listener.
<listener>
<listener-class>de.mash.project.shared. MyGuiceServletConfig</ listener-class>
</listener>
The Listener looks like this:
public class MyGuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new MyServletModule());
}
}
Then I added the following in my Worker Entity-class://@RequestScoped@Entity
@Table(name = "Worker")
public class Worker {
@Transient
@Inject static EntityManager em3;
You don't want a single EntityManager for the whole app lifetime and shared between threads, so you have to use a Provider<EntityManager> here.
...
@Transactional
public void persist() {...}
@Transactional
public static Worker findWorker(Long id) {...}
}
I don't think Guice can intercept static methods, so you'd have to explicitly begin/end a transaction here.
I set a breakpoint on my doFilter() method in my PersistFilter, and its just doing what Jens descripted above.EntityManager em = create(); //save this em instance somewhere so you can access it in your RF service methods, e.g. using a static ThreadLocalfilterChain.doFilter() //this calls the RF servlet and does all the RF requestsem.close() //Don't forget to cleanup the ThreadLocal if you have used one.Instead of EntityManager, its called UnitOfWork
I thought my WorkerEntity´s em3 EntityManager would now get injected but it doesnt :(
Static injection needs to be requested explicitly from your module: https://code.google.com/p/google-guice/wiki/Injections#Static_Injections
Im not sure if I´m totally wrong, or if just something small is missing/wrong.
Use a service (with a custom ServiceLocator that uses Guice, see https://github.com/tbroyer/gwt-maven-archetypes/tree/master/guice-rf-activities/src/main/resources/archetype-resources/__rootArtifactId__-server/src/main/java for an example) instead of static methods in your entity; that way you'll remove the need for static injection and @Transaction will Just Work™.
Beware that your service will be a "quasi-singleton" in RequestFactory (singleton that can be garbage-collected under low-memory conditions and recreated later when needed), so always inject Provider<>s.
-- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment