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);
}
}
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.
– Antonio Alexandre
@Antonioalexandre I tested and worked normally. Which version of Hibernate you are using?
– Felipe Marinho
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.
– Antonio Alexandre
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.
– Antonio Alexandre
@Antonioalexandre This is a bug of the version you are using, I edited my reply with more details.
– Felipe Marinho
Thank you for clarifying Felipe! I will download the newest version and base my development on it.
– Antonio Alexandre