0

We're developing a suite of modules to (hopefully) be deployed in Apache Karaf. Each module has a layered structure starting with the domain JPA entities / DAO, services, web components, etc., where each of these layers turns into a different OSGi bundle.

The JPA entities extend some abstract supertypes that belong to a common bundle. And here comes the problem..

As OpenJPA is "advertised" a lot when learning the ways of Karaf and OSGi so we tried it first. Compile-time enhancing was a disaster and a complete waste of time due to our entity hierarchy that spans multiple bundles (jars): several PUs and the abstract supertypes. Even if it worked, I'm afraid the OpenJPA enhancing requires us to compile all modules with the same Java and OpenJPA versions which might turn ugly in the future where each module will develop at a different rate.

Then we looked into Hibernate and had it working thanks to a couple of tutorials (not very popular it seems). However, although all persistent units are transaction-type="JTA", the datasource is of JTA type via JNDI, and the DAO CRUD methods are marked as in Blueprint, the DAO save method, for example, that should only hold something like

entityManager.persist(account)

only works when followed by

entityManager.flush();

I was expecting the changes to be flushed automatically at the end of the transaction (i.e. at the end of the save method - the current transaction end boundary)

If we add flush() just after persist, but then throw a runtime exception there is no rollback for the whole save method.

There are no errors in the log and checking it at debug level shows

Created a new persistence context org.apache.aries.jpa.container.impl.EntityManagerWrapper@42e4823d for transaction [Xid:globalId=ffffffca2d6dfffff...
...
Skipping JTA sync registration due to auto join checking
...
Clearing up EntityManager org.apache.aries.jpa.container.impl.EntityManagerWrapper@42e4823d as the transaction has completed.

so there is a transaction, but it ends without the changes pushed to the database..

By the way, the flush mode is set to AUTO.

Thanks for any tip you might have.

Daniel Paval
  • 172
  • 12
  • Can you post your code or a simplified version somewhere. It is a bit difficult to guess with just this information. Hibernate is currently not very OSGi friendly. So you might also give Eclipselink a try. It is now supported in aries. – Christian Schneider May 28 '13 at 23:26

2 Answers2

0

Since the Hibernate team did some improvements for Hibernate to run more smoothly on OSGi you might consider using the latest Hibernate version, not sure if it's released already.

Achim Nierbeck
  • 5,265
  • 2
  • 14
  • 22
0

I would recommend using EclipseLink for easier OSGI integration.

I once used JPA with OSGI. I started with OpenJPA, switched to Hibernate after hitting issues and I ended up using EclipseLink to solve other problems.

Hope it helps

rimero
  • 2,383
  • 1
  • 14
  • 8