Question

We know that two threads manipulating the same entity at the same time, will throw an OptimisticLockException if all defaults apply. What happens if the field(s) that the threads manipulated, was marked with the annotation @Transient or modifier transient?

My intuition says that the persistence provider shall not bother one bit what we do with our transient fields and how they are accessed. That further tells me that synchronization mechanisms should be applied to these fields, if we deem that important enough in our application.

However, I have googled all my Java EE books and the JPA 2.0 specification but I cannot find this "issue" addressed. That tells me I must be missing something here and that I'm overly worried??

Était-ce utile?

La solution

OptimisticLockException will only be used if there is an @Version field in the entity, and if a transaction tries to save an entity that has been modified by another transaction since the state of the entity has been loaded.

Each transaction has its own instance of each entity it loads. Entities are not thread-safe, and must not be shared by several threads.

Transient fields are indeed ignored completely by JPA. But I don't see how synchronization could change anything, since each thread has its own entity instance. Moreover, in most enterprise applications, multiple JVMs use the same database, so synchronization doesn't help there. Frankly, using transient fields in entities usually show a design problem, and relying on the state of a transient field in an entity to hold state shared by multiple threads is plain wrong. If the state is shared by multiple threads or even processes, it should probably be saved in the database.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top