Currently i am using Hibernate, Spring and postgres as database in my application. Unfortunately the connection to the database is keep on increasing.
Every method in the DAO Implementation class is increasing the connection count. Below is the piece of code in DAO Impl.
@Autowired
SessionFactory sessionFactory;
/**
* Used to save or update a SourceConfig.
*
* @throws Exception
*/
@Override
@Transactional
public Long saveOrUpdateSourceConfig(SourceConfig sourceconfig) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
HttpSession httpsession = request.getSession();
try {
session.saveOrUpdate(sourceconfig);
session.flush();
tx.commit();
} catch (JDBCException e) {
tx.rollback();
String nextException = e.getSQLException().getNextException().getLocalizedMessage();
e.printStackTrace();
System.out.println("next version" + nextException);
}
Serializable id = session.getIdentifier(sourceconfig);
return (Long) id;
}
I am unable to use session.getcurrentsession() and always this returns as null and i am not closing the session anywhere in the DAOImpl.
Please your response will be highly appreciated.