问题
I'm using a Groovy
script in Mule ESB
to get output parameters from Oracle
stored procedure (including cursor) and getting an exception.
Minimal example:
import groovy.sql.Sql
import oracle.jdbc.pool.OracleDataSource
import oracle.jdbc.driver.OracleTypes
def ds = new OracleDataSource()
// setting data source parameters here
def sql = new Sql(ds)
def data = []
sql.call("""declare
result_table sys_refcursor;
begin
open result_table for select 1 as a from dual;
insert into CURSOR_TEST (ID) values (1);
commit;
${Sql.resultSet OracleTypes.CURSOR} := result_table;
insert into CURSOR_TEST (ID) values (2);
commit;
end;
"""
){ table ->
throw new RuntimeException("Never getting this exception.")
table.eachRow {
data << it.toRowResult()
}
}
sql.close()
return data
Error:
Message : java.sql.SQLException: Closed Statement (javax.script.ScriptException) Code : MULE_ERROR--2 -------------------------------------------------------------------------------- Exception stack is: 1. Closed Statement(SQL Code: 17009, SQL State: + 99999) (java.sql.SQLException) oracle.jdbc.driver.SQLStateMapping:70 (null) 2. java.sql.SQLException: Closed Statement (javax.script.ScriptException) org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:323 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html) 3. java.sql.SQLException: Closed Statement (javax.script.ScriptException)
(org.mule.api.transformer.TransformerException) org.mule.module.scripting.transformer.ScriptTransformer:39 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html) -------------------------------------------------------------------------------- Root Exception stack trace: java.sql.SQLException: Closed Statement at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70) at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199) + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything) ********************************************************************************
Select
from CURSOR_TEST
returns 1
and 2
.
Oracle server version: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
.
Mule version: 3.5.0
.
I'm using jdbc\lib\ojdbc6.jar
from oracle client version 11.1.0.7.0
.
What am I doing wrong?
回答1:
The following code can help you get variable of SYS_REFCURSOR
from Oracle anonymous block.
We should focus on a few key details:
- Class
groovy.sql.Sql
doesn't have correspondingOutParameter
and we make it manually asCURSOR_PARAMETER
and pass it tosql.call
method - Consider that the block starts with
{call DECLARE
and ends withEND }
without semicolon after END. Otherwise we can get a poorly recognizableSQLException
in the face. - The question marks
?
inside thesqlString
are places for parameter bindings. Bindings are made in the natural order. In this example:- the first
?
binds with the first element inparametersList
:"abc"
, treating the value asIN
parameter ; - the second
?
binds with CURSOR_PARAMETER treating the value asOUT
parameter of passed type;
- the first
- There is only one enter into closure after
sql.call
andResultSet rs
provide rows of cursormy_cur
declared in anonymous block.
import groovy.sql.OutParameter
import groovy.sql.Sql
import oracle.jdbc.OracleTypes
import java.sql.ResultSet
def driver = 'oracle.jdbc.driver.OracleDriver'
def sql = Sql.newInstance('jdbc:oracle:thin:@MY-SERVER:1521:XXX', 'usr', 'psw', driver)
// special OutParameter for cursor type
OutParameter CURSOR_PARAMETER = new OutParameter() {
public int getType() {
return OracleTypes.CURSOR;
}
};
// look at some ceremonial wrappers around anonymous block
String sqlString = """{call
DECLARE
my_cur SYS_REFCURSOR;
x VARCHAR2(32767) := ?;
BEGIN
OPEN my_cur
FOR
SELECT x || level AS my_column FROM dual CONNECT BY level < 10;
? := my_cur;
END
}
""";
// the order of elements matches the order of bindings
def parametersList = ["abc", CURSOR_PARAMETER];
// rs contains the result set of cursor my_cur
sql.call(sqlString, parametersList) { ResultSet rs ->
while (rs.next()) {
println rs.getString("my_column")
}
};
来源:https://stackoverflow.com/questions/20242512/how-to-get-cursor-from-oracle-using-groovy