D
D
Denis Kuznetsov2019-07-05 10:10:30
Java
Denis Kuznetsov, 2019-07-05 10:10:30

How to release Connection in Hibernate?

Hello, after several requests in the application, I came across such an exception

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30002ms.

I didn’t set up a connection pool, my task was to use the usual Hibernate in the Spring application, for this I took out the Session from the EntityManager every time and worked with it, as is done in the usual hibernate. As far as I understand, the dataSource is responsible for the connection pool, and this thing is already in the JDBC part. How can I not overflow the connection pool in my application or release it using Hibernate tools?
My Dao implementations look something like this:
@Component
public class GrowBoxDaoImpl implements GrowBoxDao {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Override
    public List<GrowBox> findByUser(Long userId) {

        Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
        String hqlQuery = "from GrowBox gb where gb.responsibleUser.id =: userId";
        Query query = session.createQuery(hqlQuery);
        query.setParameter("userId", userId);
        List growBoxes = query.getResultList();
        session.close();
        return growBoxes;
    }

    @Override
    public GrowBox findById(Long id) {

        Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
        GrowBox growBox = session.get(GrowBox.class, id);
        session.close();

        return growBox;
    }

    @Override
    public GrowBox saveBox(GrowBox box) {
        Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
        session.getTransaction().begin();
        session.saveOrUpdate(box);
        session.flush();
        session.getTransaction().commit(); // call flush too
        session.close();
        return box;
    }

    @Override
    public void deleteBox(Long id) {

        String hqlQuery = "delete GrowBox where id =: id";
        Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
        session.getTransaction().begin();

        session.createQuery(hqlQuery).setParameter("id", id).executeUpdate();
        session.flush();
        session.getTransaction().commit();
        session.close();
    }
}

I have several entities, each has its own Dao and its own Service, several controllers in which these Services are used
in my Dao I tried to correctly execute transactions and always closed sessions
here is the full project repository if it helps https://github.com/DennisKingsman/ HibernateWithSpr...
PS disconnect() method; doesn't help either

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lexas, 2019-07-15
@lexas

I will assume that the problem is not in the hibernate, but in the transaction manager. transactions are opened but not closed. session from hibernate has nothing to do with it.
I would advise:
a) to define explicit boundaries of transactions
b) to debug who opens and why does not pop up in the closing (commit or rollback) of the transaction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question