playframework / playframework/play1

[play1.5.2] transaction control in thread

Open
#1,296 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.6k
Forks
671
Avg merge
12d 15h
Merged PRs (30d)
1

Description

Execute the following code in play 1.5.2

@Test
public void test01() {
	new Thread(() -> {
		EntityManager em = JPA.createEntityManager();
		em.setFlushMode(FlushModeType.COMMIT);
		JPA.bindForCurrentThread(JPA.DEFAULT, em, false);
		JPA.em().getTransaction().begin();
		t_user user = t_user.findById(1L);
		user.name = "123456";
		System.out.println(user.name);
	        JPA.em().getTransaction().commit();
	}).start();
}

The following error occurred

Exception in thread "Thread-4" java.lang.IllegalStateException: Session/EntityManager is closed
at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:337)
at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:135)
at org.hibernate.internal.AbstractSharedSessionContract.checkOpenOrWaitingForAutoClose(AbstractSharedSessionContract.java:343)
at org.hibernate.internal.SessionImpl.afterTransactionBegin(SessionImpl.java:3190)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.afterTransactionBegin(JdbcCoordinatorImpl.java:462)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.afterBeginCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:136)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$000(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:204)
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:56)
at BasicTest.lambda$0(BasicTest.java:42)
at java.lang.Thread.run(Thread.java:748)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in BasicTest.java at line 42 and trace JPA.bindForCurrentThread, JPA.em(), and the EntityManager transaction calls used from the new thread. Run the example against Play 1.5.2 and inspect the reported closed-session path; done means the reproduced transaction behavior is understood and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.