Jump to content
Changes to the Jaspersoft community edition download ×

Using a complex Spring bean (Datasource?) in JI


Grazy Mos

Recommended Posts

At the start of executing a report in jasperintelligence 1.1.0 I want to send a static

 

query to the database. The connection that sends this query should also be used to execute

 

the report!

 

My idea was to put the query in a bean(code) that links to the datasource:

 

public class ConSourceImp implements ConSource{
Connection c;
public DataSource datSourc;

public void setDataSource(DataSource datSourc)
{
this.datSourc = datSourc;
// use();
}

public void use(){
try{
c = datSourc.getConnection();
//do something with the connection
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

 

applicationContext.xml:

 

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@serverdb:1521:devdb</value>
</property>
<property name="username">
<value>username</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>

<bean id="conSource" class="consource.ConSourceImp">
<property name="dataSource">
<ref bean="dataSource2"/>
</property>
</bean>

 

I am not sure if the bean conSource can work as a datasource and this idea is possible to astablish with jasper intelligence without adjusting the source code. When i try executing the bean in jasperIntelligence it gives no method exceptions.

 

How can i get this working?

 

 

Any help would be greatly appriciated

 

thanks in advance,

 

Niels

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

The BeanDataSource is set up to do this.

 

The bean can be a class that implements:

 

Code:

public interface ReportDataSourceService {

void setReportParameterValues(Map parameterValues);

void closeConnection();

}

 

Create the bean definition in a file named something like applicationContext-myDataSource.xml and put it in /WEB-INF. Add the JAR with your class(es) into /WEB-INF/lib

 

You can create a Bean data source in the repository with the name of your bean, and select this Bean data source as the data source for a report unit.

 

 

 

Sherman

JasperSoft

Link to comment
Share on other sites

Thanks, im still confused how to call the bean from jasper intelligence. Should I call the DAO bean or create a transactionmanager bean and call that?

 

And how is the bean method used? Do i have to create a bean method and use it? or can I leave it blank.

 

Thanks in advance,

 

Niels

Link to comment
Share on other sites

You can refer to the bean directly. There is no need to wrap it in a transaction manager.

 

If your bean implements ReportDataSourceService, you can leave the method name blank. If you have a bean that has methods that return ReportDataSourceServices, you can add the method name. The idea here was that you could have some common connection/data source related code in the bean and different result sets reflected in different methods.

 

 

Sherman

JasperSoft

Link to comment
Share on other sites

Then why it gives me the following error when i try to run a report in jasper intelligence:

 

org.springframework.webflow.ActionExecutionException: Exception thrown executing [AnnotatedAction@1262f7c targetAction = com.jaspersoft.jasperserver.war.action.ViewReportAction@1f784d7, attributes = map[[empty]]] in state 'verifyData' of flow 'viewReportFlow'; nested exception is com.jaspersoft.jasperserver.api.JSException: Bean with name: conSource does not have method:
org.springframework.webflow.ActionExecutionException: Exception thrown executing [AnnotatedAction@1262f7c targetAction = com.jaspersoft.jasperserver.war.action.ViewReportAction@1f784d7, attributes = map[[empty]]] in state 'verifyData' of flow 'viewReportFlow'; nested exception is com.jaspersoft.jasperserver.api.JSException: Bean with name: conSource does not have method:
com.jaspersoft.jasperserver.api.JSException: Bean with name: conSource does not have method:
etc..

 

applicationContext-myDatasource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="conSource" class="consource.ConSourceImp">
<property name="dataSource">
<ref bean="dataSource2"/>
</property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" singleton="false">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:devdb</value>
</property>
<property name="username">
<value>user</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
</beans>

consourceImp:

import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRParameter;

public class ConSourceImp implements ReportDataSourceService{
Connection c;
public DataSource datSourc;


public void setDataSource(DataSource datSourc)
{
this.datSourc = datSourc;
}

public void closeConnection(){
try{
c.close();
}
catch(Exception e){
e.printStackTrace();
}

}


public void use(){
try{
c = datSourc.getConnection();
//do something with the connection

}
catch(Exception e)
{
e.printStackTrace();
}

}

public void setReportParameterValues(Map parameterValues) {
use();
try {
c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
} catch (SQLException e) {
e.printStackTrace();
throw new JSExceptionWrapper(e);
}
parameterValues.put(JRParameter.REPORT_CONNECTION, c);
}
}

 

ReportDataSourceService interface:

import java.util.Map;

public interface ReportDataSourceService {

void setReportParameterValues(Map parameterValues);

void closeConnection();
}

 

How could i fix this?

 

thanks in advance,

niels

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...