0

我在 servlet 中有一个更新查询。语法是正确的,但是当我执行查询时,什么也没有发生。执行保持冻结状态,命令永远不会结束。几分钟后,消息“广告:GRIZZLY0023:中断空闲线程:http-thread-pool-8080(5)”。每隔几秒打印一次

我尝试过:

con.stmt.executeUpdate ("SQL");
con.rset = con.stmt.executeQuery ("SQL");

并使用 PreparedStatement。

这是查询:

System.out.println(""
                    + " UPDATE JSP_TABLE SET "
                    + " field1        = '" + request.getParameter("input1") + "',"
                    + " field2= '" + request.getParameter("input2") + "',"
                    + " field3= '" + request.getParameter("input3") + "',"
                    + " field4= '" + request.getParameter("input4") + "',"
                    + " field5= '" + request.getParameter("input5") + "',"
                    + " field6= '" + request.getParameter("input6") + "',"
                    + " field7= '" + request.getParameter("input7") + "',"
                    + " field8= '" + indicador + " VS " + request.getParameter("input8") + "',"
                    + " field9= '" + request.getParameter("input") + " al " + request.getParameter("hasta9") + "'"
                    + " WHERE ID_PK = " + request.getParameter("inputPK"));

我所有的语句、结果集和连接在使用结束时都会关闭。我有第二次更新,效果很好。

    package Funciones;

    import conexion.conectar;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class ActualizarDinamica extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException, SQLException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            conectar con = new conectar();
            try {
                /*
                 * TODO output your page here. You may use following sample code.
                 */
                con.stmt = con.conn.createStatement();

                String indicador = "";
                switch (Integer.parseInt(request.getParameter("indicador"))) {
                    case 1:
                        indicador = "Nivel de Servicio";
                        break;
                    case 2:
                        indicador = "Venta";
                        break;
                    case 3:
                        indicador = request.getParameter("txt_otro");
                        break;
                    case 0:
                        indicador = "Presencia";
                        break;
                };

                 /* way 1   
                  * con.rset = con.stmt.executeQuery(""
                        + " UPDATE JSP_TABLE SET "
                        + " FIELD1        = '" + request.getParameter("input1") + "',"
                        + " FIELD2           = '" + request.getParameter("input2") + "',"
                        + " FIELD3          = '" + request.getParameter("input3") + "',"
                        + " FIELD4  = '" + request.getParameter("input4") + "',"
                        + " FIELD5         = '" + request.getParameter("input5") + "',"
                        + " FIELD6       = '" + request.getParameter("input6") + "',"
                        + " FIELD7          = '" + request.getParameter("input7") + "',"
                        + " FIELD8         = '" + indicador + " VS " + request.getParameter("input8") + "',"
                        + " FIELD9     = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
                        + " WHERE FIELD_PK = " + request.getParameter("input11_ID")); 

              con.rset = con.stmt.executeQuery(""
                        + " UPDATE JSP_TABLE2 SET"
                        + " FIELD_DATE =  TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
                        + " WHERE FIELD_ID = " + request.getParameter("input11_ID"));

*/


              /*  way 2
                con.stmt.executeUpdate(""
                        + " UPDATE JSP_TABLE SET "
                        + " FIELD1        = '" + request.getParameter("input1") + "',"
                        + " FIELD2           = '" + request.getParameter("input2") + "',"
                        + " FIELD3          = '" + request.getParameter("input3") + "',"
                        + " FIELD4  = '" + request.getParameter("input4") + "',"
                        + " FIELD5         = '" + request.getParameter("input5") + "',"
                        + " FIELD6       = '" + request.getParameter("input6") + "',"
                        + " FIELD7          = '" + request.getParameter("input7") + "',"
                        + " FIELD8         = '" + indicador + " VS " + request.getParameter("input8") + "',"
                        + " FIELD9     = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
                        + " WHERE FIELD_PK = " + request.getParameter("input11_ID"));


                con.stmt.executeUpdate(""
                        + " UPDATE JSP_TABLE2 SET"
                        + " FIELD_DATE =  TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
                        + " WHERE FIELD_ID = " + request.getParameter("input11_ID"));
                     */

                //way 3
                PreparedStatement updateSmallQuery = null;
                PreparedStatement updateBigQuery = null;

                String bigQuery=
                        ""
                        + " UPDATE JSP_TABLE SET "
                        + " FIELD1        = '" + request.getParameter("input1") + "',"
                        + " FIELD2           = '" + request.getParameter("input2") + "',"
                        + " FIELD3          = '" + request.getParameter("input3") + "',"
                        + " FIELD4  = '" + request.getParameter("input4") + "',"
                        + " FIELD5         = '" + request.getParameter("input5") + "',"
                        + " FIELD6       = '" + request.getParameter("input6") + "',"
                        + " FIELD7          = '" + request.getParameter("input7") + "',"
                        + " FIELD8         = '" + indicador + " VS " + request.getParameter("input8") + "',"
                        + " FIELD9     = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
                        + " WHERE FIELD_PK = " + request.getParameter("input11_ID");

                String smallQuery= ""
                        + " UPDATE JSP_TABLE2 SET"
                        + " FIELD_DATE =  TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
                        + " WHERE FIELD_ID = " + request.getParameter("input11_ID");
                try {
                    con.conn.setAutoCommit(false);
                    updateSmallQuery = con.conn.prepareStatement(smallQuery);
                    updateBigQuery = con.conn.prepareStatement(bigQuery);
                    updateBigQuery.executeUpdate();
                    updateSmallQuery.executeUpdate();
                    con.conn.commit();
                } catch (SQLException e) {
                    System.out.println("catch!");
                    System.out.println(e.getMessage());
                    if (con != null) {
                        try {
                            System.err.print("Transaction is being rolled back");
                            con.conn.rollback();
                        } catch (SQLException excep) {
                        }
                    }
                } finally {
                    if (updateSmallQuery != null) {
                        updateSmallQuery.close();
                    }
                    if (updateBigQuery != null) {
                        updateBigQuery .close();
                    }
                    con.conn.setAutoCommit(true);
                }
                response.sendRedirect("sol_env_c.jsp");

            } finally {
                con.conn.commit();
                con.conn.close();
                con.stmt.close();
                out.close();
            }
        }

    }

