Slow opening of Jframe with Hibernate

Asked

Viewed 91 times

2

Good morning to all.

I have a Swing application that has a Jframe using Hibernate 3 for networked Mysql BD persistence. When I run through Netbeans, it opens very fast.

But when I run the jar built on another computer, this Jframe takes too long to open, reaching almost 1 minute of waiting. I’m suspicious of Hibernate, but because Netbeans opens so fast, in 1 or 2 seconds, if it also needs to access the network to get to the BD ?

From now on, thank you all for your cooperation.


Entity Manager

private EntityManager getEntityManager() {
    EntityManagerFactory factory = null;
    EntityManager entityManager = null;

    try {
        //Obtém o factory a partir da unidade de persistência.
        factory = Persistence.createEntityManagerFactory("AtualizacaoCadastralPU");
        //Cria um entity manager.
        entityManager = factory.createEntityManager();
    //Fecha o factory para liberar os recursos utilizado.
    } finally {
        factory.close();
    }

    return entityManager;

}

Queries

public List<BeanTiposOcorrencia> buscaTipos(){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("BeanTiposOcorrencia.findAll");
    em.getTransaction().begin();
    em.getTransaction().commit();

    return query.getResultList();

}

public List<BeanSubtiposOcorrencias> buscaSubTipos(){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("BeanSubtiposOcorrencias.findAllComTipo");
    em.getTransaction().begin();
    em.getTransaction().commit();

    return query.getResultList();

}

public String buscaAtividade(String protocolo){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("ResultAtividade.findAtividade");
    query.setParameter(1, protocolo);
    em.getTransaction().begin();
    em.getTransaction().commit();

    return ((ResultAtividade)query.getResultList()).getAtividade();

}

public String buscaDetalhe(){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("BeanSubtiposOcorrencias.findAll");
    em.getTransaction().begin();
    em.getTransaction().commit();

    return ((BeanSubtiposOcorrencias)query.getSingleResult()).getDescricao();

}

public List<ResultAgrupamento> buscaAgrupamentos(){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("ResultAgrupamento.findAll");
    em.getTransaction().begin();
    em.getTransaction().commit();

    return query.getResultList();

}

public List<ResultDetalhe> buscaDetalhes(){

    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("ResultDetalhe.findAll");
    em.getTransaction().begin();
    em.getTransaction().commit();

    return query.getResultList();

}

Persistence unit

<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" 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_1_0.xsd"> <persistence-unit name="AtualizacaoCadastralPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>entity.BeanOcorrenciasGsv</class> <class>entity.BeanTiposOcorrencia</class> <class>entity.BeanSubtiposOcorrencias</class> <properties> <property name="hibernate.connection.username" value="xxx"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value="xxx"/> <property name="hibernate.connection.url" value="jdbc:mysql://xxxxx"/> <property name="hibernate.show.sql" value="true"/> </properties> </persistence-unit> </persistence>

I have seen reports of other complaints about the slowness of networked Hibernate, but my biggest doubt is because when running in Netbeans this slowness does not occur.

I hope I have improved the search effort and made my question clear and useful.

  • 1

    Without seeing the code it is difficult to identify the problem, add the code in the question.

1 answer

3


You don’t need to work with transactions in data read-only cases, so your functions would look like this:

public List<BeanSubtiposOcorrencias> buscaSubTipos(){
    EntityManager em = getEntityManager();
    Query query = em.createNamedQuery("BeanSubtiposOcorrencias.findAllComTipo");
    return query.getResultList();
}

On the slowness, make sure it can’t be DNS problem by pointing directly to the server IP, and also make sure that the version of driver Mysql connector used in your jar is the same as used by Netbeans.

Ah, about your Factory, use Static:

private static EntityManagerFactory factory;
private static EntityManager entityManager;

private static EntityManager getEntityManager() {
    if (entityManager == null) {
        factory = Persistence.createEntityManagerFactory("AtualizacaoCadastralPU");
        entityManager = factory.createEntityManager();
    }
    return entityManager;
}

This way, you create the connection only once, and not all the time, just make sure to close them at the close of the application.

Browser other questions tagged

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