Good, I have an application made in java and mysql, after hours the connection is closed, I understand that by the global variable of the server mysql wait-timeout.
I have a class that creates the connection to the BBDD and then a class "manager" where I do all the queries and updates. I think the problem is that I do not close the connection and when I stay open, once the mysql timeout is fulfilled, it is cut ...
The thing is that I tried to create a connection in each method and close it but closing it and making a connection again gives me the error of no operations allowed after connection closed.
My class to create the connection is
public class SGBD {
private static Connection conn=null;
public static SGBD instance;
private String servidor;
private String IP;
private String user;
private String password;
private SGBD(String servidor, String IP,String user, String password){
this.setServidor(servidor);
this.setIP(IP);
this.setUser(user);
this.setPassword(password);
try{
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver());
}catch(Exception e){
System.out.println("Driver no encontrado");
System.exit(0);
}
conn=DriverManager.getConnection("jdbc:"+this.getServidor()+"://"+this.getIP(),this.getUser(),this.getPassword());
System.out.println("Conexion establecida con exito.");
}catch(Exception e){
System.out.println("Error en la conexion, el mensaje es: "+e.getMessage());
System.exit(2);
}
}
public static boolean isConnected(){
return !(conn==null);
}
public Connection getConn() {
return conn;
}
public static SGBD getInstance(String servidor, String IP,String user, String password){
if(isConnected()){
return instance;
}else{
instance=new SGBD(servidor,IP,user,password);
return instance;
}
}
And the class that generates queries is similar to this:
public class GestorBD {
Connection conn;
/**
* Constructor de la clase
* @param servidor
* @param ip
* @param username
* @param password
*/
public GestorBD(String servidor, String ip, String username, String password){
this.setConn(SGBD.getInstance(servidor, ip, username, password).getConn());
}
public GestorBD(){
//Base de datos servidor remoto
this.setConn(SGBD.getInstance("","","","").getConn());
//Base de datos servidor local
//this.setConn(SGBD.getInstance("","","","").getConn());
}
public Connection getConn(){
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* Obtener la lista de las inmobiliarias registradas en la BBDD
* @return
* @throws SQLException
* @throws EmailIncorrectoException
* @throws LongitudCadenaException
*/
public List <Inmobiliaria> getInmobiliarias() throws SQLException, LongitudCadenaException, EmailIncorrectoException{
List <Inmobiliaria> lista = new ArrayList<Inmobiliaria>();
this.getConn().createStatement().execute("USE 'mhs_simple_form'");
Statement stm = this.getConn().createStatement();
ResultSet rs = stm.executeQuery("select * from tbl_inmobiliaria ORDER BY nombre_inmobiliaria ASC");
while(rs.next()){
Inmobiliaria inmo = new Inmobiliaria(
rs.getInt("id_inmobiliaria"),
rs.getString("nombre_inmobiliaria"),
rs.getString("email_inmobiliaria"),
rs.getString("pass_inmobiliaria"),
rs.getString("telefono_inmobiliaria"),
rs.getInt("estado_inmobiliaria"));
lista.add(inmo);
}
stm.close();
rs.close();
return lista;
}
I do not know where to close the connection since it gives me an error. Thank you very much for the help