在Java循环中抛出DSRA9110e的嵌套式预处理语句执行。声明已结束。错误

时间:2016-07-27 14:52:17

标签: hibernate websphere prepared-statement

我正在使用JSF 2,使用Oracle 11gR2,我的应用程序部署在Websphere 8.5上。我有以下代码,在提到的行号引发主题错误。我使用了两个解决方案,但两者都会抛出相同的错

public void insertEmployee(List<Employee> empList) throws SQLException
{
    Session sess = HibernateUtil.currentSession();
    Connection cn = sess.connection();
    String sqlIdentifier = "insert into EMP_DTLS (ID, EMP_NAME, ORG_NAME) values (?, ?, ?)";
    PreparedStatement statement = cn.prepareStatement(sqlIdentifier);
    cn.setAutoCommit(false);
    Employer em =(Employer) sess.get(Employer.class, empList.get(0).getEmployerId());

       for (Employee currEmployee: empList)
       {           
       statement.setLong(1,getEmpNewId(cn,sess));   // ERROR ON THIS LINE
       statement.setString(2,currEmployee.getEmpName()); 
        statement.setString(3, em.getName());
        statement.addBatch();
        }
        try 
        {    
           statement.executeBatch();
        }
       catch (Exception ex)
    {
        ex.printStackTrace();
        cn.rollback();
    }
    finally
    {
            cn.commit();
            statement.close();
            statement = null;
    }   
    } 
    public Long getEmpNewId(Connection cn, Session sess) throws SQLException
    {
    long empId = 1;
    String sqlIdentifier = "select SEQ_EMP_DTLS_ID.NEXTVAL from dual";
    PreparedStatement statement = null;
    statement = cn.prepareStatement(sqlIdentifier);
    synchronized( this ) 
    {
       ResultSet rs;
    try {
        rs = statement.executeQuery();
        if(rs.next())  empId = rs.getLong(1);
        statement.close();
        statement = null;
        sqlIdentifier = null;
    } catch (SQLException e) {
        e.printStackTrace();
    }

    }
     return empId;
}   

我也使用了以下解决方案,但同样的错误: -

public void insertEmployee(List<Employee> empList) throws SQLException
{
    Session sess = HibernateUtil.currentSession();
    Connection cn = sess.connection();

    String sqlIdentifier = "insert into EMP_DTLS "
            + "(ID, EMP_NAME, ORG_NAME)"
            + "values (SEQ_EMP_DTLS_ID.NEXTVAL, ?, ?)";
    PreparedStatement statement = cn.prepareStatement(sqlIdentifier);
    cn.setAutoCommit(false);
      Employer em =(Employer) sess.get(Employer.class, empList.get(0).getEmployerId());  

       for (Employee currEmployee: empList)
       {           
          statement.setString(1,currEmployee.getEmpName()); // Error on this line 
          statement.setString(2, em.getName());
          statement.addBatch();
        }
      ....

这是错误: -

com.ibm.websphere.ce.cm.objectclosedexception dsra9110e statement is closed

我做错了什么。

1 个答案:

答案 0 :(得分:0)

在休眠调用结束时,可能会关闭连接(或返回到池),因此语句也会关闭。 我的建议是在hibernate中使用nativeQuery,而不是使用jdbc。 改变hibernate给你的连接(setAutocommit)

也是不好的做法