Wednesday, March 27, 2013

Spring @Transactional boundaries

InvalidDataAccessApiUsageException: detached entity passed to persist:

I experienced the exception above recently and thought it was simply, something like one of the following links:

Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

In fact though it was because I was executing the queries across two different transactions and once I added @Transactional to the service method they were being executed in -- it changed to the same transaction.

1 comment: