Bean Datasource的连接池问题 - Jasperserver

时间:2015-07-09 13:05:44

标签: mysql javabeans connection-pooling jasperserver jboss-eap-6

我创建了一个bean数据源来连接我的数据库。我创建此数据源的目的是根据给定的参数切换数据源。但是当我使用bean数据源运行报告时,它不会将连接释放回池中。我的连接池大小为20,因此当我运行第21个报告时,服务器挂起在JConsole上打印以下消息

Name: pool-7-thread-9
State: WAITING on org.apache.commons.pool.impl.GenericObjectPool@1f8ed84
Total blocked: 0  Total waited: 4

Stack trace: 
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:485)
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:748)
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
com.loits.jasper.ds.SwitchDS.setReportParameterValues(SwitchDS.java:75)
com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl.fillReport(EngineServiceImpl.java:1725)
com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl$ReportFill.runWithDataSource(EngineServiceImpl.java:1086)
com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl$ReportFill.runReport(EngineServiceImpl.java:1015)
com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl$ReportRunnable.run(EngineServiceImpl.java:908)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

这是我在我的豆子里做的事情

package com.loits.jasper.ds;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.sql.SQLException;

import net.sf.jasperreports.engine.JRParameter;

import org.springframework.security.context.SecurityContextHolder;

import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;
import com.jaspersoft.jasperserver.api.metadata.user.domain.Role;
import com.jaspersoft.jasperserver.api.metadata.user.domain.client.ProfileAttributeImpl;
import com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails;
import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
import com.jaspersoft.jasperserver.api.common.service.BeanForInterfaceImplementationFactory;
import com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JdbcDataSourceService;
import com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JdbcReportDataSourceServiceFactory;
import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;

public class SwitchDS implements ReportDataSourceService {

    private JdbcDataSourceService connection;
    private RepositoryService repositoryService;
    private BeanForInterfaceImplementationFactory dataSourceServiceFactory;

    // private DataSourceServiceFactory dataSourceServiceFactory;

    public SwitchDS(RepositoryService repositoryService,
            BeanForInterfaceImplementationFactory dsServiceFactory) {
        super();
        this.repositoryService = repositoryService;
        this.dataSourceServiceFactory = dsServiceFactory;

        System.out.println("repositoryService " + repositoryService);
        System.out.println("dataSourceServiceFactory "
                + dataSourceServiceFactory);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void setReportParameterValues(Map parametersValue) {

        String ds_name = (String) parametersValue.get("P_SCM");

        String ds_uri = "";

        if(ds_name.equalsIgnoreCase("D_001_S")){
            ds_uri = "/datasources/lolc_fusion";
        }
        else if(ds_name.equalsIgnoreCase("D_005_S")){
            ds_uri = "/datasources/lofc_fusion";
        }
        else if(ds_name.equalsIgnoreCase("D_009_S")){
            ds_uri = "/datasources/lomc_fusion";            
        }
        else if(ds_name.equalsIgnoreCase("D_010_S")){
            ds_uri = "/datasources/clc_fusion";
        }
        else if(ds_name.equalsIgnoreCase("D_025_S")){
            ds_uri = "/datasources/lofac_fusion";
        }


        if(connection == null){
            connection = getRepositoryDatasource(ds_uri);
        }


        try {
            parametersValue.put(JRParameter.REPORT_CONNECTION, connection
                    .getDataSource().getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public JdbcDataSourceService getRepositoryDatasource(String repositoryURI) {

        try {
            ExecutionContext context = JasperServerUtil.getExecutionContext();
            ReportDataSource datasource = (ReportDataSource) repositoryService
                    .getResource(context, repositoryURI);

            System.out.println("datasource " + datasource.getName());
            System.out.println("datasource class " + datasource.getClass());

            JdbcReportDataSourceServiceFactory factory = (JdbcReportDataSourceServiceFactory) dataSourceServiceFactory
                    .getBean(datasource.getClass());
            JdbcDataSourceService DSservice = (JdbcDataSourceService) factory
                    .createService(datasource);


            return DSservice;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }
    }

    @Override
    public void closeConnection() {
        if (connection != null) {
            connection.closeConnection();
            System.out.println("Connection closed .. ");
        }
    }

    public static void main(String[] args) {

    }
}

1 个答案:

答案 0 :(得分:0)

对我而言,就像你正在关闭连接而不是将其返回到池中一样。

西蒙