几次调用后,hibernate无法打开JDBC Connection

时间:2012-12-28 05:05:42

标签: spring hibernate jdbc jdbctemplate hibernate-criteria

我是hibernate的新手,并尝试使用它,所以出于学习目的,我使用的是Criteria API和HQL中的一些。

我的应用程序运行正常,但问题是执行5-6次后,不再打开JDBC连接并且应用程序挂起。

在日志中它说...打开JDBC连接......就是这样。

我读了几篇文档,并说这是因为一旦打开会话就不会发布。

这是我正在运行的功能。

    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

有人可以指导我出错的地方。

1 个答案:

答案 0 :(得分:1)

您正尝试使用HibernateDaoSupport.getSession()方法获取会话。

API说,

  

请注意,这并不是要从HibernateTemplate代码调用的   而只是在简单的Hibernate代码中。要么依赖线程绑定   会话或组合使用它   withreleaseSession(构造org.hibernate.Session)。

因此,请使用下面的HibernateDaoSupport.releaseSession(org.hibernate.Session)方法关闭开放会话,而不是session.close()

releaseSession(session);