2015-10-20 36 views
2

閱讀REF_CURSOR我嘗試調用存儲過程無法與StoredProcedureItemReader

create or replace PROCEDURE "PROC"(cur OUT SYS_REFCURSOR) 

與這個bean。

@Bean 
    StoredProcedureItemReader<?> itemReader() { 
     StoredProcedureItemReader<?> storedProcedureItemReader = new StoredProcedureItemReader<>(); 
     storedProcedureItemReader.setDataSource(dataSource); 
     storedProcedureItemReader.setProcedureName("PROC"); 
     storedProcedureItemReader.setRefCursorPosition(1); 
     storedProcedureItemReader.setRowMapper(new ColumnMapRowMapper()); 
     storedProcedureItemReader.open(new ExecutionContext()); 
     return storedProcedureItemReader; 
    } 

,但我得到

org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:134) 
    ... 54 more 
Caused by: java.sql.SQLException: Invalid column index 
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameterInternal(OracleCallableStatement.java:125) 
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:304) 
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:393) 
    at oracle.jdbc.driver.OracleCallableStatementWrapper.registerOutParameter(OracleCallableStatementWrapper.java:1569) 
    at org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:202) 
    ... 56 more 

我怎麼能說只有一個PROC出光標,並得到重複的結果?

回答

0

我認爲您需要爲讀取參數cur添加一個參數。嘗試添加以下行打開storedProcedureItemReader前:

storedProcedureItemReader.setParameters(new SqlParameter[] { new SqlParameter("cur", OracleTypes.CURSOR) }); 
0

這就是我如何使用這裏,我們必須設置cursor's位置,否則春天會嘗試做一個的getObject(0)!

@Bean 
    public ItemReader<FailuredProcess> getFailuredProcessReader() throws BatchOMSException { 
     StoredProcedureItemReader<FailuredProcess> reader; 

     try { 
      reader = new StoredProcedureItemReader<FailuredProcess>(); 

      reader.setDataSource(dataSource); 
      reader.setProcedureName(queryHelper.getQuery("query.oms.listFailuredProcessJobs")); 
      reader.setRowMapper(new AlarmRowMapper()); 
      reader.setPreparedStatementSetter(new PreparedStatementSetterAdapter()); 
      reader.setVerifyCursorPosition(false); 
      reader.setParameters(new SqlParameter[] { new SqlParameter("cur", OracleTypes.CURSOR) }); 
      reader.setRefCursorPosition(NumberUtils.INTEGER_ONE); 
      reader.afterPropertiesSet(); 

      return reader; 
     } catch (Exception e) { 
      LOGGER.error(e.getMessage(), e); 
      throw new BatchOMSException(e); 
     } 
0

@Bean @profile( 「包」) 公共ItemReader readerProcedureCall()拋出異常{ StoredProcedureItemReader讀者=新StoredProcedureItemReader <>();

LOGGER.info("***** About getting ResultSet From Procedure **** "); 
    reader.setDataSource(omniflowDataSource); 
    reader.setProcedureName("proce name"); 
    reader.setRowMapper(new CustomRowMapper()); 
    reader.setPreparedStatementSetter(new PreparedStatementSetter(){ 
     @Override 
     public void setValues(PreparedStatement ps) throws SQLException{ 
      CallableStatement eventCallableSt=(CallableStatement)ps; 
       eventCallableSt.registerOutParameter(1, OracleTypes.CURSOR); 
     } 
    }); 
    reader.setVerifyCursorPosition(false); 
    reader.setParameters(new SqlParameter[] {new SqlParameter("pCusRecOut",OracleTypes.CURSOR)}); 
    reader.setRefCursorPosition(NumberUtils.INTEGER_ONE); 
    reader.afterPropertiesSet(); 

    return reader; 
} 
+0

請更新您的答案並提供更多詳細信息,只有代碼答案沒有那麼有用+花時間格式化 –