Slow JFrame Opening with Hibernate

2

Good morning everyone.

I have a Swing application that has a JFrame using Hibernate 3 for persistence in MySQL BD on the network. When I go through NetBeans, it opens up pretty fast.

But when I run jar built on another computer, this JFrame takes too long to open, reaching almost 1 minute of waiting. I am suspicious of Hibernate, but why does NetBeans open so fast, in 1 or 2 seconds, if it also needs to access the network to get to the DB?

Right now, thanks to everyone for the collaboration.

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 drive

<?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've seen reports of other complaints about the net Hibernate slowness, but my biggest doubt is that running on NetBeans does not slow this down.

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

    
asked by anonymous 26.07.2016 / 14:33

1 answer

3

You do not need to deal with transactions in 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, check that it can not be DNS problem pointing directly to the IP of the server, and also make sure that the version of the driver MySQL connector used in your jar is the same used by NetBeans.

Oh, 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;
}

So you create the connection once, not every time, just be sure to close them at the end of the application.

    
27.07.2016 / 18:46