miran miran - 2 months ago 17
Java Question

Using REQUIRES_NEW JTA transaction type in MyBatis on Wildfly

I am trying to use mybatis-cdi within Wildfly and I need to run part of method in separate transaction - see following snippet:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ItemService {

@Inject
@Mapper
ItemMapper mapper;

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void doSomething() {
List<Item> items = mapper.findByTime(new Date());
for(Item i : items) {
update(i);
}
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void update(Item item) {
mapper.updateSomehow(item);
}
}


Is this correct way how to do it? Is this supported with mybatis-cdi, or should it be done some different way?

Answer

You're not getting your expected behavior as interceptors are not applied when using a self reference in a method, for example how doSomething() calls update() directly. You would need that to occur in a separate bean to allow a separate transaction interaction.

Comments