Search code examples
mysqlhibernatespring-mvctransactionsspring-transactions

I want to Ignore a Specific Entity from being @Transactional (using Spring/Hibernate)


I run a SpringMVC application and I'm using the @Transactional annotations in conjunction with a HibernateTransactionManager.

As a result, anywhere in my application that I throw a RuntimeException, we roll back- this is great! Everything works perfectly.

But now I'd like to exclude a single Entity from the Transactionality. This Entity represents something that I want persisted (benchmarks/logs/errors) even if (especially if) there is a RuntimeException which marks everything else for Rollback.

I'd rather not have to call some repository from outside of my Interceptor which begins and ends the Transaction.

Is there a straightforward way to accomplish this?


Solution

  • An entity is not transactional. A method is.

    If you want some part of your algorithm to not be part of the current transaction, then put that part in a method of a separate bean, and annotate it with @Transactional(propagation = REQUIRES_NEW), so that a dedicated, separate transaction is opened and committed when invoking this method, unaffected by the outcome of the current transaction.