I think you are on a wrong track.
You only create one Injector instance for an application and you only start the persistence service once.

What you have to do is:
- Don't inject EntityManager into your DAO but inject Provider<EntiyManager>. The EntityManager is an object with a limited lifetime. It will become unusable once a unit of work has finished. Therefore you need to get an instance from the provider just when you need it. And you should not store the EntiyManager in a member variable of a class.
- begin and end UnitOfWork if you don't use the PersistFilter in a ServletContainer. If your application runs in a servlet container I highly recommend you use the PersistFilter as described on the guice homepage to span a unit of work around a request.



On 08/29/2012 06:04 PM, Arthur Gregório wrote:
hello guys!

I am developing a project JSE with Hibernate, JPA and Guice to inject dependencies and part of persistence.

But I think there's something wrong with my architecture ... I have the following scenario:

I have a bean that maps the database and has the attributes of the person (for example) to access the data of this person have a DAO composed by interface where I keep the access methods and the implementation of this interface where actually writes that these methods will do. All DAOs extend an abstract DAO that has entitymanager.

To use DAO created a controller that receives via @inject the DAO implementation class to be controlled.

however every time you need to use the controller have to get a new injector instantiating and initializing the PersistentService

eg

when I want to use the controller:

PersonController controller = PersonController.getInstance();

the getInstance() method run:

Injector injector = Guice.createInjector(new JpaPersistModule("MyBudgetPU"));
        
injector.getInstance(PersistenceInitializer.class);
        
return injector.getInstance(PersonController.class);

where the line "injector.getInstance (PersistenceInitializer.class)" I initialize the PersistentService with the class:

public class PersistenceInitializer {

        @Inject
        public PersistenceInitializer(PersistService persistService) {
            persistService.start();
        }
    }

Finally, my question is if you really need to be doing this every time you have to use a controller or is there a way I can make just one time and all the controller's having their members injected, or at least maintain the service of persistence started without keep starting him every time.
--
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/-/FrcgcsXP0qMJ.
To post to this group, send email to google-guice@googlegroups.com.
To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google-guice@googlegroups.com.
To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply via email to