Using a complex Spring bean (Datasource?) in JI

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
Grazy Mos's picture
456
Joined: Sep 12 2006 - 11:13pm
Last seen: 17 years 3 weeks ago

5 Answers:

The BeanDataSource is set up to do this.

The bean can be a class that implements:

Code:
<br />
public interface ReportDataSourceService {<br />
	<br />
	void setReportParameterValues(Map parameterValues);<br />
	<br />
	void closeConnection();<br />
<br />
}</td></tr></tbody></table><br />
<br />
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<br />
<br />
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.<br />
<br />
<br />
<br />
Sherman<br />
JasperSoft
swood's picture
24557
Joined: Jun 21 2006 - 12:48pm
Last seen: 10 years 11 months ago
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
Grazy Mos's picture
456
Joined: Sep 12 2006 - 11:13pm
Last seen: 17 years 3 weeks ago
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
swood's picture
24557
Joined: Jun 21 2006 - 12:48pm
Last seen: 10 years 11 months ago
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
Grazy Mos's picture
456
Joined: Sep 12 2006 - 11:13pm
Last seen: 17 years 3 weeks ago
The web GUI saves the bean method name as an empty string, and the data source service checks the method name against null and tries to find a method named "". This is now fixed on SVN.

The workaround is to always use a bean factory method name.

Regards,
Lucian
lucianc's picture
87341
Joined: Jul 17 2006 - 1:10am
Last seen: 6 hours 58 min ago
Feedback
randomness