间歇性的" java.sql.SQLException:org.apache.commons.dbcp.DelegatingStatement已关闭。"

时间:2013-02-06 11:35:46

标签: java tomcat

我很长时间以来一直面对这个例外。
我的tomcat / java Web应用程序具有非常大的数据库事务。
我使用MSSQL服务器作为我的后端并使用DBCP,tomcat中的数据库连接池 我有以下练习使用它。

我的servlet中的数据库池。

//Skeleton
dopost
{
  //geting connection using JNDI
  //Creating statement object
  // calling method of execution of Business Logic 
}
finally
{
  //closing all DB resorces
}
Business Logic
{
  //do all Database transaction
}

//Original code spinet
public class Frm_mst_operation_II extends HttpServlet
{

protected void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException
{
    try
    {
        String option=httpservletrequest.getParameter("option");            
        InitialContext initialcontext = new InitialContext();
        if(initialcontext == null)
            throw new Exception("Boom - No Context");

        InitialContext initialcontext2 = new InitialContext();
        if(initialcontext2 == null)
            throw new Exception("Boom - No Context");
        Context context = (Context)initialcontext.lookup("java:/comp/env");
        Context context2 = (Context)initialcontext.lookup("java:/comp/env");
        DataSource datasource = (DataSource)context.lookup(RuntimeConf.abi_database);
        DataSource datasource_sql = (DataSource)context2.lookup(RuntimeConf.abi_sql_database);
        if(datasource != null)
        {
            if(dbcon == null)
            {
                dbcon = datasource.getConnection();
            }
            else
            {
                if(!dbcon.isClosed())
                {
                    dbcon.close();
                }
                dbcon = datasource.getConnection();
            }

            if(dbcon_sql == null)
            {
                dbcon_sql = datasource_sql.getConnection();
            }
            else
            {
                if(!dbcon_sql.isClosed())
                {
                    dbcon_sql.close();
                }
                dbcon_sql = datasource_sql.getConnection();
            }

            if(dbcon_sql != null)
            {
                dbcon.setAutoCommit(false);
                dbcon_sql.setAutoCommit(false);
                stmt_ora1 = dbcon.createStatement();
                stmt = dbcon_sql.createStatement();

                if(option.equals("Insert_cge_Data"))
                { 
                    Insert_CGE_Data(httpservletrequest);                
                }

                dbcon.close();
                dbcon_sql.close();
                dbcon=null;
                dbcon_sql=null;
            }
        }
        else
        {
            System.out.println("Data Source Not Found - Error : Exception In Frm_mst_operation_II");
        }

        httpservletresponse.sendRedirect(url);
    }
    catch(Exception exception)
    {
        System.out.println("Exception In Frm_mst_operation_II And Exception Is :: "+exception);
        exception.printStackTrace();
    }
    finally
    {
        if(rset != null) 
        { 
            try 
            {
                rset.close(); 
            }
            catch(SQLException e) 
            {
                System.out.println("Exception in Frm_mst_operation_II "+e);
            }
            rset = null;
        }
        if(stmt != null) 
        {    
            try
            {
                stmt.close(); 
            }
            catch(SQLException e) 
            {
                System.out.println("Exception in Frm_mst_operation_II "+e);
            }
            stmt = null;
        }
        if(stmt_ora1 != null) 
        {    
            try
            {
                stmt_ora1.close(); 
            }
            catch(SQLException e) 
            {
                System.out.println("Exception in Frm_mst_operation_II "+e);
            }
            stmt_ora1 = null;
        }
        if(dbcon != null) 
        { 
            try
            {
                dbcon.close(); 
            }
            catch(SQLException e)
            {
                System.out.println("Exception in Frm_mst_operation_II "+e);
            }
            dbcon = null;

        }
        if(dbcon_sql != null) 
        { 
            try
            {
                dbcon_sql.close(); 
            }
            catch(SQLException e)
            {
                System.out.println("Exception in Frm_mst_operation_II "+e);
            }
            dbcon_sql = null;

        }
    }
}//End Of doPost() Method ...

public void Insert_CGE_Data(HttpServletRequest request) throws IOException,
ServletException, SQLException {

    try 
    {

        query_th_ora = "  INSERT INTO PR_OPR_TH " + " ( "
        + " COMP_N, YR_N, TRN_N, TRN_C, "
        + " TRN_D, MCH_N, SHIFT_N, STRT_TIME, "
        + " END_TIME,  PROD_N, BODY_N, "
        + " CAR_N , FIRE_N, CREATED_UID," + " CREATED_D , " +
        // " UPDATED_UID, UPDATED_D" +
        " TRNTYP , TOT_QTY " + " , RSN_N ) " + " VALUES " + " ( "
        + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
        + "', 'CGE" + TRN_C + "', " + " to_date('" + trn_d
        + "', 'dd/mm/yyyy') , '" + mch_nm
        + "', '" + shift + "', to_date('" + in_time
        + "', 'dd/mm/yyyy hh:mi:ss AM'), " + " to_date('"
        + out_time + "', 'dd/mm/yyyy hh:mi:ss AM'), '" + prod_n
        + "', '" + body_n + "', " + " '" + car_n + "', '"
        + fire_n + "',  '" + user_cd + "',to_date('"+ora_sysdt+"', 'dd/mm/yyyy  HH24:MI:SS'), " + " 'CGE', '" + tot_qty + "'"
        + " , '"+RSN_N+"' ) " + " ";

        //SQL MD20230302

        query_th_sql = "  INSERT INTO PROD.PR_OPR_TH " + " ( "
        + " COMP_N, YR_N, TRN_N, TRN_C, "
        + " TRN_D, MCH_N, SHIFT_N, STRT_TIME, "
        + " END_TIME,  PROD_N, BODY_N, "
        + " CAR_N , FIRE_N, CREATED_UID," + " CREATED_D , " +
        // " UPDATED_UID, UPDATED_D" +
        " TRNTYP , TOT_QTY " + " , RSN_N, FLAG) " + " VALUES " + " ( "
        + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
        + "', 'CGE" + TRN_C + "', " + " PROD.to_date('" + trn_d
        + "', 'dd/mm/yyyy') , '" + mch_nm
        + "', '" + shift + "', PROD.to_date('" + in_time
        + "', 'dd/mm/yyyy hh:mi:ss AM'), " + " PROD.to_date('"
        + out_time + "', 'dd/mm/yyyy hh:mi:ss AM'), '" + prod_n
        + "', '" + body_n + "', " + " '" + car_n + "', '"
        + fire_n + "',  '" + user_cd + "', PROD.to_date('" + sysdt
        + "', 'dd/mm/yyyy hh:mi:ss AM'), " + " 'CGE', '" + tot_qty + "'"
        + " , '"+RSN_N_SQL+"', 'Y'  ) " ;



        //ORA
        query_td_ora = "INSERT INTO PR_OPR_TD" + " ("
        + " COMP_N, YR_N,  TRN_N, TRN_C,  "
        + " TRN_D, TRNDTL_N, SRNO_C, QTY, SMPL_RMK,  "
        + " OPR_N, OPRGRP_N, NXT_OPR,  REJTYP_N,  "
        + " REJ_QTY,  STATUS,  SRNO_N,  DECK_NO,  "
        + " CREATED_UID , CREATED_D , " +
        " TRNTYP, PREV_OPR, GOOD_QTY, PREV_STATUS,  "
        + " BODY_N, BARCODE_NO, PREVTRNDTL_N, PROD_N, " + "" +
        " glzsrno_c, good_wt, exttrn_d," +
        " glztrn_d, unit_wt, layer_n, zones_n, SOTTRN_D, ASSBLD_STS" +
        " )" + " VALUES"
        + "(" + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
        + "', 'CGE" + TRN_C + "', " + " to_date('" + trn_d
        + "', 'dd/mm/yyyy'), '" + trndtl_n + "',  '" + srno_c + "', '" + accept_qty
        + "',  '" + barcode_no + "', " + " '" + OPR_N + "', '"
        + OPRGRP_N + "', '" + NXT_OPR + "', '" + REJTYP_N + "',  "
        + " '" + rej_qty + "', '" + status + "', '" + srno_n
        + "', '" + deck_no + "', " + " '" + user_cd
        + "',  to_date('"+ora_sysdt+"', 'dd/mm/yyyy HH24:MI:SS'), "
        + " 'CGE', '" + opr_n + "', '" + good_qty + "', '"
        + PREV_STATUS + "',  " + " '" + body_n + "', '"
        + barcode_no + "', '" + TRNDTL_N + "'" + " , '"+prod_n+"', " +
        " '" + glzsrno_c + "', '" + good_wt + "', to_date('" + exttrn_d + "' , 'dd/mm/yyyy'), " +
        " to_date('" + glztrn_d + "' , 'dd/mm/yyyy'), '" + unit_wt + "', '" + layer_n + "', " +
        " '" + zones_n + "', to_date('"+SOTTRN_D+"', 'dd/mm/yyyy'),'"+ASSBLD_STS+"' " +
        ")";
        //SQL MD20230302
        query_td_sql = "INSERT INTO PROD.PR_OPR_TD" + " ("
        + " COMP_N, YR_N,  TRN_N, TRN_C,  "
        + " TRN_D, TRNDTL_N, SRNO_C, QTY, SMPL_RMK,  "
        + " OPR_N, OPRGRP_N, NXT_OPR,  REJTYP_N,  "
        + " REJ_QTY,  STATUS,  SRNO_N,  DECK_NO,  "
        + " CREATED_UID , CREATED_D , " +
        " TRNTYP, PREV_OPR, GOOD_QTY, PREV_STATUS,  "
        + " BODY_N, BARCODE_NO, PREVTRNDTL_N, PROD_N, " + "" +
        " glzsrno_c, good_wt, exttrn_d," +
        " glztrn_d, unit_wt, layer_n, zones_n, FLAG, SOTTRN_D, ASSBLD_STS" +
        " )" + " VALUES"
        + "(" + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
        + "', 'CGE" + TRN_C + "', " + " PROD.to_date('" + trn_d
        + "', 'dd/mm/yyyy'), '" + trndtl_n + "',  '" + srno_c + "', '" + accept_qty
        + "',  '" + barcode_no + "', " + " '" + OPR_N + "', '"
        + OPRGRP_N + "', '" + NXT_OPR + "', '" + REJTYP_N_SQL + "',  "
        + " '" + rej_qty + "', '" + status + "', '" + srno_n
        + "', '" + deck_no + "', " + " '" + user_cd
        + "', PROD.to_date('" + sysdt + "', 'dd/mm/yyyy hh:mi:ss AM'), "
        + " 'CGE', '" + opr_n + "', '" + good_qty + "', '"
        + PREV_STATUS + "',  " + " '" + body_n + "', '"
        + barcode_no + "', '" + TRNDTL_N + "'" + " , '"+prod_n+"', " +
        " '" + glzsrno_c + "', '" + good_wt + "', PROD.to_date('" + exttrn_d + "' , 'dd/mm/yyyy'), " +
        " PROD.to_date('" + glztrn_d + "' , 'dd/mm/yyyy'), '" + unit_wt + "', '" + layer_n + "', " +
        " '" + zones_n + "', 'Y', PROD.to_date('"+SOTTRN_D+"', 'dd/mm/yyyy'), '"+ASSBLD_STS+"' " 
        +")";

        //System.out.println(query_th_ora);
        stmt_ora1.executeUpdate(query_th_ora);
        //System.out.println(query_th_sql);
        stmt3.executeUpdate(query_th_sql);

        //System.out.println(query_td_ora);
        stmt_ora1.executeUpdate(query_td_ora);
        //System.out.println(query_td_sql);
        stmt3.executeUpdate(query_td_sql);

    } 
    catch (SQLException e) 
    {
        dbcon.rollback(); 
        dbcon_sql.rollback();
        e.printStackTrace();

    }
    catch (Exception e) 
    {
        dbcon.rollback(); 
        dbcon_sql.rollback();
        e.printStackTrace();
    }

}

} 而在Business Logic方法之间的某些地方,我总是得到那个例外。
奇怪的是,我总是在我的servlet中得到这个异常,但从不在Databean中。
我知道为什么会引起这种异常,但我不明白原因。
第二件事我的tomcat服务也间歇性地挂起。
变得完全没有反应,需要重新启动才能再次运行应用程序。

所以请求所有人请回复我的查询/问题/问题并帮助我摆脱这个泥袋。有一点需要提及的是我的客户端机器在WiFi网络上运行,它对提出这个问题有什么影响吗?

提前多多感谢

1 个答案:

答案 0 :(得分:0)

在关闭时尝试使用PreparedStatemen时,您可以获得此异常 此ID在此question

中详细说明