I created javafx apllication that connects to database and creates LineChart from data and i works fine except that when getting data im connecting to database in loop and that makes application slow when using h2 embedded database. I am using spring jdbcDaoSupport to make queries.
Making connection in loop is bad design, but i don't know how i would make it work differently. How should i manage connections like that?
I tried searching for answer but i didn't found anything relevant to my problem.
Here is relevant part of code:
public void cijenaVodePoGradovimaGraf(){
ObservableList<Grad> gradList;
ObservableList<String> gradNaziv;
gradNaziv=FXCollections.observableArrayList();
gradList=databaseService.getVodoopskrbaGradList();
for (Grad grad : gradList) {
gradNaziv.add(grad.getNaziv());
}
CategoryAxis xOs=new CategoryAxis(gradNaziv);
xOs.setTickLabelRotation(270);
xOs.setTickLabelFont(Font.font(14));
NumberAxis yOs=new NumberAxis();
yOs.setLabel("Cijena [kn/kubik]");
yOs.setTickLabelFont(Font.font(14));
XYChart.Series<String, Number> series=new XYChart.Series<>();
series.setName("Varijabilni dio");
XYChart.Series<String, Number> series2=new XYChart.Series<>();
series2.setName("Fiksni dio");
//this is where problem is
for(int i=0;i<gradNaziv.size();i++){
ObservableList<Vodoopskrba> v=databaseService.getVodoopskrbaList(gradNaziv.get(i));
series.getData().add(new XYChart.Data<>(gradNaziv.get(i),v.get(0).getVodoOdvVar()));
series2.getData().add(new XYChart.Data<>(gradNaziv.get(i),v.get(0).getVodoOdvFix()));
}
ObservableList<XYChart.Series<String, Number>> data=FXCollections.observableArrayList();
data.add(series);
data.add(series2);
lcGraf=new LineChart<>(xOs,yOs,data);
lcGraf.setTitle("Cijena vode");
for(XYChart.Series<String, Number> s : lcGraf.getData()) {
for (XYChart.Data<String, Number> d : s.getData()) {
Tooltip tt=new Tooltip(d.getYValue().toString());
Tooltip.install(d.getNode(), tt);
d.getNode().setOnMouseEntered((event)->{
d.getNode().getStyleClass().add("onHover");
});
d.getNode().setOnMouseExited((event)->{
d.getNode().getStyleClass().remove("onHover");
});
}
}
AnchorPane root=((AnchorPane)this.getStage().getScene().getRoot());
((GridPane)root.getChildren().get(0)).add(lcGraf, 0, 1, 2, 1);
}
I marked part of code where i am looping through list of cities and then i am making database connection to get water suply company from that town and then i add data about price of water in that city to chart.
@Kayaman suggested that i should use connection pool and after some research on internet i found how to set it and it solved speed problem, now stage loads immediately. I am posting solution here so if somebody else encounters this problem too.
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="2" />
</bean>
its only thing that needs change.
<!-- tomcat jdbc dependency -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.35</version>
</dependency>
EDIT:
I found that its possible to use spring SingleConnectionDataSource to make JdbcDaoSupport to reuse same connection and thats exactly what i need.
Here is code:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
<property name="url" value="${jdbc.url}" />
<property name="suppressClose" value="true" />
</bean>
Because suppressClose=true connection will not be closed when code call close on datasource so we need to close it manually when we dont need it anymore. I did it the next way:
@Override
public void stop() {
SingleConnectionDataSource ds;
ds=((SingleConnectionDataSource)AppUtil.getContext().getBean("dataSource"));
ds.destroy();
AppUtil.closeContext();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.