Java 循环中的嵌套准备语句执行引发 DSRA9110e。语句已关闭。错误
Nested Prepared Statement Execution in Java loop throwing DSRA9110e. Statement is Closed. error
我正在使用 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
我做错了什么。
可能连接在休眠调用结束时关闭(或返回到池中),因此语句也关闭了。
我的建议是在休眠中使用 nativeQuery,而不是使用 jdbc。
改变 hibernate 给你的连接也是不好的做法 (setAutocommit)
我正在使用 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
我做错了什么。
可能连接在休眠调用结束时关闭(或返回到池中),因此语句也关闭了。 我的建议是在休眠中使用 nativeQuery,而不是使用 jdbc。 改变 hibernate 给你的连接也是不好的做法 (setAutocommit)