1

When we set hibernate.transaction.auto_close_session to true, it is throwing java.lang.IllegalStateException: Session/EntityManager is closed .

We are using hibernate 5.3.18.Final and spring 5.3.28 versions.

When we set hibernate.transaction.auto_close_session to false above error is not seen. However we want that property to be true in our codebase.

Could you please confirm the above error is resolved in which versions of hibernate? or is it safe to ignore the above error?

Thanks in Advance.

Anuja
  • 11
  • 2

1 Answers1

0

Set hibernate.transaction.auto_close_session to true means that session will be automatically closed when the transaction completes.

If the session is already closed and then you call EntityManager#close() or Session#close() , it will throw that IllegalStateException. There is no harm and so you could simply try-catch it to make the codes keep running instead of interrupted by it.

But a better idea is to use EntityManager#isOpen() or Session#isOpen() to check if the session is already closed before calling EntityManager#close() or Session#close() .

Something like :

if (entityManager.isOpen()){
  entityManager.close();
}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Thanks for the response @ken-chan. Just wanted to know is there any compatibility issue between spring and hibernates version which we are using that causing this error? – Anuja Jul 06 '23 at 07:50
  • don't think it is related. spring 5 should well support hibernate 5.3. Also if you think my answer is helpful to you , please consider to upvote it or even mark it as accepted answer. i spend so much time to research and write this answer for you for free . thanks. – Ken Chan Jul 06 '23 at 13:15
  • I found https://hibernate.atlassian.net/browse/HHH-11311?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel while searching for the issue. Just wanted to know in which version of hibernate the issue is resolved? – Anuja Jul 24 '23 at 11:42