我是hibernate的新手并尝试使用它,所以为了学习目的,有些地方我使用Criteria API,有些地方使用HQL。
我的应用程序运行良好,但问题是在执行 5-6 次后,不再打开 JDBC 连接并且应用程序挂在那里。
在日志中它说......打开 JDBC 连接......就是这样。
我读了一些文档,它说这是因为一旦打开 Session 就没有释放它。
这是我正在运行的功能。
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;
}
日志
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
有人可以指导我哪里出错了。