Hibernate works OK, but does not finish the process

Asked

Viewed 377 times

3

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

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

Follows a code exemplifying the problem:

Testehibernate.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
    }

}

Java test.

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>

Follow code modified and working with Murilo’s tip to close 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);
    }

}

3 answers

3

You need to close your SessionFactory using the method closethus:

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:

That’s a bug specific version you are using (version 4.3.0.Final) caused by Connection pool of Hibernate, as can be seen here. Since several new versions have already been released, the ideal would be for you to upgrade to a version where the bug has aids corrected.

If you can’t 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);
  • Felipe, at home I had done closed the tb Factory but it was not. I gave +1 by will and tb pq I am not in front of the computer to test again. But if you can test and see if this will solve it would help a lot. When I get home I will see it again. If it works accepted as a response. But at first though it is not in question I had tried before closing Factory tb and gave in the same.

  • 1

    @Antonioalexandre I tested and worked normally. Which version of Hibernate you are using?

  • I will only be able to see the version now on Monday when I get home. When talking about working work, well... works. The point is that something in Hibernate keeps it running and doesn’t let the process shut down naturally. That’s the problem.

  • Felipe, the version of Hibernate I am using in the test is 4.3. In this version only closing the Factory does not release the process after execution. I will test with other versions. The question is still open to those who have other ideas.

  • 1

    @Antonioalexandre This is a bug of the version you are using, I edited my reply with more details.

  • Thank you for clarifying Felipe! I will download the newest version and base my development on it.

Show 1 more comment

3


Already tried to close the service Registry, thus?

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);
    }

}
  • Solved. For your answer to be perfect, just include import org.hibernate.service.Serviceregistry; Big hug. :)

  • Done! Thank you! Hug!

-2

Add at the end:

 HibernateUtil.getSessionFactory().close();
  • At the end of which function? In which line exactly? Be clearer to help future people with the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.