2

I followed the official hibernate tutorial and also several others that describe how to get started with Hibernate (e.g. Hibernate Many To Many Mapping Join Tables Annotation and XML Configuration Example).

I have a local MySQL Server running under Windows 7 and everything seems to work fine. I can connect using the XML configuration file and Hibernate inserts data into my test database but I cannot properly close the hibernate connection. I get the error message that the connection was not properly closed even if it received the request to do so.

I googled around and saw that many people had that problem a while ago, but could not find any recent information on how to solve the problem.

How is it possible that the official tutorial leads to such a problem? Thank you for anyone of how to suggest of how to tackle this problem.

EDIT: This is the warning messages that I get. There is about a 15s pause between the program printing out the Item2 ID=53 line and the WARNING messages popping up afterwards:

Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into CART (cart_total) values (?)
Before committing transaction
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Cart ID=53
Cart1 ID=54
Item1 ID=54
Item2 ID=53
[WARNING] thread Thread[Abandoned connection cleanup thread,5,com.journaldev.hibernate.main.HibernateManyToManyMain] was interrupted but is still aliv
e after waiting at least 15000msecs
[WARNING] thread Thread[Abandoned connection cleanup thread,5,com.journaldev.hibernate.main.HibernateManyToManyMain] will linger despite being asked t
o die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the ru
nning code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=com.journaldev.hibernate.main.HibernateManyToManyM
ain,maxpri=10]
java.lang.IllegalThreadStateException
        at java.lang.ThreadGroup.destroy(ThreadGroup.java:775)
        at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:328)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.936 s
[INFO] Finished at: 2015-04-28T13:11:56+02:00
[INFO] Final Memory: 12M/33M
[INFO] ------------------------------------------------------------------------

Here is my hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
        <property name="hibernate.connection.username">test</property>
        <property name="hibernate.connection.password">test</property>        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>

        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>
        <property name="hibernate.c3p0.max_size">10</property>
        <property name="hibernate.c3p0.max_statements">10</property>
        <property name="hibernate.c3p0.min_size">10</property>
        <property name="hibernate.c3p0.timeout">100</property>

        <mapping resource="com/journaldev/hibernate/model/cart.hbm.xml" />
        <mapping resource="com/journaldev/hibernate/model/item.hbm.xml" />
    </session-factory>
</hibernate-configuration>

This is the HibernateUtil that I copied from the manual above:

package com.journaldev.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            System.out.println("Hibernate Configuration loaded");

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            System.out.println("Hibernate serviceRegistry created");

            SessionFactory sessionFactory = configuration
                    .buildSessionFactory(serviceRegistry);

            return sessionFactory;
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null)
            sessionFactory = buildSessionFactory();
        return sessionFactory;
    }

}

This is the relevant class that is executed

package com.journaldev.hibernate.main;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.journaldev.hibernate.model.Cart;
import com.journaldev.hibernate.model.Item;
import com.journaldev.hibernate.util.HibernateUtil;

public class HibernateManyToManyMain {

    //Saving many-to-many where Cart is primary
    public static void main(String[] args) {

        Item iphone = new Item();
        iphone.setPrice(100); iphone.setDescription("iPhone");

        Item ipod = new Item();
        ipod.setPrice(50); ipod.setDescription("iPod");

        Set<Item> items = new HashSet<Item>();
        items.add(iphone); items.add(ipod);

        Cart cart = new Cart();
        cart.setItems(items);
        cart.setTotal(150);

        Cart cart1 = new Cart();
        Set<Item> items1 = new HashSet<Item>();
        items1.add(iphone);
        cart1.setItems(items1);
        cart1.setTotal(100);

        SessionFactory sessionFactory = null;
        try{
        sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(cart);
        session.save(cart1);
        System.out.println("Before committing transaction");
        tx.commit();
        sessionFactory.close();

        System.out.println("Cart ID="+cart.getId());
        System.out.println("Cart1 ID="+cart1.getId());
        System.out.println("Item1 ID="+iphone.getId());
        System.out.println("Item2 ID="+ipod.getId());

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close();
        }

    }

}

