• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Hibernate SessionFactory and java.util.Timer - LogicalConnectionManagedImpl is closed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Setup:

I have Spring core 5 application + hibernate core (5.4.9.Final), to access database I use single hibernate session factory initialized when spring loads and then I get SessionFactory instance by using spring's @Autowired in all my @org.springframework.stereotype.Repository classes.

Code:



When this code gets called by Spring Controller or REST controller, it works fine, but I need to check something periodically so I used java.util.Timer and java.util.TimerTask for that, however when this same code gets called from timer thread I get following error:




Why the session is closed when accesing it from Timer thread ? Spring already by default processes controller requests in many threads and there is not a problem with that.



Messy solution:
I have been able to supress this error by doing following, but this is really messy (+ not even using try-with-resources), however I have not been able to find any solution nor reason why calling this from a timer completely breaks SessionFactory.


The code above works only when call "s = sf.getCurrentSession();" is made first that always throws HibernateException and after that sf.openSession() works for some strange reason.

If I try to add finally block where I try to do s.close() it starts throwing the same error again. So the only messy solution is to never close the session apparently which is really bad.

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opening a session in catch smells bad.  My guess is that java.util.Timer being the client does not support Transaction...you must access the EJB timer service.
 
Sheriff
Posts: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason M smith wrote:Opening a session in catch smells bad.  My guess is that java.util.Timer being the client does not support Transaction...you must access the EJB timer service.


Almost right. However, since this is Spring, a Spring timer is the way to go.

Lukas, the following two will probably help you along:
* https://spring.io/guides/gs/scheduling-tasks/
* https://www.baeldung.com/spring-scheduled-tasks
 
Saloon Keeper
Posts: 23263
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to point out also that legacy Hibernate is not the ORM that I would recommend for modern code. For many years now, Hibernate has supported the Java Persistence Architecture (JPA), which is a JEE-standard ORM protocol, unlike legacy Hibernate, which was a non-standard proprietary solution.

JPA is a component of the Enterprise JavaBeans version 3 standard, and can be used both with EJBs and with non-EJB mechanisms such as in stand-alone apps and partial-stack webapps (for example, Tomcat and jetty), just like legacy Hibernate does.

JPA is supported as part of Spring Data's Spring JPA component.

It's relatively easy to convert most legacy Hibernate to JPA. The Hibernate team was a major contributor to the design of JPA.
 
Lukas Dakado
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, by using @Scheduled annotation above the method instead of java.util.Timer everything works as it should. Also needed to add @EnableScheduling to spring config class.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic