Possible problems of an infinite loop in the connections with Database

0

I'm doing an infinite loop in which I call a method that is going to do a programmed task of modifying data in my BD. the loop is executed when I start my program.

My question is whether this infinite loop would not bring me problems or deficiencies in the connections with my BD.

  import java.sql.*;
  import java.text.SimpleDateFormat;
  import java.util.Date;

/**
  *
  * @author bitPanG98
  */
public class TareasProgramadas extends Thread {

public void run() {

    while (true) {

        try {

            Thread.sleep(5000);
            modificarEstadoReservacionPorExceso();

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

    }

}

private void modificarEstadoReservacionPorExceso() {

    try {
        /*Ponemos el formato que va a tener la fecha*/
        SimpleDateFormat formatoFecha = new SimpleDateFormat("yyyy-MM-dd");
        /*Obtenemos la fecha actual y la almacenamos en la varibale de tipo Date 'dateActual' */
        Date dateActual = FechaHora.obtenerFechaActual();
        /*Declaramos una variable de  tipo String el cual Obtenemos la fecha actual pasandole el formato de fecha */
        String fechaActualFormateada = formatoFecha.format(dateActual);

        String consultaCargarDatos = " SELECT r.codigo_reservacion , m.numero_mesa "
                + " FROM reservacion r , mesas m , detalle_reservacion dr "
                + " WHERE dr.codigo_reservacion=r.codigo_reservacion AND dr.numero_mesa = m.numero_mesa AND r.estado='Activa' AND r.fecha_reservacion = CURDATE() AND r.hora_maximaespera <= curTime() ";

        Connection con = new ConexionBD().conectarBD();
        Statement sta = con.createStatement();
        ResultSet rs = sta.executeQuery(consultaCargarDatos);

        while (rs.next()) {

            String codigoReservacion = rs.getString("codigo_reservacion");
            String numeroMesa = rs.getString("numero_mesa");

            System.out.println("resultados de las reservaciones   codigoR :  " + codigoReservacion + " .... numero mesa : " + numeroMesa);
            String consultaModificarReservacion = " UPDATE reservacion r, mesas m SET r.estado ='Inabilitada' , m.estado ='Libre' WHERE   r.codigo_reservacion= '" + codigoReservacion + "' AND m.numero_mesa='" + numeroMesa + "' ";

            Statement sentencia = con.createStatement();
            int respuesta = sentencia.executeUpdate(consultaModificarReservacion);
            if (respuesta > 0) {
                System.out.println("se ejecuto la consulta de modificacion");
            } else {
                System.out.println("no se ejeuto la consulta");
            }

        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    }

}

  }
    
asked by bitPanG98 06.12.2017 в 19:35
source

1 answer

0

To avoid saturation of your memory space and the connection to the BD, I recommend you close your object con and object sta

con.close();
sta.close();

I would recommend that you close them with the try-catch-finally statement in the following way.

try{ ... }catch(SQLException ex){ ... }finally{ con.close(); sta.close(); }

If you like some more information, I invite you to see the official documentation here or that you visit this post here .

    
answered by 07.12.2017 в 16:38