I have a provider where I can always read Entities but I was never write some until I set the @TransactionManagement(TransactionManagementType.CONTAINER)
on class level and the @Transactional
on the overridden filter method.
@Provider
@Priority(value = 1)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SecurityUsageResponseFilter implements ContainerResponseFilter
{
@PersistenceContext(unitName = "MyAppPersistenceUnit")
private EntityManager entityManager;
@Override
@Transactional
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException
{
//code
updateUserAndHeaderInformation(id, responseContext);
//code
}
private void updateUserAndHeaderInformation(Object userId, ContainerResponseContext responseContext)
{
//This ALWAYS work
RestrictedUsers user = entityManager.find(RestrictedUsers.class, userId);
//Only works when transactions are explicitly set
user.setLastTransferToken(newTransferToken);
user.setLastObfuscationId(newObfuscationId);
}
}
My question is: Are there maybe any side effects when the filter
method is suddenly transactional? Usually transactions are only used in service methods, but here I'm using a provider any maybe there is some different behaviour in the background which could interfere things I didn't think about yet?
It may work but I wouldn't do that.
Your filter should be kept lean and these operations should be delegated to the service layer, which is responsible for demarcating the transactions.