Hibernate does not close connections/sessions in BD

Asked

Viewed 537 times

0

I’m in big trouble using hibernate. Each time I call a method that performs a query or update in the database, Hibernate opens a session in the database and then does not close. I’ve tried EVERYTHING, but really every time it’s called on.getTransaction.Begin(); it creates the session in bd Oracle and doesn’t close anymore.

Below follows details of my code (persistence.xml, Genericdao etc.)

persistence:

<persistence version="2.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_2_0.xsd">


    <persistence-unit name="FilaTrello" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>integration.wcc.model.Chamado</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@//10.ip.aqui/xxxint.xxx.com.br"/>
            <property name="javax.persistence.jdbc.user" value="x_SUPORTE"/>
            <property name="javax.persistence.jdbc.password" value="x_SUPORTE"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
            <property name="hibernate.connection.shutdown" value="true"/>
            <!-- <property name="hibernate.hbm2ddl.auto" value="none"/> -->
            <property name="hibernate.synonyms" value="true"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="false"/>
          </properties>

    </persistence-unit>
</persistence>

Genericdao:

package integration.wcc.dao;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaQuery;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
abstract class GenericDAO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("FilaTrello");
    private EntityManager em = emf.createEntityManager();;


    private Class<T> entityClass;

    public void beginTransaction() {
            em.clear();
            em.getTransaction().begin();

    }

    public void commit() {
        em.getTransaction().commit();
    }

    public void rollback() {
        em.getTransaction().rollback();
    }

    public void closeTransaction() {
       em.close();
    }

    public void commitAndCloseTransaction() {
        commit();
        closeTransaction();
    }

    public void flush() {
        em.flush();
    }

    public void joinTransaction() {
        em = emf.createEntityManager();
        em.joinTransaction();
    }

    public GenericDAO(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public void save(T entity) {
        em.persist(entity);

    }

    public void delete(Object id, Class<T> classe) {
        T entityToBeRemoved = em.getReference(classe, id);

        em.remove(entityToBeRemoved);
    }

    public T update(T entity) {
        return em.merge(entity);
    }

    public T find(int entityID) {
        return em.find(entityClass, entityID);
    }

    public T findL(Long entityID) {
        return em.find(entityClass, entityID);
    }

    public T findReferenceOnly(int entityID) {
        return em.getReference(entityClass, entityID);
    }

    // Using the unchecked because JPA does not have a
    // em.getCriteriaBuilder().createQuery()<T> method
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List<T> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return em.createQuery(cq).getResultList();
    }

    // Using the unchecked because JPA does not have a
    // query.getSingleResult()<T> method
    @SuppressWarnings("unchecked")
    protected T findOneResult(String namedQuery, Map<String, Object> parameters) {
        T result = null;

        try {
            Query query = em.createNamedQuery(namedQuery);

            // Method that will populate parameters if they are passed not null and empty
            if (parameters != null && !parameters.isEmpty()) {
                populateQueryParameters(query, parameters);
            }

            result = (T) query.getSingleResult();

        } catch (NoResultException e) {
            System.out.println("No result found for named query: " + namedQuery);
        } catch (Exception e) {
            System.out.println("Error while running query: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    protected List<T> findListResult(String namedQuery, Map<String, Object> parameters) {
        List<T> result = null;

        try {
            Query query = em.createNamedQuery(namedQuery);

            // Method that will populate parameters if they are passed not null and empty
            if (parameters != null && !parameters.isEmpty()) {
                populateQueryParameters(query, parameters);
            }

            //result = (T) query.getSingleResult();
            result = (List<T>) query.getResultList();

        } catch (NoResultException e) {
            System.out.println("No result found for named query: " + namedQuery);
        } catch (Exception e) {
            System.out.println("Error while running query: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }


    private void populateQueryParameters(Query query, Map<String, Object> parameters) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
    }
}

DAO De Acesso:

public class FilaChamadoDAO extends GenericDAO<FilaChamado> {

    private static final long serialVersionUID = 1L;

    public FilaChamadoDAO() {
        super(FilaChamado.class);
    }

public List<FilaChamado> findTicketsByFila(int idFila,String listType) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("filaNumber", idFila);
        parameters.put("listParam", listType);

        return super.findListResult(FilaChamado.FIND_TICKETS_FILA_BY_FILA, parameters);
    }

There I have a facade class for each model of mine, which makes the access interface to the DAO:

 public class FilaChamadoFacade {

        private FilaChamadoDAO FilaChamadoDAO = new FilaChamadoDAO();

        public List<FilaChamado> listAll() {
            FilaChamadoDAO.beginTransaction();
            List<FilaChamado> result = FilaChamadoDAO.findAll();
            FilaChamadoDAO.closeTransaction();
            return result;
        }

public List<FilaChamado> findTicketsByFila(int idFila,String listaDestino) {
        List<FilaChamado> result = null;
        try {
            FilaChamadoDAO.beginTransaction();
            result = FilaChamadoDAO.findTicketsByFila(idFila, listaDestino);
            FilaChamadoDAO.flush();
        } catch (HibernateException e) {
            if (FilaChamadoDAO != null) {
                FilaChamadoDAO.rollback();
            }
            e.printStackTrace();
        } finally {
            FilaChamadoDAO.closeTransaction();

        }
        return result;
    }

When I call the Listall() method or some namedQuery, I can in no way close these connections. I call the transactions in my main:

public class TestNamedQuery {
    public static void main(String[] args) {

        TrelloUser trelloU = new TrelloUser();
        TrelloUserFacade facade2 = new TrelloUserFacade();
        List<TrelloUser> trelloUsers1 = new ArrayList<TrelloUser>();
        trelloUsers1 = facade2.listAll();

        for (TrelloUser trelloUser : trelloUsers1) {
            if(trelloUser.getUserToken() != null) {
                System.out.println("Selectss do usuario: "+trelloUser.getUserName());
                ChamadoCard cards = new ChamadoCard();
                cards.InsereChamado(trelloUser);
            }
        }


    }
}

As you can see, in my facade class each time I start a transaction right after I close it, but this does not happen in Oracle, where a session is created for each beginTransaction.

2 answers

0


After many attempts, I found a publication saying that I should "commitr" or "rollback" whenever I call the method beginTransaction () to my Entity Manager. Then I changed my code in the Genericda class.java`:

public void closeTransaction() {
        em.getTransaction().commit();
        em.close();
    }

I’m still trying to understand why Hibernate forces me to COMMIT a transaction, even in a query statement, and I think I can win a great performance without doing it. But that change worked very well for me

0

I’m still trying to understand why Hibernate forces me to commit a transaction

Select is a DML data Manipulation language, using Select Into you can change tables, I don’t know exactly if select into has JPA equivalent but SQL makes sense.

http://royalarun.blogspot.com.br/2014/01/why-select-is-dml-statement-in-sql.html

Using containers like spring-data or EJB(java EE) you can delegate to the framework the control of the transaction, eliminating all this Boilerplate.

https://dzone.com/articles/how-does-spring-transactional

Browser other questions tagged

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