In my web client, where I started using Spring WebFlux, I got following exception:
reactor.core.Exceptions$ErrorCallbackNotImplemented: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
Code:
@Service
@Transactional
@Slf4j
public class PersonSyncServiceImpl
extends HotelApiCommunicationService implements PersonSyncService {
private static final String REST_ENDPOINT_PATH = "/api/sync/person";
@PersistenceContext
private EntityManager em;
@Autowired
private SynchronizationService syncMgr;
@Override
public int initialSyncPersonData(ObiektDTO obiekt) {
WebClient client = WebClient.create("http://" + obiekt.getAdresIp());
AtomicInteger count = new AtomicInteger(0);
Flux<Person> personFlux = client.get()
.uri(REST_ENDPOINT_PATH)
.retrieve()
.bodyToFlux(Person.class);
personFlux.subscribe(person -> {
CNPerson cnPerson = syncMgr.convertObject(person, CNPerson.class);
cnPerson.setObiektLoid(obiekt.getLoid());
em.persist(cnPerson);
count.getAndIncrement();
});
return count.get();
}
}
I know that the problem is on this line, because reactor can't get entity manager.
em.persist(cnPerson);
Controller method on server:
@GetMapping(value = "/person", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Person> getPersonList() {
return Flux.fromStream(
personDao.findAll().stream());
}
How to fix it? How to access transaction with reactor and using JPA save a record to database?