Bom eu tenho um metodo que valida o login
ele verifica se a senha e o login são iguais
se sim ele vai fazer um update na tabela mudando o status " ONLINE " para 1
eu estou tentando entender mais sobre commit
gostaria de saber se estou mt errado nesse metodo?
como eu conseguiria fazer o rollback também
ficaria grato.
METODO:
public boolean validarSenha(Usuario u) {
String sql = "SELECT * FROM usuario where login = ? and senha = ?";
try {
this.con = new ConnectionFactory().getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, u.getLogin());
stmt.setString(2, u.getSenha());
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
Usuario usuarioLogado = new Usuario(
rs.getLong("id_usuario"),
rs.getString("login"),
rs.getString("senha"),
rs.getInt("idpermissao"),
rs.getString("nome"),
rs.getBoolean("online"));
con.setAutoCommit(false);
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String online = "UPDATE usuario SET online=1 WHERE id_usuario = " + Sessao.getInstancia().getUsuario().getId() + " ";
stmt2.executeUpdate(online);
con.commit();
aSessao.getInstancia().setUsuario(usuarioLogado);
stmt.close();
rs.close();
return true;
} else {
stmt.close();
rs.close();
return false;
}
} catch (SQLException ex) {
Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}