Ich verwende JSF 2, mit Oracle 11gR2 und meine Anwendung wird auf Websphere 8.5 bereitgestellt. Ich habe folgenden Code, der Betreff-Fehler bei der angegebenen Zeilennummer wirft. Ich benutzte zwei Lösungen, aber beide würden denselben Fehler werfen.Verschachtelte vorbereitete Anweisungsausführung in der Java-Schleife, die DSRA9110e wirft. Aussage ist geschlossen. Fehler
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;
}
habe ich folgende Lösung auch, aber denselben Fehler: -
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();
}
....
Dies ist der Fehler: -
com.ibm.websphere.ce.cm.objectclosedexception dsra9110e statement is closed
Was mache ich falsch.
Ihre Antwort half. Tatsächlich ist diese Codezeile Employer em = (Employer) sess.get (Employer.class, empList.get (0) .getEmployerId()); würde die Verbindung schließen (weil ich nicht weiß, welchen Grund), aber ich öffnete Verbindung nach dieser Linie wieder und es funktionierte. – learner