2016-07-27 13 views
0

私はOracle 11gR2でJSF 2を使用しています。私のアプリケーションはWebsphere 8.5にデプロイされています。私は言及された行番号で主題エラーをスローする次のコードを持っています。私は2つのソリューションを使用しましたが、両方とも同じエラーが発生します。ネストされたPrepared Statement Javaループ投げDSRA9110eでの実行。ステートメントはクローズされています。エラー

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 

私は間違って何をやっています。

答えて

0

おそらく、接続が休止状態コールの終了時に閉じられる(またはプールに返される)可能性があるため、ステートメントも閉じます。 jdbcを使用する代わりに、hibernateでnativeQueryを使用することをお勧めします。 あなたに与えられた接続を変更することも悪い習慣です(setAutocommit)

+0

あなたの回答は助けになりました。実際にはこのコード行はEmployer em =(Employer)sess.get(Employer.class、empList.get(0).getEmployerId()); (私は何の理由を知っていないため)接続を閉じますが、私はこの行の後に接続を再開し、それは働いた。 – learner

関連する問題