dlcathukorala Posted July 8, 2015 Share Posted July 8, 2015 Hi,I have created a bean data source to connect to my database. My objective of creating this datasource is to switch datasource as per a parameter given. But when I run reports using the bean data source, it does not release the connection back into the pool. My connection pool size is 20, so when I run the 21st report, server hangs printing the following message on the JConsoleName: pool-7-thread-9State: WAITING on org.apache.commons.pool.impl.GenericObjectPool@1f8ed84Total 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) Here what I have done in my bean 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) {}}[/code] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now