Search code examples
javaspringosgiopenjpaapache-karaf

OpenJPA persistence between OSGi bundles


I have two OSGi bundles deployed on Apache Karaf container. Lets say they are A and B. The A bundle is the main bundle which contains all database entities and persistence logic. The B OSGi bundle is an application which uses methods from A to store some data into database. Also bundle B has CXF web service endpoint. The persistence is handled by OpenJPA.

Method in bundle A wraps the persist method of entity manager. In bundle B there is a method which takes an object as a parameter which is defined in bundle A and has the method to persist data. When I call that method in bundle B, all is ok, the data is persisted into database as expected.

The problem is that I want to update the an entity in B bundle's CXF endpoint implementation class which is exposed as a Spring bean and has OSGi reference to an object from bundle A which contains find statement to find the entity I need to update.

The entity is returned to me correctly when I invoke find method, but when I set some new value the transaction does not get committed and the data does not appears in the database.

As I know one of OpenJPA features is that when you call persist to an object, then later you can set some new data to it and the needed transactions are made to update that entity. It does not seem to be working in my case. I have tried to annotate CXF endpoint implementation class with org.springframework.transaction.annotation.Transactional annotation. But this is not working as well.

Maybe someone know where might be the problem? Do I need to write query which updates my entity in CXF endpoint?


Solution

  • In this case I suggest switching from spring to blueprint. If you take a look at aries blueprint (for example) which is used by Karaf you are able to retrieve for example DAOs from Bundle A, call a "changing method" from Bundle B and it'll persisted by the Transaction Manager attached to Bundle A.