Back Forum Reply New

Spring Transaction Rollback and Hibernate setAlwaysUseNewSession

Hi I'm a bit of a newbie to all of this.

I've got a scenario where I'm creating a Hibernate template within a Spring transaction. Within that template I'm updating a database within a Hibernate transaction. Later on in the Spring transaction I'm getting a (legitimate) exception, causing a rollback.

If on creation of the Hibernate template, I call setAlwaysUseNewSession(false) then as part of the Spring rollback, the Hibernate transaction is rolled back too (which is what I want).

If I call setAlwaysUseNewSession(true), then the Hibernate transaction won't be rolled back as part of the Spring transaction.

However, from the Spring docs for setAlwaysUseNewSession(true): quot;Within a transaction, a new Hibernate Session used by this template will participate in the transaction through using the same JDBC Connection. In such a scenario, multiple Sessions will participate in the same database transactionquot;.

Doesn't this mean the Hibernate transaction should be rolled back when the Spring transaction is rolled back, or is there some subtlety I'm missing in all of this?

I'm happy to post my config/code if required, but I was hoping to see if anyone has an answer to the question posed above, as I can get a variation in the behaviour just by calling setAlwaysUseNewSession with a different value, so I was hoping someone more familiar with the interactions between the two transactional models might help.

Cheers

Jez
¥
Back Forum Reply New