Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program using hibernate does not terminate

Tags:

java

hibernate

I created a program using Hibernate.

The program reaches the main function end, nevertheless the program is running.

I wonder if it happens when SessionFactory is configured using Hibernate Version 4.x.

Is the way to configure wrong?


manual1_1_first_hibernate_apps.java

public static void main(String[] args) {      args[0] ="list";     if (args.length <= 0) {         System.err.println("argement was not given");         return;     }      manual1_1_first_hibernate_apps mgr = new manual1_1_first_hibernate_apps();      if (args[0].equals("store")) {         mgr.createAndStoreEvent("My Event", new Date());     }     else if (args[0].equals("list")) {         mgr.<Event>listEvents().stream()             .map(e -> "Event: " + e.getTitle() + " Time: " + e.getDate())             .forEach(System.out::println);     }     Util.getSessionFactory().close(); }  private <T> List<T> listEvents() {     Session session = Util.getSessionFactory().getCurrentSession();     session.beginTransaction();     List<T> events = Util.autoCast(session.createQuery("from Event").list());     session.getTransaction().commit();     return events; } 

Util.java

private static final SessionFactory sessionFactory;  /**  * build a SessionFactory  */ static {     try {         // Create the SessionFactory from hibernate.cfg.xml          // hibernate version lower than 4.x are as follows         // # it successful termination. but buildSessionFactory method is deprecated.         // sessionFactory = new Configuration().configure().buildSessionFactory();          // version 4.3 and later         // # it does not terminate. I manually terminated.         Configuration configuration = new Configuration().configure();         StandardServiceRegistry serviceRegistry =                  new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();         sessionFactory = configuration.buildSessionFactory(serviceRegistry);     }     catch (Throwable ex) {         // Make sure you log the exception, as it might be swallowed         System.err.println("Initial SessionFactory creation failed." + ex);         throw new ExceptionInInitializerError(ex);     } }  /**  * @return built SessionFactory  */ public static SessionFactory getSessionFactory() {     return sessionFactory; } 

The following console log snippets when program terminate and use buildSessionFactory method.

2 08, 2014 8:42:25 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH000030: Cleaning up connection pool [jdbc:derby:D:\Java\jdk1.7.0_03(x86)\db\bin\testdb] 

but if do not use deprecated buildSessionFactory method and terminated(program is running), the above two lines do not appear.

ENVIRONMENT:

  Hibernate 4.3.1  DERBY  JRE 1.8  IntelliJ IDEA 13 
like image 637
tomop Avatar asked Feb 08 '14 11:02

tomop


People also ask

What happens if Hibernate session is not closed?

When you don't close your Hibernate sessions and therefore do not release JDBC connections, you have what is typically called Connection leak. So, after a number of requests (depending on the size of your connection pool) the server will not be able to acquire a connection to respond your request.

Is Hibernate deprecated?

No, Hibernate is not deprecated.


1 Answers

I met this problem also today, and I found the solution is, in the end of your main method (or thread), you should close your Session Factory, like:

sessionFactory.close(); 

And then, your program will terminate normally.

If You use JavaFX 8 in main method add:

@Override public void stop() throws Exception {     sessionFactory.close(); } 

This method will close session factory and destroy thread on program exit.

like image 63
Rxxxx Avatar answered Oct 15 '22 22:10

Rxxxx