My objective is to provoke an optimistic locking exception in my application using a unit test. I already have a understanding on how to do this in theory. But my problem in practice is how do I maintain a transaction between two threads ?
So, this is what I've done so far:
I am using a JUnit test with:
@RunWith(SpringJUnit4ClassRunner.class)
using EntityManager org.springframework.orm.jpa.JpaTransactionManager
where each method is defined @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
and start a transaction with entityManager.getTransaction().begin();
and end with entityManager.getTransaction().rollback());
This works great, saving, updating etc in single threaded tests.
To create multiple threads I use Springs TaskExecutor (similiar to what's desribed here: Any good Spring threading with a TaskExecutor examples?)
But what do I have to do to maintain the transaction between the two threads? I've tried stuff like annotating the run()-method with @Transactional but this doesn't work.