I would have expected that the command sessionFactory.close() would properly shutdown and close everything. Here are the versions that I am using with the maven command mvn compile exec:java -Dexec.mainClass=com.journaldev.hibernate.main.HibernateManyToManyMain:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.journaldev.hibernate</groupId>
    <artifactId>HibernateManyToManyMapping</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.9.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.3.9.Final</version>
        </dependency>   
    </dependencies>

</project>

If anybody could point me to a direction of how to solve this problem, I would be very thankful.


New trial with JPADemo.java. I am still having the same problem:

package com.cjg;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.cjg.Employee;


public class JPADemo
{
        public static void main(String[] args)
    {
                EntityManagerFactory emf = Persistence.createEntityManagerFactory("cjg-JPA");
                EntityManager em = emf.createEntityManager();

                em.getTransaction().begin();
                //Employee employee = new Employee();
                //employee.setName("Chandan");
                //System.out.println("COMITTING");
                //em.persist(employee);
                em.getTransaction().commit();
                System.out.println("COMITTING finished");

                em.close();
                emf.close();
                //System.out.println("after EntityManager.close()");

                //System.exit(0);
    }
}

relevant part of pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.9.Final</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>

my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">

        <persistence-unit name="cjg-JPA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.username" value="test" />
            <property name="hibernate.connection.password" value="test" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/test" />
        </properties>
        </persistence-unit>
</persistence>

Am I maybe using an old version of something? I am fairly experienced with programming in general and know how to troubleshoot code but I don't seem to understand the cause of this problem at all. When looking at the connections at the MySQL Server side, I can see that em.close() actually closes the connection. Somehow only the pool is not closed properly. My output is

INFO: HHH000230: Schema export complete
COMITTING finished
Mai 01, 2015 1:08:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost/test]
[WARNING] thread Thread[Abandoned connection cleanup thread,5,com.cjg.JPADemo] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[Abandoned connection cleanup thread,5,com.cjg.JPADemo] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the ru
nning code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=com.cjg.JPADemo,maxpri=10]
java.lang.IllegalThreadStateException
        at java.lang.ThreadGroup.destroy(ThreadGroup.java:775)
        at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:328)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Thank you and have a nice weekend. Stephan

stephanmunich
  • 363
  • 2
  • 11
  • What exactly is the error message? – vanOekel Apr 26 '15 at 20:18
  • How do u know ur Hibernate session isn't closed properly ? Do you get any exception ? If yes, then show us. Add some code snippet where do u get this exception for better understanding. – OO7 Apr 27 '15 at 09:29
  • Dear all, I am still having the same problem. I have tried a new function but have the same problem, see above. Thank you if anybody knows what I am doing wrong. – stephanmunich May 01 '15 at 11:03
  • Googled "ThreadGroup.destroy(ThreadGroup.java:775)" and found [this answer](http://stackoverflow.com/a/28017081/3080094) – vanOekel May 01 '15 at 12:41

1 Answers1

5

thank you for your helpful comments. I have found the solution. One needs to call maven with the additional flag -Dexec.cleanupDaemonThreads=false. If anybody knows why this is the case, please let me know. However, this definitely solves the above problem. So in the end, it did not actually have anything to do with hibernate but was a problem with maven instead. If you don't want to enter mvn compile exec:java -Dexec.mainClass=com.journaldev.hibernate.main.HibernateManyToManyMain -Dexec.cleanupDaemonThreads=false everytime you want to execute the project, you can also add

<project>
...
<build>
<plugins>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
       <mainClass>com.cjg.JPADemo</mainClass>
       <cleanupDaemonThreads>false</cleanupDaemonThreads>
  </configuration>
</plugin>
</plugins>
</build>

</project>

to your pom.xml to achieve the saim result. Again, if anybody can explain what this actually does or why this is necessary, I would be very curious, because it took me nearly a week to solve this issue. Thank you to everybody for your help.

stephanmunich
  • 363
  • 2
  • 11