I have below code,
@Service
public class MyHandlerService {
@Inject
private MyRepoService myRepoService;
@Transactional(value = "manager1", propagation = Propagation.REQUIRES_NEW)
public void handle(final String message) {
myRepoService.save(message);
}
}
@Service
@Transactional("manager1")
public class MyRepoService {
Inject
private final VehicleService vehicleService;
private void save(String message) {
MYVEHICLELIST.getEntries(message).forEach(vehicle -> {
Vehicle vehicle = vehicleService.findById(vehicle.getId());
if (table1ObjectEntity == null)
vehicleService.save(vehicle);
}
});
}
}
@Service
public class VehicleService {
@Inject
private final VehicleRepository repo;
@Transactional(value = "manager1", readOnly = true)
public Vehicle findById(final String id) {
return repo.findById(id);
}
public void save(Vehicle obj) {
repo.save(obj);
}
}
I have two entries in MYVEHICLELIST,
- The first one gets looped and it is saved in the proxy (not committed)
- when the second one is looped, then upon hitting
vehicleService.findById(vehicle.getId())
the first vehicle data in proxy gets committed and flushed to the database
I expect both vehicles to get persisted only when MyHandlerService.handle() method is returned.
TransactionManager used is com.atomikos.icatch.jta.UserTransactionManager