Multiple Sessions in Java Web?

0

Good morning, have all of you. My question is this, I have a coupon printing system for raffles, made in NetBeans (Servlets), this system has the option to log in and enter, until there everything works fine. The problem begins when registering the coupons per customer for example I register 5000 credits and print 5 coupons, but when another user logs in and registers the credits of another customer, I believe that it enters into internal conflict and throws a null value, I saw on the internet that the Apache Tomcat server internally manages the users connected to the system, I also saw that for that type of multi-user system you have to work with Threads, so my doubt would be that. What do you recommend me to do?

This is the error that prints me.

This is my code to log in.

public int idUsuario(entusuario enti) {
    int id=0;
    try {

        ps=xcone.prepareStatement("select idPersona from operador where nombreusuario=(?) and claveusuario=(?)");
        //ps=cone.xconMysql().prepareStatement("select idPersona from operador where nombreusuario=(?) and claveusuario=(?)");
        ps.setString(1, enti.getNombreusuario());
        ps.setString(2, enti.getClaveusuario());
        rs=ps.executeQuery();
        while(rs.next())
        {
            id=rs.getInt(1);
        }
        //System.err.println("el id del cliente es "+id);
    }catch(SQLException e){
        System.err.println("Error al otbener el id del cliente "+e.getMessage());
    } 

    return id;
}

public ArrayList<entusuario> getPersona(int id){
    ArrayList<entusuario> datos=new ArrayList<>();

    try {
            ps=xcone.prepareStatement("select per.idpersona,\n" +
            //ps=cone.xconMysql().prepareStatement("select per.idpersona,\n" +
        "per.nombres,\n" +
            "per.apellidos,\n" +
            "op.idoperador,\n" +
            "op.nombreusuario,\n" +
            "soc.idSocio,\n" +
            "soc.nombreSocio \n" +
            "from persona per\n" +
            "inner join operador op on per.idPersona=op.idpersona \n" +
            "inner join socio soc on soc.idSocio=op.idSocio \n"+
            "where per.idPersona=(?)");
        ps.setInt(1, id);
        rs=ps.executeQuery();
        while(rs.next())
        {
            ent=new entusuario();
            ent.setIdpersona(rs.getInt(1));
            ent.setNombres(rs.getString(2));
            ent.setApellidos(rs.getString(3));
            ent.setIdoperador(rs.getInt(4));
            ent.setNombreusuario(rs.getString(5));
            ent.setIdSocio(rs.getInt(6));
            ent.setNombreSocio(rs.getString(7));
            datos.add(ent);
            System.err.println("Usuario "+ent.getNombres()+" logueado");
        }
    } catch (SQLException e) {
        System.err.println("Error al obtener datos del usuario "+e.getMessage());
        return null;
    }
    return datos;
}

What I could understand is that he is connecting many times to the BD.

    
asked by Jcastillo 18.11.2018 в 18:07
source

1 answer

1

I enclose a way to perform the query that should solve the problems:

public ArrayList<entusuario> getPersona(int id){
ArrayList<entusuario> datos=new ArrayList<>();

try {
        Statment stmt = xcone.createStatment();
        ResultSet rs = stmt.executeQuery(/*tu consulta*/);

    while(rs.next())
    {
        ent=new entusuario();
        ent.setIdpersona(rs.getInt(1));
        ent.setNombres(rs.getString(2));
        ent.setApellidos(rs.getString(3));
        ent.setIdoperador(rs.getInt(4));
        ent.setNombreusuario(rs.getString(5));
        ent.setIdSocio(rs.getInt(6));
        ent.setNombreSocio(rs.getString(7));
        datos.add(ent);
        System.out.println("Usuario "+ent.getNombres()+" logueado");
    }
} catch (SQLException e) {
    System.err.println("Error al obtener datos del usuario "+e.getMessage());
    return null;
} finally(){
    //se agrega un bloque finally, para asegurar (no importa si el flujo del programa
    //fue o no el correcto) cerrar la conexión completamente y dejar limpio el sistema
    if(rs !=null) rs.close();
    if(stmt != null) stmt.close();
    if(xcone !=null) xcone.close();
}
return datos;
}

Now, be sure to re-create a connection to mysql if you make another request, since the connection was closed (you have to create a new one). Try there and tell me. I hope it works for you. If it does not, add the code where you create the connection in "xcone". Greetings!

    
answered by 19.11.2018 в 17:41