■ 现象 DB2,Schema和访问DB的用户名不一致。 调用存储过程时必须指定Schema,否则Error。
DB2 不指定Schema调用存储过程失败 SQLCODE=-440, SQLSTATE=42884
■ 错误代码 存储过程
SET SCHEMA = "QUOTATION"; SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1"; CREATE PROCEDURE COPY_QUOTATION_TABLES (。。。。。。) BEGIN 。。。。。。 END;
GRANT EXECUTE ON PROCEDURE "QUOTATION"."COPY_QUOTATION_TABLES"( VARCHAR(36), VARCHAR(36), INTEGER, TIMESTAMP, INTEGER, INTEGER ) TO USER "DB2INST1" WITH GRANT OPTION;
Mapper.xml
<update id="copyQuotationTables" statementType="CALLABLE" parameterType="map"> CALL COPY_QUOTATION_TABLES ( #{quotationId, jdbcType=VARCHAR, mode=IN} , #{newQuotationId, jdbcType=VARCHAR, mode=IN} , #{userId, jdbcType=INTEGER, mode=IN} , #{currentTime, jdbcType=TIMESTAMP, mode=IN} , #{reasonTypeFlag, jdbcType=INTEGER, mode=IN} , #{copyReason, jdbcType=INTEGER, mode=IN} ) </update>
Mapper.java
/** * Copy Quotation Tables * * @param param the parameter */ void copyQuotationTables(Map<String, Object> param);
Service.java
Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("quotationId", quotationId); paramMap.put("newQuotationId", newQuotationId); paramMap.put("userId", userId); paramMap.put("currentTime", currentTime); paramMap.put("reasonTypeFlag", 1); paramMap.put("copyReason", 1); Mapper.copyQuotationTables(paramMap);
Tomcat context.xml
<Resource name="jdbc/testDS" auth="Container" type="javax.sql.DataSource" driverClassName="com.ibm.db2.jcc.DB2Driver" url="jdbc:db2://127.0.0.1:50000/test:currentSchema=QUOTATION;" username="db2inst1" password="db2inst1" maxActive="100" maxIdle="30"/>
■ 正确代码 Tomcat context.xml中追加currentFunctionPath。
<Resource name="jdbc/testDS" auth="Container" type="javax.sql.DataSource" driverClassName="com.ibm.db2.jcc.DB2Driver" url="jdbc:db2://127.0.0.1:50000/test:currentSchema=QUOTATION;currentFunctionPath=QUOTATION;" username="db2inst1" password="db2inst1" maxActive="100" maxIdle="30"/>
如果想在DB工具(如:Toda)中调用存储过程,但不指定Schema, 如:CALL COPY_QUOTATION_TABLES(。。。。。。) 需要在创建存储过程时,指定Current Path
SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1","QUOTATION";
■ 原因 DB2存储过程(和函数)不受CURRENT SCHEMA控制,而是由CURRENT PATH控制。