Multiple transactions with JPAContainer

Investigating JpaContainer I need to know if I can use it to run more than one DB transaction at same time.
I mean I want to have a window inserting data with one transaction (and its own commit point) and another window doing similar work in a different transaction.
Is it possible ?
Tks
Tullio

Hi,

Yes it is possible, however you need to create your own EntityProvider by extending a suitable one for the transaction management to work correctly due to a bug in JPAContainer. Here’s an example:

@TransactionManagement
public abstract class EjbEntityProvider<T> extends CachingMutableLocalEntityProvider<T> {

    @EJB(beanName = "GenericDao")
    protected GenericDao dao;

    protected EjbEntityProvider(Class entityClass) {
        super(entityClass);
    }

    @PostConstruct
    public void init() {
        setTransactionsHandledByProvider(false);
        setEntityManager(dao.getEntityManager());
        setEntitiesDetached(false);

    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    protected void runInTransaction(Runnable operation) {
        super.runInTransaction(operation);
    }

    @Override
    public void updateEntityProperty(Object entityId, String propertyName,
            Object propertyValue) throws IllegalArgumentException {
        // Override to do nothing in order to not update entities after OptimisticLockExceptions
    }
}

Using the EntityProvider above, you should get an OptimisticLockException whenever two users edit the same property/properties of the same item. It is up to your program to handle these exceptions in some way.

HTH,
/Jonatan