0

I am using karaf 3.0.3 and the following Hibernate bundles:

161 | Active | 80 | 4.2.12.Final | hibernate-core
162 | Active | 80 | 4.2.12.Final | hibernate-entitymanager
172 | Active | 80 | 4.2.12.Final | hibernate-osgi

With this set-up I cannot get a transaction roll-back to work, although I can deploy the exact same components (JPA DAOs, service layer etc) as a standalone Spring application, and I see roll-backs performed correctly.

What are the gotchas when trying to get transactions to work successfully with OSGI/hibernate?

  • is it something to do with cross-bundle transactions? (My DAOs and service layer are in separate bundles)

If I get not replies here, I will update with more specific information, but first I want to see if this is a common problem with OSGi/hibernate/jpa.

edit after user2007829's comment: The DAOs extend Spring's JpaRepository, and the service layer method is annotated with Spring's @Transactional. I believe in this setup, auto commit should be implicitly off, and the standalone spring app set-up is working as expected. Is that wrong?

How should the persistence unit XML file differ in the OSGI case? Right now the only difference is that the transaction-type is JTA in the karaf case, and I have a declaration like this:

<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/‌​my-datasource)</jta-data-source>
david webber
  • 518
  • 1
  • 5
  • 13

2 Answers2

1

There are many possible reasons why this could occur. For example your data source might not wrap an XADataSource correctly. In this case it can occur that the EntityManager did not join the transaction or is not enlisted as XA ressource.

Have a look at these tutorials. It is easier to start from a working basis: http://www.liquid-reality.de/x/LYBk http://www.liquid-reality.de/x/C4DK

Unfortunately the tutorials are based on blueprint and aries jpa so it is not exactly spring. You still should be able to use some of the informations. Additionally I can only recommend to switch from spring to blueprint as spring does not support OSGi anymore.

For blueprint there are apache aries which the tutorials are based on or eclipse gemini.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Thanks, your comment and tutorial were very useful. I created the datasource according to your tutorial, and left the rest of my app untouched and I was able to prove that the transaction rollback was working. I suspect the "XA" is key here as you suspected. Can you comment on why I need XA transactions even though I only have one datasource and the app is running in one JVM? Is it because my transaction spans several bundles? – david webber Apr 09 '15 at 09:34
  • 1
    I am not sure how spring works internally. Generally in jpa you can either use transactions type RESOURCE_LOCAL or JTA. When you run in a container it is recommended to use JTA which requires XADataSource. So probably you set spring to run in JTA mode. Btw. JTA is always a good idea in a container and it has no real performance disadvantages. – Christian Schneider Apr 09 '15 at 12:31
0

And how do you use the hibernate? Do you create a transaction and work within it or are your queries session related? Also, did you set autocommit to false?

  • The DAOs extend Spring's JpaRepository, and the service layer method is annotated with Spring's @Transactional. I believe in this setup, auto commit should be implicitly off, and the standalone spring app set-up is working as expected. Is that wrong? How should the persistence unit XML file differ in the OSGI case? Right now the only difference is that the transaction-type is JTA in the karaf case, and I have a declaration like this: osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/payliquid-datasource) – david webber Apr 08 '15 at 16:25