Hibernate works OK, but does not finish the process

3

I do not know if this is normal, but Hibernate starts the process, inserts the object into the database, but its java process is open.

What do I have to do to make this process stop automatically after insertion?

Here is a code that exemplifies the problem:

TestHibernate.java

package testehibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class TesteHibernate
{
    public static void main(String[] args)
    {
        Configuration configuration = new Configuration().configure();

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

        SessionFactory factory = configuration.buildSessionFactory(builder.build());
        Session session = factory.openSession();

        Teste teste = new Teste();
        teste.setNome("Alexandre");
        teste.setEmail("[email protected]");

        session.beginTransaction();
        session.save(teste);
        session.getTransaction().commit();    

        session.close();
        factory.close(); // Não muda nada, o processo continua sem encerrar
    }

}

Test.java

package testehibernate;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teste
{
    @Id
    private int id;
    private String nome;
    private String email;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }    

    public String getNome()
    {
        return nome;
    }

    public void setNome(String nome)
    {
        this.nome = nome;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

}

Test Table:

CREATE TABLE 'teste' (
  'id' int(11) NOT NULL,
  'nome' varchar(255) NOT NULL,
  'email' varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE 'teste'
  ADD PRIMARY KEY ('id');
TO_INCREMENT for table 'teste'

ALTER TABLE 'teste'
  MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

   <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Mapping files -->
        <mapping class="testehibernate.Teste"/>
    </session-factory>

</hibernate-configuration>

Following modified code and working with Murilo's hint to close the registry:

package testehibernate;

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

public class TesteHibernate
{
    public static void main(String[] args)
    {
        Configuration configuration = new Configuration().configure();

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        Session session = factory.openSession();

        Teste teste = new Teste();
        teste.setNome("Alfredo");
        teste.setEmail("[email protected]");

        session.beginTransaction();
        session.save(teste);
        session.getTransaction().commit();    

        session.close();
        factory.close();

        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }

}
    
asked by anonymous 17.02.2017 / 20:45

3 answers

3

Have you tried closing the service registry as well?

package testehibernate;

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

public class TesteHibernate
{
    public static void main(String[] args)
    {
        Configuration configuration = new Configuration().configure();

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        Session session = factory.openSession();

        Teste teste = new Teste();
        teste.setNome("Alexandre");
        teste.setEmail("[email protected]");

        session.beginTransaction();
        session.save(teste);
        session.getTransaction().commit();    

        session.close();
        factory.close();

        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }

}
    
20.02.2017 / 13:22
3

You need to close your SessionFactory using the close method like this:

package testehibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class TesteHibernate {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

        SessionFactory factory = configuration.buildSessionFactory(builder.build());
        Session session = factory.openSession();

        Teste teste = new Teste();
        teste.setNome("Alexandre");
        teste.setEmail("[email protected]");

        session.beginTransaction();
        session.save(teste);
        session.getTransaction().commit();    

        session.close();
        factory.close();                //<<<----Fechando a SessionFactory
    }

}

Edit:

This is a bug specific to the version you are using (version 4.3.0.Final) caused by the connection pool of Hibernate, as can be seen here . As several new versions have been released, you should ideally upgrade to a version where has been fixed.

If you can not do this, use some other connection pool since the hibernate connection pool is not to be used in production or add the following line to the end of your code:

>
StandardServiceRegistryBuilder.destroy(builder);
    
17.02.2017 / 21:08
-2

Add in the end:

 HibernateUtil.getSessionFactory().close();
    
29.08.2018 / 20:17