[英]Exec stored procedure not working with JdbcCursorItemReader
I need to read data from both select and exec Sybase queries using JdbcCursorItemReader in Spring-Batch.我需要在 Spring-Batch 中使用 JdbcCursorItemReader 从 select 和 exec Sybase 查询中读取数据。 While SELECT queries run perfectly, the Exec queries are running into the following error:虽然 SELECT 查询运行完美,但 Exec 查询遇到以下错误:
Caused by: org.springframework.jdbc.UncategorizedSQLException: Executing query;引起:org.springframework.jdbc.UncategorizedSQLException: Executing query; uncategorized SQLException for SQL [exec proc_1]; SQL [exec proc_1] 的未分类 SQLException; SQL state [ZZZZZ]; SQL 状态 [ZZZZZ]; error code [7773];错误代码 [7773]; Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。
; ; nested exception is com.sybase.jdbc4.jdbc.SybSQLException: Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.嵌套异常是 com.sybase.jdbc4.jdbc.SybSQLException:在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。 ... 43 common frames omitted ...省略了43个常用帧
Caused by: com.sybase.jdbc4.jdbc.SybSQLException: Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.引起:com.sybase.jdbc4.jdbc.SybSQLException:在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。
... 45 common frames omitted ...省略了 45 个常用帧
The code for the JdbcCursorItemReader is as follows: JdbcCursorItemReader 的代码如下:
JdbcCursorItemReader itemReader = new JdbcCursorItemReader();
ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
itemReader.setDataSource(getDataSource());
itemReader.setRowMapper(rowMapper);
itemReader.setFetchSize(batchSize);
itemReader.setSql(dataSql); //datasql is passed Sybase query
Even after using StoredProcedureItemReader as suggested, getting the following error:即使按照建议使用 StoredProcedureItemReader 后,也会出现以下错误:
Caused by: org.springframework.jdbc.UncategorizedSQLException: Executing stored procedure;引起:org.springframework.jdbc.UncategorizedSQLException:正在执行存储过程; uncategorized SQLException for SQL [{call ftsps_report(?, ?)}]; SQL [{call ftsps_report(?, ?)}] 的未分类 SQLException; SQL state [ZZZZZ]; SQL 状态 [ZZZZZ]; error code [7773];错误代码 [7773]; Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。
; ; nested exception is com.sybase.jdbc4.jdbc.SybSQLException: Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.嵌套异常是 com.sybase.jdbc4.jdbc.SybSQLException:在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:90) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82) at org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:226) at org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:406) at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ... 43 common frames omitted
Caused by: com.sybase.jdbc4.jdbc.SybSQLException: Execute cursor 'jconnect_implicit_1' is declared on a procedure which contains a non-SELECT or a SELECT with a COMPUTE clause.引起:com.sybase.jdbc4.jdbc.SybSQLException:在包含非 SELECT 或带有 COMPUTE 子句的 SELECT 的过程上声明了执行游标“jconnect_implicit_1”。 For the declaration of this cursor to be legal it should have a single SELECT statement without a COMPUTE clause.为了使这个游标的声明合法,它应该有一个没有 COMPUTE 子句的 SELECT 语句。
at com.sybase.jdbc4.tds.Tds.processEed(Tds.java:4112) at com.sybase.jdbc4.tds.Tds.nextResult(Tds.java:3229) at com.sybase.jdbc4.tds.Tds.getResultSetResult(Tds.java:3974) at com.sybase.jdbc4.tds.TdsCursor.open(TdsCursor.java:333) at com.sybase.jdbc4.jdbc.SybCallableStatement.sendRpc(SybCallableStatement.java:2032) at com.sybase.jdbc4.jdbc.SybCallableStatement.execute(SybCallableStatement.java:241) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) at com.sun.proxy.$Proxy83.execute(Unknown Source) at org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:210)
Have defined the StoredProcedureItemReader as follows:已定义 StoredProcedureItemReader 如下:
StoredProcedureItemReader itemReader = new StoredProcedureItemReader();
ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
itemReader.setDataSource(getDataSource());
itemReader.setRowMapper(rowMapper);
itemReader.setFetchSize(batchSize);
itemReader.setProcedureName(dataSql);
SqlParameter[] parameter = {new SqlParameter("date1", Types.DATE),new SqlParameter("date2", Types.DATE)};
itemReader.setParameters(parameter);
itemReader.setPreparedStatementSetter(psSetter);
where psSetter setValues I have defined as follows:其中 psSetter setValues 我定义如下:
ps.setDate(1, Date.ValueOf(paramList.get(0));
ps.setDate(2, Date.ValueOf(paramList.get(1));
I tried for few other stored procedures also and got similar issues.我也尝试了其他一些存储过程并遇到了类似的问题。 Is it an error with the definition.是不是定义错误。 I just want to define parameters by index preferably.我只想最好按索引定义参数。 Is there any straightforward way of doing so?有什么直接的方法吗? Is it expecing the parameters in the storedproc to be named date1/date2 specifically and failing because of that?是否期望存储过程中的参数专门命名为 date1/date2 并因此而失败?
For stored procedures, you need to use the StoredProcedureItemReader
and not the JdbcCursorItemReader
, something like:对于存储过程,您需要使用StoredProcedureItemReader
而不是JdbcCursorItemReader
,例如:
@Bean
public StoredProcedureItemReader storedProcedureItemReader() {
StoredProcedureItemReader reader = new StoredProcedureItemReader();
reader.setProcedureName("yourProcedureName");
// set other properties
return reader;
}
Please refer to the reference documentation for more details.有关更多详细信息,请参阅参考文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.