Answer the question
In order to leave comments, you need to log in
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.
@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();
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question