org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法获取连接,池错误超时等待空闲对象?

时间:2013-02-20 09:42:55

标签: html

我正在使用liferay 6.1并创建了我的自定义portlet,并且我正在使用自定义查询,它使用以下内容直接从数据库中获取记录....

public List<DashBoardBean> GetPieChartDataForCampaignbyOrganization(
            ThemeDisplay pThemeDisplay) {

        log.info("In GetPieChartDataForCampaignbyOrganization ");
        Context CtxObj;
        long pParentOrgId;
        ResultSet advResultSet = null;
        List<DashBoardBean> dashboardbeanObjList = new ArrayList<DashBoardBean>();
        try {

            CtxObj = new InitialContext();
            DataSource DsObj = (DataSource) CtxObj
                    .lookup("java:comp/env/jdbc/Liferay");
            Connection ConObj = DsObj.getConnection();
            Statement sStmtObj = ConObj.createStatement();

            advResultSet = sStmtObj
                    .executeQuery("MY CUSTOM QUERY WILL BE HERE");
            while (advResultSet.next()) {
                DashBoardBean dashboardbeanObj = new DashBoardBean();
                dashboardbeanObj
                        .setsOrganizationName(advResultSet.getString(1));
                dashboardbeanObj.setlOrganizationCount(advResultSet.getLong(2));
                dashboardbeanObjList.add(dashboardbeanObj);
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            dashboardbeanObjList = null;
            e.printStackTrace();
        } catch (SQLException e) {
            dashboardbeanObjList = null;
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (PortalException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        log.info("Leave GetPieChartDataForCampaignbyOrganization ");
        return dashboardbeanObjList;
    }

上面是我的一个函数的代码片段...我有超过5个具有相同结构的函数...只是SQL查询功能不同...

现在问题是我必须在页面加载中调用超过5个这样的函数...所以当我用来经常重新加载页面时...它给我的错误如下......

   org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
        at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:114)
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
        at emenu.advertise.appbl.DashBoardCustomQuery.GetPieChartDataForCampaignStatus(DashBoardCustomQuery.java:112)
        at emenu.advertise.appbl.DashBordBL.GetPieChartDataForCampaignStatus(DashBordBL.java:292)
        at emenu.advertise.portlet.RestaurantPortlet.serveResource(RestaurantPortlet.java:402)
        at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:118)
        at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:71)
        at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:111)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
        at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
        at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:531)
        at com.liferay.portlet.InvokerPortletImpl.invokeResource(InvokerPortletImpl.java:626)
        at com.liferay.portlet.InvokerPortletImpl.serveResource(InvokerPortletImpl.java:436)
        at com.liferay.portal.action.LayoutAction.processPortletRequest(LayoutAction.java:1075)
        at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:719)
        at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:249)
        at ............................
.......java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
            at java.lang.Thread.run(Thread.java:619)
        Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
            at org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1171)
            at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
            ... 131 more
        09:24:11,163 INFO  [http-bio-8080-exec-5][DashBoardCustomQuery:665] Leave GetHighestClickRestaurantName 
        09:24:11,163 INFO  [http-bio-8080-exec-5][DashBoardCustomQuery:304] Leave GetLineChartDataForHighestClickedByRestaurant 
        09:24:11,164 INFO  [http-bio-8080-exec-5][DashBordBL:166] Leave GetLineChartDataByRestaurant From DashBordBl

我知道我做错了...但我不知道在哪里......也许是因为它的SQL连接刚从服务器丢失///是因为我没有关闭连接...请帮助我......纠正我错在哪里......我刚刚给出了一个我的函数示例,就像我从其他函数中获取数据一样......

1 个答案:

答案 0 :(得分:-1)

我刚刚找到了解决方案,因为我在执行query.i之后从未关闭过连接对象。如下所示在try catch块之后对我有用...

finally {
             try {
                 if(CtxObj!=null)   {
                     CtxObj.close();
                    }
                    if(sStmtObj!=null){
                        sStmtObj.close();
                    }
                    if(ConObj!=null){
                        ConObj.close();
                    }
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                log.info(e);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                log.info(e);
            }
        }