Java Hibernate多次调用oracle函数

时间:2019-04-12 20:50:29

标签: java hibernate callable-statement

我使用Java Hibernate和EJB框架从内部为循环调用Oracle函数,并在finally块中使用可调用语句close, 第一次调用并关闭后,第二次调用无法打开连接,并且出现错误:“ org.hibernate.exception.GenericJDBCException:执行工作时出错”

private Object callDBFunctionWithParIndex(String functionName, List<Object> procParList, Integer returnParIndex) throws SQLException, Exception, BusinessException {
    Session hibernateSession = getEntityManager().unwrap(Session.class);
    Object returnMes = null;

    try {
        //That is important point,just pass object to ReturningWork<T> whatever you want to return from procedure, an also to execute method return type
        returnMes = hibernateSession.doReturningWork(new ReturningWork<Object>() {
            @Override
            public Object execute(Connection connection) throws SQLException {
                CallableStatement callStmnt = null;
                try {

                    Object innerReturn = null;
                    String inPar = adjustParameterString(procParList);
                    callStmnt = prepareCall(" { ? = call " + functionName + inPar + " } ", connection);
                    // Parameters can be numbered or named
                    if (procParList != null && procParList.size() > 0) {
                        for (int i = 0; i < procParList.size(); i++) {
                            FunctionCallParDTO funPar = (FunctionCallParDTO) procParList.get(i);
                            if (funPar.getParameterMode().equals(ParameterMode.IN)) {
                                callStmnt = addInParameter(callStmnt, i + 1, funPar.getParValue());
                            } else {
                                callStmnt = addOutParameter(callStmnt, i + 1, funPar.getParType());
                            }
                        }
                    }

                    excecute(callStmnt);

                    if (returnParIndex != null) {
                        innerReturn = getOutParameter(callStmnt, returnParIndex);
                    } else {
                        innerReturn = null;
                    }
                    return innerReturn;
                } catch (Exception ex) {
                    throw new SQLException("");
                } finally {
                    //DbUtils.closeQuietly(callStmnt);
                    //DbUtils.closeQuietly(connection);
                }
            }

        });
    } catch (HibernateException e) {
        throw e;
    }
    return returnMes;

}

0 个答案:

没有答案