dana-i2cat/opennaas — pull request #11 by gbehrmann (2012-01-23 15:12:56+00:00)
Title: Feature/close jpa entitymanager

Hi,

I early on discovered a problem with persistence in Mantychore. Sometimes resources created with resource:create didn't exist after a restart.

Further investigation showed that this happened when Mantychore was shut down immediately after creating a resource. This is so because HSQLDB is configured to delay writes to disk by up to 10 seconds. HSQLDB of course flushes those changes earlier when it is shut down, however in Mantychore closing the application does not trigger a shutdown of the embedded HSQLDB instance. Thus the changes are lost.

To solve this problem I had to fix three issues:

1. EntityManagers created by GenericOSGiJpaRepository are never closed.
2. HSQLDB is never shut down
3. EntityManagerFactories creates by Dynamic JPA are never closed.

The fixes are:

1. Add a close method to GenericOSGIJpaRepository and make the lifecycle container managed; the reference is now injected into ResourceRepository (rather than creating it internally in ResourceRepository). This also makes the interface of ResourceRepository cleaner.

2. Add the shutdown=true property to the HSQLDB configuration. This triggers an automatic shutdown when the last EntityManager and EntityManagerFactory is closed.

3. Replace Dynamic JPA with Aries JPA; the latter correctly closes the EntityManagerFactory.

Just adding shutdown=true without fixing (1) and (3) doesn't work (for obvious reasons).

Unfortunately both (1) and (3) require changes in Mantychore. I will email a Mantychore patch to Isart right away. Please remember to commit that patch at the same time as merging this branch.

Please see detailed comments in each of the three commits.

Cheers,

/gerd