■ 现象
DB2,Schema和访问DB的用户名不一致。
调用存储过程时必须指定Schema,否则Error。
SET SCHEMA = "QUOT";
SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1";
CREATE PROCEDURE copY_TABLES (。。。。。。)
BEGIN
。。。。。。
END;
GRANT EXECUTE ON PROCEDURE "QUOT"."copY_TABLES"(INTEGER, TIMESTAMP, INTEGER, INTEGER ) TO USER "DB2INST1" WITH GRANT OPTION;
Mapper.xml
<update id="copyTables" statementType="CALLABLE" parameterType="map">
CALL copY_TABLES (
#{userId, jdbcType=INTEGER, mode=IN}
, #{currentTime, jdbcType=TIMESTAMP, mode=IN}
, #{reasonTypeFlag, jdbcType=INTEGER, mode=IN}
, #{copyReason, jdbcType=INTEGER, mode=IN} )
</update>
Mapper.java
/**
* copy Tables
*
* @param param the parameter
*/
void copyTables(Map<String, Object> param);
Service.java
Map<String, Object> paramMap = new HashMap<String, Object>();
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=QUOT;"
username="db2" password="db2" 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=QUOT;currentFunctionPath=QUOT;"
username="db2" password="db2" maxActive="100" maxIdle="30"/>
如果想在DB工具(如:Toda)中调用存储过程,但不指定Schema,
如:CALL copY_TABLES(。。。。。。)
需要在创建存储过程时,指定Current Path
SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1","QUOT";
■ 原因
DB2存储过程(和函数)不受CURRENT SCHEMA控制,而是由CURRENT PATH控制。