javax.persistence.TransactionRequiredException: Transaction is not active, consider adding @Transactional to your method to automatically activate one.
The weird part is that the run() method of the task is indeed annotated with @Transactional.
Any idea how to address this?
@Path("/hello")
public class GreetingResource {
@Inject ManagedExecutor executor;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Transactional
public String hello() {
PersonTask task= new PersonTask();
executor.execute(task);
return "hello";
}
}
@Entity
public class Person extends PanacheEntity {
public String name=UUID.randomUUID().toString();
}
public class PersonTask implements Runnable{
@Transactional
public void run() {
Person p = new Person();
p.persistAndFlush();
}
}
The error is:
ARJUNA012094: Commit of action id 0:ffffc0a8010e:e950:60200089:0 invoked while multiple threads active within it.
ARJUNA012107: CheckedAction::check - atomic action 0:ffffc0a8010e:e950:60200089:0 commiting with 2 threads active!
Thread Thread[executor-thread-2,5,executor] threw an uncaught exception: javax.persistence.TransactionRequiredException: Transaction is not active, consider adding @Transactional to your method to automatically activate one.
--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/aa0713ca-89a7-4f96-830a-22b49d6e91a4n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CABEjM6Qsv%2B0y0QzpwbD2cKb7t%3Dp8gdzwgywzpcR76eUcrjgHjg%40mail.gmail.com.
@Path("/hello")
public class GreetingResource {
@Inject ManagedExecutor executor;
@Inject TransactionManager tm;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
PersonTask task= new PersonTask();
task.setTm(tm);
executor.execute(task);
return "hello";
}
}
public class PersonTask implements Runnable{
protected TransactionManager tm;
public void setTm(TransactionManager tm) {
this.tm = tm;
}
@Override
public void run() {
try {
tm.begin();
Person p = new Person();
p.persistAndFlush();
tm.commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
@Path("/hello")
public class GreetingResource {
@Inject ManagedExecutor executor;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Transactional
public CompletionStage<String> hello() {
PersonTask task= new PersonTask();
return executor.runAsync(task).thenApply(v -> "hello");
}
}
@Entity
public class Person extends PanacheEntity {
public String name=UUID.randomUUID().toString();
}
public class PersonTask implements Runnable{
public void run() {
Person p = new Person();
p.persistAndFlush();
}
}
At least this way, you're sure the task is done before you return your response to the user, and we won't close the transaction before you're done.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CABEjM6Qsv%2B0y0QzpwbD2cKb7t%3Dp8gdzwgywzpcR76eUcrjgHjg%40mail.gmail.com.
@Path("/hello")
public class GreetingResource {
@Inject Service service;
@Inject ManagedExecutor executor;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
PersonTask task= new PersonTask();
executor.execute(() -> service.run());
return "hello";
}
}
@Entity
public class Person extends PanacheEntity {
public String name=UUID.randomUUID().toString();
}
@ApplicationScoped
public class Service {
@Transactional