Hi!
I am developing an application (deployed on JBoss 5.1.0.GA) containing a method where a single entity is picked from a collection selected from the database. Concurrent access for this method should be possible and handled like "take the first entity and mark it as reserved, if it is still reserved by another process just pick the next entity and so on". My solution for this is to iterate over the entities of the collection and write-lock the entities in a nested transaction until the lock invocation returns without a lock-exception. The nested transaction is neccessary because Hibernate sets the Transaction to rollback only if the lock fails.
The Problem is that we use JPA/JTA that doesn't support nested transactions. So I tried to implement a DataSourceTransactionManager with aspect orientated spring-aop for the method that locks the entity to handle it with JDBC, but I don't have access to the parent transaction/database-connection there. So the entitymanager still works on the parent transaction and set it to rollback-only when the lock fails. To avoid the exception handling of Hibernate I tried to do the locking and exception handling in a stored procedure, but calling stored procedures is not supported for JPA.
So what is the common solution for this kind of concurrency handling?
I also thought about extending the hibernate implementation and overriding the lock method in order zu change the exception handling. Is that possible?
Thanks in advice, Chistian
|