Io ho un'applicazione che contiene più classi. Una di queste si collega ad un database attraverso un pool di connessioni (il pool di connessioni di apache). La classe che si collega al database definisce un metodo statico shutDownDriver per chiudere il pool di connessioni. Quando io chiamo questo metodo da un'altra classe il compilatore lancia un warning :"The static method shutDownDriver() from the type Db should be accessed in a static way".
Per completezza inserisco il codice:
classe db
Codice PHP:
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.impl.GenericObjectPool;
public class Db {
Db() throws Exception{
Class.forName("org.gjt.mm.mysql.Driver");
setupDriver("jdbc:mysql://localhost:3306/db");
}
public Esame getEsame(String idEsame) throws SQLException{
return e;
}
public ResultSet getRsForXml(Esame e, Studente s) {
return null;
}
public static void setupDriver(String connectionURI) throws Exception{
GenericObjectPool connectionPool = new GenericObjectPool(null);
connectionPool.setMaxActive(100);
connectionPool.setMaxIdle(50);
connectionPool.setMinIdle(5);
ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(connectionURI,"root","");
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver =
(PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.registerPool("cp", connectionPool);
}
public static void shutDownDriver() throws Exception{
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool("cp");
}
}
classe chiamante:
Codice PHP:
package model;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;
public class Converter {
String error;
boolean e = false;
Db db;
Converter() throws Exception{
super();
db = new Db();
}
public void error(String error){
this.error = error;
this.e = true;
}
public String generateXml(String idesame, int mod){
Esame e;
try {
e = db.getEsame(idesame);
} catch (SQLException e2) {
try {
db.shutDownDriver();
} catch (Exception e1) {
error(e1.toString());
return error;
}
error(e2.toString());
return error;
}
.....................
......................
Il metodo shutDownDriver l'ho definito statico perché così è consigliato dagli implementatori che hanno rilasciato questo esempio
Ora le domande sono due: come posso risolvere questo warning? Lasciando la situazione com'è quali implicazioni si avranno durante l'esecuzione?
Grazie per l'attenzione.