Reputation: 6111
I am new to hibernate and trying to use it, so for learning purpose some where I am using Criteria API and some where HQL.
My application is running fine but problem is after executing 5-6 times, no more JDBC connection get opened and application hangs there.
in logs it says...Opening JDBC Connection......thats it.
I read few docs and it says this is because of not releasing Session once it is opened.
This is the functions I am running.
public List<Place> getAllPlaces() {
Session session = getSession();
Criteria cr = session.createCriteria(Place.class);
List ls= cr.list();
session.close();
return ls;
}
public List<User> getAllUser() {
Session session = getSession();
Criteria cr = session.createCriteria(User.class);
List ls= cr.list();
session.close();
return ls;
}
public List<Carpooler> getAllCarpooler() {
Session session = getSession();
Criteria cr = session.createCriteria(Carpooler.class);
List ls= cr.list();
session.close();
return ls;
}
public List<SourceToDestinationDetails> getAllSourceToDestinationDetailsbyCarpooler(
Carpooler carpooler) {
Session session = getSession();
Criteria cr = session.createCriteria(SourceToDestinationDetails.class);
cr.add(Expression.eq("listOfSourceToDestinationDetails",carpooler));
List ls= cr.list();
session.close();
return ls;
}
public List<Carpooler> getExactMatchCarpooler(String from, String to) {
log.debug("Request received for fetching Exact match Carpooler.");
List<Carpooler> listOfFinalCarpooler = new ArrayList<Carpooler>();
List list = null;
try{
list = getHibernateTemplate().findByNamedQueryAndNamedParam("findExactMatchingCarpooler", new String[]{"source","destination"}, new String[]{from,to});
if(list!=null){
log.debug("Fetched Exact match carpooler list is :"+list.toString());
for (int j = 0; j < list.size(); j++) {
Object[] l = (Object[])list.get(j);
Carpooler c = (Carpooler)l[0];
SourceToDestinationDetails std = (SourceToDestinationDetails)l[1];
Carpooler c1 = new Carpooler();
c1.setCarpoolerCreationDate(c.getCarpoolerCreationDate());
c1.setCarpoolerId(c.getCarpoolerId());
c1.setDrivingLicenceNumber(c.getDrivingLicenceNumber());
c1.setUser(c.getUser());
c1.setListOfVehicleDetails(c.getListOfVehicleDetails());
c1.setUserType(c.getUserType());
List<SourceToDestinationDetails> listOfSourceToDestinationDetails =new ArrayList<SourceToDestinationDetails>();
listOfSourceToDestinationDetails.add(std);
c1.setListOfSourceToDestinationDetails(listOfSourceToDestinationDetails);
listOfFinalCarpooler.add(c1);
// log.debug("Carpooler is :"+c.getCarpoolerId());
// log.debug("SourceToDestinationDetails is :"+std.getSourceToDestinationId());
}
}else{
log.debug("List is null");
}
log.debug("Returning back from fetching Exact match Carpooler");
return listOfFinalCarpooler;
}catch(Exception e){
log.error("Exception Occurred while fetching Exact Match Result :"+e.getMessage());
}
return null;
}
Logs
2012-12-28 10:32:33,529 DEBUG http-8080-4 [DashboardController.getLoginPage1] - Fetching list of all user, to display count on home page.
2012-12-28 10:32:33,529 DEBUG http-8080-4 [SessionFactoryUtils.doGetSession] - Opening Hibernate Session
2012-12-28 10:32:33,530 DEBUG http-8080-4 [SessionImpl.<init>] - opened session at timestamp: 13566709535
2012-12-28 10:32:33,531 DEBUG http-8080-4 [AbstractBatcher.logOpenPreparedStatement] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2012-12-28 10:32:33,531 DEBUG http-8080-4 [ConnectionManager.openConnection] - opening JDBC connection
Can someone please guide me where I am going wrong.
Upvotes: 2
Views: 1341
Reputation: 12924
You are trying to get the session using HibernateDaoSupport.getSession() method.
API Says,
Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session or use it in combination withreleaseSession(org.hibernate.Session).
So use HibernateDaoSupport.releaseSession(org.hibernate.Session) method as below to close the open sessions instead of session.close()
.
releaseSession(session);
Upvotes: 1