This
is a popular strategy, where an EntityManager is created and
destroyed around each database transaction. Set the transaction-type attribute on your persistence unit's configuration:
<persistence-unit name="myFirstJpaUnit" transaction-type="RESOURCE_LOCAL">
And change the PersistenceModule's configuration accordingly:
Injector injector = Guice.createInjector(..., PersistenceService
.usingJpa()
.across(UnitOfWork.TRANSACTION)
.buildModule());
Note: this is the default UnitOfWork strategy even if unspecified (as in the earlier example). For advanced users, keep in mind that in warp-persist this strategy works a bit differently from Java EE container-managed EJBs and the PersistenceContextType setting for EntityManagers is always EXTENDED (not TRANSACTION as you may be led to think). You can safely ignore this in SE or servlet environments.
Hi Dhanji
Very interesting .. So you are saying that the warp persist documentation are wrong?
And we have set the UOW to transaction. If you need sample code or a demo project from me please say so.. So I guess what I am experiencing are actually not that the em are being destroyed around database transactions. If it were that data would be refreshed.
One thing to mention though are that we only read from the database, so we have not methods encapsulated with the transactional annotations. And another note would be that we are running on Jboss 4
If you don't want to use @Transactional, you could switch to UnitOfWork.REQUEST if it fits your use case, otherwise you could consider using a custom unit of work as described here: http://www.wideplay.com/transactionsemantics (bottom of the page)
Non-transactional access is usually not what you want though, you'd have to be 100% sure nobody else is doing writes and you are only doing reads. My advice would be to use transactions even if you don't need them (yet). It has little performance impact and you'll sleep better.
If you use Hibernate (not JPA), there's even an experimental READ_ONLY mode for @Transactional.
Also note that the reason you have to configure Hibernate in a certain way is that WP is not really a persistence abstraction. It's just a library that tries to ease the integration of persistence in your Guice app. In theory, if you know Guice and Hibernate / JPA you're good to go. It's just that the docs are a bit confusing at the moment because the 1.0 docs are a bit outdated and the 2.0 docs aren't ready yet. And we thank you for pointing out the pain points :)
uice.createInjector(..., Servlets.configure()
.filters()
.buildModule();
If you don't want to use @Transactional, you could switch to UnitOfWork.REQUEST if it fits your use case, otherwise you could consider using a custom unit of work as described here: http://www.wideplay.com/transactionsemantics (bottom of the page)I've tried switching to Request. It's not that I dont want to use transactional but we just do not have any methods that writes. It's a read only configuration database. Hmm looking thru the code I noticed this:
public void configure() {
bindConstant().annotatedWith(JpaUnit.class).to(unit);
bind(ReadOnlyAllInOneService.class).to(
ReadOnlyAllInOneServiceDatabaseImpl.class).in(Singleton.class);
bind(PersistenceInitializer.class).asEagerSingleton();
}
Could all the fuss be that I bind our db service as Singleton, I guess I can bind it as session scoped? If so I really apologize :(
Non-transactional access is usually not what you want though, you'd have to be 100% sure nobody else is doing writes and you are only doing reads. My advice would be to use transactions even if you don't need them (yet). It has little performance impact and you'll sleep better.
If you use Hibernate (not JPA), there's even an experimental READ_ONLY mode for @Transactional.
Also note that the reason you have to configure Hibernate in a certain way is that WP is not really a persistence abstraction. It's just a library that tries to ease the integration of persistence in your Guice app. In theory, if you know Guice and Hibernate / JPA you're good to go. It's just that the docs are a bit confusing at the moment because the 1.0 docs are a bit outdated and the 2.0 docs aren't ready yet. And we thank you for pointing out the pain points :)So could one use dynamic finders and "plain" jpa/hibernate/guice?