0

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,

  1. The first one gets looped and it is saved in the proxy (not committed)
  2. 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

SUBHASHC37
  • 23
  • 5
  • It doesn't get committed it gets flushed because you are issuing a query and it needs to have the full state for the transaction. Flushing != commiting. – M. Deinum Jul 17 '23 at 12:24
  • @M.Deinum it gets persisted in the database, is that expected? – SUBHASHC37 Jul 17 '23 at 13:05
  • 1
    How do you know it gets persisted? It should only flush and execute the query, the fact tha tyou see a query doesn' t mean it is committed. If it is there is something wrong in your setup, but there is too little information to make an informed guess. Looking at the code what you are doing wouldn't even work/compile here, so please show some actual code instead of some non working dummy code. – M. Deinum Jul 17 '23 at 13:31

0 Answers0