Yes, desktop application can't use PersistFilter. But I get to this a little later...

First your example:

public class DAO {
   
    private final Provider<EntityManager> emProvider;
   
    @Inject
    public DAO(Provider<EntityManager> emProvider) {
        checkNotNull(emProvider);
        this.emProvider = emProvider;
    }

    @Transactional
    public Object findMyObject(in id) {
        final EntityManager em = emProvdider.get();
        // do your work here
    }
}

The only trick is, that emProvider.get() will always return a valid EntityManager instance. (If you want to you can look at the source: JpaPersistService).

Now to the UnitOfWork:
I think the easiest way is to use the @Transactiona annotation.
The @Transaction annotation spans a UnitOfWork around the outer most transaction. For most applications this is sufficient. If you have a lot of small transactions which are triggered by an single user action it may introduce a little overhead because it will create a new EntityManager for every transaction.
But until you can measure this overhead I wouldn't worry about it. (Remember the first rule about optimization: don't do it!)

There is one thing you have to be aware of when using the @Transaction annotation.
It will only work
 - when the instance is of the class has been instantiated by guice (i.e has been injected into your class or has been retrieved with injector.getInstance())
 - when the method which is annotated is NOT private.

Hope this helps

Greets Stephan


On 08/29/2012 10:29 PM, Arthur Gregório wrote:
can you provide one example in how to use the Provider<EntiyManager>

is a desktop application... I think not giving in to use PersistFilter with JSE application

Arthur P. Gregório
+55 45 9958-0302
@gregorioarthur
www.arthurgregorio.eti.br



2012/8/29 Stephan Classen <[email protected]>
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 [email protected].
To unsubscribe from this group, send email to [email protected].
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 [email protected].
To unsubscribe from this group, send email to [email protected].
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 [email protected].
To unsubscribe from this group, send email to [email protected].
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 [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply via email to