希望这有助于找到答案。PD:小的运行没有问题。大的就是问题所在。

编辑:

这是 conectar()

public conectar() {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            System.out.println("Oracle JDBC driver loaded ok.");
            conn = DriverManager.getConnection(params, user, password);
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }
4

1 回答 1

0

Bill 几乎可以肯定是正确的,而您只是锁定了要更新的行。您提到“语法是正确的”,这表明您可能已经直接在数据库中检查了它 - 例如使用 SQL*Plus 或 SQL Developer - 并且没有在该会话中发出rollback(或)。commit尝试从您的代码更新相同的行然后挂起等待锁被释放。

如果您可以找到手动运行更新的会话,请在其中发出 a rollback。如果不能,您可以通过如下查询查看哪些会话持有锁:

select vs.osuser, vs.process, vs.logon_time, vs.sid, vs.serial#, vs.program,
    dl.lock_type, dl.mode_held, dl.blocking_others
from dba_locks dl
left join v$session vs on vs.sid = dl.session_id
where lock_type != 'Media Recovery'
and lock_type != 'Redo Thread';

当您的代码正在运行 - 并挂起 - 您可以通过以下方式查看阻止它的内容:

select vsw.osuser, vsw.process, vsw.logon_time, vsw.sid, vsw.program,
    dw.lock_type, dw.mode_held,
    vsh.osuser, vsh.process, vsh.logon_time, vsh.sid, vsh.serial#, vsh.program
from dba_waiters dw
left join v$session vsw on vsw.sid = dw.waiting_session
left join v$session vsh on vsh.sid = dw.holding_session;

或搜索其他示例,例如this。您需要权限来查询v$性能视图,如果您没有权限,您可能需要 DBA 帮助。如果您无法手动回滚锁定,您可能需要 DBA 帮助才能终止锁定会话。

您也应该真正注意有关参数化语句的评论。

于 2012-08-30T19:16:29.830 回答