BatchSqlUpdate所需的存储过程OUT参数值提取

时间:2015-04-02 10:31:13

标签: java spring oracle batch-processing

我成功使用BatchSqlUpdate实现来执行批量存储过程(Oracle Function)调用。呼叫成功,我在后端更新结果。

唯一的问题是如何成功解析return和OUT参数值。 以下是我如何实现这一点,

public class MyClass extends BatchSqlUpdate {

    private StoredProcedureAttributes spAttributes;
    private final Map<String, Object> inParameters;
    List<Map<String, Object>> batchedOutArguments;

....

    public MyClass (StoredProcedureAttributes spAttributes, int batchSizeRequired) 
            throws SQLException {

        // Call statement will look like
        // { :SP_OUT_RETURN_VALUE = call myschema.MY_FUNC(:p_error_message,:p_module_context_name,:p_sql_error,:p_host_id,:p_code,:p_value_type,:p_value)}
        super (spAttributes.getDataSource(), spAttributes.getCallStatement());

        this.spAttributes = spAttributes;
        declareParameters();
        setBatchSize(batchSizeRequired);        
        compile();

.....


    private void declareParameters() {

        for (StoredProcedureParameter spParameter : this.spAttributes.getStoredProcedureParameters()) {

            switch (spParameter.getArgumentType()) {
                case IN: 
                    declareParameter(new SqlParameter(spParameter.getParameterName(), spParameter.getSqlType()));
                    break;
                case OUT:
                    declareParameter(new SqlOutParameter(spParameter.getParameterName(), spParameter.getSqlType()));
                    break;
                case IN_OUT: 
                    declareParameter(new SqlInOutParameter(spParameter.getParameterName(), spParameter.getSqlType()));
                    break;
            }
        }

        LOGGER.debug("Declared parameters for [{}]", STORED_PROC_NAME);
    }

.....

    public int execute(EntityWriterState writerState) throws StoredProcedureException {

        int rtnVal = 0;

        for (StoredProcedureParameter spParameter : this.spAttributes.getStoredProcedureParameters()) {

            switch (spParameter.getArgumentType()) {
                case IN: 
                case IN_OUT: break;
                case OUT:
                    Object outParam = null;
                    inParameters.put(spParameter.getParameterName(), outParam);
                    break;
            }
        }

        try {

            super.updateByNamedParam(inParameters);
            batchedOutArguments.add(inParameters);
        } catch (Exception ex) {
            throw new StoredProcedureException(ex);
        }

包含OUT参数引用的每组参数(用于进行一次过程调用)都保存在batchedOutArguments中。

但是为OUT类型变量获取任何东西都没有运气, 在Oracle方面,我的程序看起来像,

create or replace FUNCTION           "MY_FUNC" (
      p_error_message         OUT NOCOPY VARCHAR2,
      p_module_context_name   OUT NOCOPY VARCHAR2,
      p_sql_error             OUT NOCOPY VARCHAR2,
      p_host_id             IN VARCHAR2,
      p_code                 IN VARCHAR2,                               
      p_value_type           IN VARCHAR2, 
      p_value                IN NUMBER)
    RETURN NUMBER
  AS
....

是否有任何方法可以提取OUT变量?

0 个答案:

没有答案