1

I have the following code running on Wildfly 8.0.0.Final:

@Stateful
public class MyJPABean<T> {

    @PersistenceContext(unitName = "myUnitName")
    private EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void write(T entity) {
        em.persist(entity);
        em.flush();
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void update(T entity) {
        em.merge(entity);
    }
}

Both write and update methods are called parallely, because they are called directly from another class which handles HTTP requests:

@Path("api")
public class MyApiController {

    private MyJPABean<MyJpaModel> jpaBean;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public void handlePost(MyJpaModel myJpaModel) {
       jpaBean.write(myJpaModel);
    }
}

Some of the the "write" method calls are successful, but from time to time, when the "write" method is called parallely by multiple threads, it starts to hang on em.persist(entity).

At PostgreSQL server side I can see transaction in state: "idle in transaction" with query: "select nextval ('hibernate_sequence')" or insert query that was created from the entity I just tried to persist.

When this problem happens I am not able to persist entities anymore. Everything hangs on em.persist(entity). Either I am not able to kill wildfly process, I have to kill it with -9 signal.

Is there anything I am doing wrong?

After some time (when the timeout occurs) I can see the following warnings in the application logs:

16:44:30,590 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:2d in state  RUN
16:44:30,591 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper Worker 0) ARJUNA012095: Abort of action id 0:ffff7f000101:-231d9b5d:55030494:2d invoked while multiple threads active within it.
16:44:30,591 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper Worker 0) ARJUNA012108: CheckedAction::check - atomic action 0:ffff7f000101:-231d9b5d:55030494:2d aborting with 1 threads active!
16:44:30,598 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:35 in state  RUN
16:44:31,090 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:2d in state  CANCEL
16:44:31,092 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012378: ReaperElement appears to be wedged: com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.afterCompletion(TwoPhaseCoordinator.java:487)
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.afterCompletion(TwoPhaseCoordinator.java:463)
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.cancel(TwoPhaseCoordinator.java:118)
com.arjuna.ats.arjuna.AtomicAction.cancel(AtomicAction.java:215)
com.arjuna.ats.arjuna.coordinator.TransactionReaper.doCancellations(TransactionReaper.java:377)
com.arjuna.ats.internal.arjuna.coordinator.ReaperWorkerThread.run(ReaperWorkerThread.java:78)

16:44:31,100 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:35 in state  SCHEDULE_CANCEL
16:44:31,594 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:2d in state  CANCEL_INTERRUPTED
16:44:31,595 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012120: TransactionReaper::check worker Thread[Transaction Reaper Worker 0,5,main] not responding to interrupt when cancelling TX 0:ffff7f000101:-231d9b5d:55030494:2d -- worker marked as zombie and TX scheduled for mark-as-rollback
16:44:31,596 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper Worker 1) ARJUNA012095: Abort of action id 0:ffff7f000101:-231d9b5d:55030494:35 invoked while multiple threads active within it.
16:44:31,597 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012110: TransactionReaper::check successfuly marked TX 0:ffff7f000101:-231d9b5d:55030494:2d as rollback only
16:44:31,597 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper Worker 1) ARJUNA012108: CheckedAction::check - atomic action 0:ffff7f000101:-231d9b5d:55030494:35 aborting with 1 threads active!
16:44:31,600 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff7f000101:-231d9b5d:55030494:35 in state  CANCEL
16:44:31,602 WARN  [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012378: ReaperElement appears to be wedged: org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:231)

As a jdbc driver, "postgresql-9.2-1004.jdbc41" is used. PostgreSQL server version is "9.3". I also had c3p0 on the classpath, but even when I delete this dependency, problem apeears.

Konrad
  • 1,605
  • 3
  • 24
  • 45
  • Did you found a solution? ;) http://stackoverflow.com/questions/28992996/multithreading-transaction-hanging-during-persisting-entity-to-postgresql-via-wi – Marcin Erbel Mar 13 '15 at 16:38
  • I found out that it happens also on different datasources (h2). It looks like there is a problem with wildfly jta manager. Still don't know what's going on. If someone has an example app where jaxrs is used together with ejb and jpa please share and I will compare. I don't want to use spring. – Konrad Mar 15 '15 at 09:18
  • Does `MyJPABean` really need to be @Stateful? These kind of service beans are normally @Stateless. Note that it is an error for more than one thread to access the same @Stateful bean instance at the same time. – Steve C Mar 21 '15 at 06:17

1 Answers1

1

I've prepared a sample application using JAX-RS, EJB and JPA - https://github.com/aparnachaudhary/prototypes/tree/master/jaxrs-db-access. I used the JMeter script to access the REST endpoint with multiple threads. But I could not reproduce the issue with PostgreSQL 9.3, postgresql-9.3-1103.jdbc41.jar and WildFly 8.1.0.Final.

Aparna Chaudhary
  • 1,325
  • 8
  • 10
  • Thanks, I will try and come back with my test results. – Konrad Mar 16 '15 at 06:48
  • Your's app works fine. I will try to compare differences (libraries, persistence and datasource configurations). – Konrad Mar 16 '15 at 08:15
  • Look on that:http://stackoverflow.com/questions/634102/log4j-is-hanging-my-application-what-am-i-doing-wrong – Marcin Erbel Mar 17 '15 at 12:01
  • 1
    I compared my application with yours and and came to the same conclusion as Marcin Erbel - the problem was with the logger configuration. See this topic: http://stackoverflow.com/questions/634102/log4j-is-hanging-my-application-what-am-i-doing-wrong – Konrad Mar 20 '15 at 15:07