调整如下Db2 CFG参数:
mon_locktimeout
: HISTORY
mon_deadlock
: WITHOUT_HIST
mon_lockwait
: HISTORY
mon_lw_thresh
: 30000000
要求:若 mon_locktimeout
和 mon_deadlock
本身的设置已经高于 HISTORY
和 WITHOUT_HIST
,则无需修改这两个参数。
对于这几参数的说明,详见Db2的Knowledge Center(例如: https://www.ibm.com/docs/en/db2/11.5?topic=parameters-mon-locktimeout-monitoring-lock-timeout
)。
mon_locktimeout
:默认值是 NONE
,可选值为 NONE
, WITHOUT_HIST
, HISTORY
, HIST_AND_VALUES
mon_deadlock
:默认值是 WITHOUT_HIST
,可选值为 NONE
, WITHOUT_HIST
, HISTORY
, HIST_AND_VALUES
mon_lockwait
:默认值是 NONE
,可选值为 NONE
, WITHOUT_HIST
, HISTORY
, HIST_AND_VALUES
mon_lw_thresh
:默认值是 5000000
,可选值为 1000
到 MAX_INT
(单位是微秒)select name, value from table(db_get_cfg()) t1
call sysproc.admin_cmd((select '
update db cfg using
,
......
' from sysibm.sysdummy1))
可以一次性更新多个参数。
xxxxxx
<if test="xxx">xxxxxxif>
xxxxxx
当 test
结果为true时,SQL语句里就会包含对应的 xxxxxx
。
为了确认 if
动态生成的SQL语句是否有误,可以在运行SQL之前,打印出SQL语句,方便检查。在 mybatis-config.xml
里添加如下配置:
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
代码结构如下:
完整代码如下:
MyMapper.java
:package dao;
import org.apache.ibatis.annotations.Param;
import pojo.EventMonitorLockingDbCfg;
import java.util.List;
public interface MyMapper {
public List<EventMonitorLockingDbCfg> doGetDbCfg();
public void doUpdateDbCfg(@Param("p1")boolean mon_locktimeout_higher, @Param("p2")boolean mon_deadlock_higher);
default public void updateDbCfg() {
List<EventMonitorLockingDbCfg> list = doGetDbCfg();
String MON_LOCKTIMEOUT = "mon_locktimeout";
String MON_DEADLOCK = "mon_deadlock";
boolean mon_locktimeout_higher = false;
boolean mon_deadlock_higher = false;
for (int i = 0; i < list.size(); i++) {
EventMonitorLockingDbCfg e = list.get(i);
String name = e.getName();
String value = e.getValue();
if (MON_LOCKTIMEOUT.equals(name)) {
if ("HIST_AND_VALUES".equals(value)) {
mon_locktimeout_higher = true;
}
} else if (MON_DEADLOCK.equals(name)) {
if ("HISTORY".equals(value) || "HIST_AND_VALUES".equals(value)) {
mon_deadlock_higher = true;
}
}
}
doUpdateDbCfg(mon_locktimeout_higher, mon_deadlock_higher);
}
}
MyMapper.xml
:
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.MyMapper">
<select id = "doGetDbCfg" resultType="pojo.EventMonitorLockingDbCfg">
select name, value from table(db_get_cfg()) t1
where name in ('mon_deadlock', 'mon_locktimeout')
select>
<select id="doUpdateDbCfg" statementType="CALLABLE" timeout="60">
call sysproc.admin_cmd((select '
update db cfg using
<if test="!p1">mon_locktimeout HISTORYif>
<if test="!p2">mon_deadlock WITHOUT_HISTif>
mon_lockwait HISTORY
mon_lw_thresh 30000000
' from sysibm.sysdummy1))
select>
mapper>
EventMonitorLockingDbCfg.java
:package pojo;
public class EventMonitorLockingDbCfg {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
mybatis-config.xml
:
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<environments default="db2">
<environment id="db2">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="jdbc:db2://127.0.0.1:50000/sample"/>
<property name="username" value="db2inst1"/>
<property name="password" value="passw0rd"/>
dataSource>
environment>
environments>
<mappers>
<mapper class="dao.MyMapper"/>
mappers>
configuration>
TestMybatis.java
:import dao.MyMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class TestMybatis {
@Test
public void test1() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
try {
MyMapper mapper = sqlSession.getMapper(MyMapper.class);
mapper.updateDbCfg();
} finally {
sqlSession.close();
}
}
}
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>test0822artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.10version>
dependency>
<dependency>
<groupId>com.ibm.db2.jccgroupId>
<artifactId>db2jccartifactId>
<version>db2jcc4version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
project>
首先查看一下相关的Db2 CFG:
➜ ~ db2 get db cfg | grep -i "MON_LOCKTIMEOUT\|MON_DEADLOCK\|mon_lockwait\|mon_lw_thresh"
Lock timeout events (MON_LOCKTIMEOUT) = NONE
Deadlock events (MON_DEADLOCK) = WITHOUT_HIST
Lock wait events (MON_LOCKWAIT) = NONE
Lock wait event threshold (MON_LW_THRESH) = 5000000
运行测试:
/home/ding/Downloads/jdk-17.0.3.1/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:/home/ding/Downloads/idea-IU-221.5921.22/lib/idea_rt.jar=39859:/home/ding/Downloads/idea-IU-221.5921.22/bin -Dfile.encoding=UTF-8 -classpath /home/ding/Downloads/idea-IU-221.5921.22/lib/idea_rt.jar:/home/ding/Downloads/idea-IU-221.5921.22/plugins/junit/lib/junit5-rt.jar:/home/ding/Downloads/idea-IU-221.5921.22/plugins/junit/lib/junit-rt.jar:/home/ding/IdeaProjects/test0822/target/test-classes:/home/ding/IdeaProjects/test0822/target/classes:/home/ding/.m2/repository/junit/junit/4.13.2/junit-4.13.2.jar:/home/ding/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/home/ding/.m2/repository/org/mybatis/mybatis/3.5.10/mybatis-3.5.10.jar:/home/ding/.m2/repository/com/ibm/db2/jcc/db2jcc/db2jcc4/db2jcc-db2jcc4.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 TestMybatis,test1
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1702143276.
==> Preparing: select name, value from table(db_get_cfg()) t1 where name in ('mon_deadlock', 'mon_locktimeout')
==> Parameters:
<== Columns: NAME, VALUE
<== Row: mon_locktimeout, NONE
<== Row: mon_deadlock, WITHOUT_HIST
<== Total: 2
==> Preparing: call sysproc.admin_cmd((select ' update db cfg using mon_locktimeout HISTORY mon_deadlock WITHOUT_HIST mon_lockwait HISTORY mon_lw_thresh 30000000 ' from sysibm.sysdummy1))
==> Parameters:
Closing JDBC Connection [com.ibm.db2.jcc.t4.b@6574a52c]
Returned connection 1702143276 to pool.
Process finished with exit code 0
再次查看DB CFG:
➜ ~ db2 get db cfg | grep -i "MON_LOCKTIMEOUT\|MON_DEADLOCK\|mon_lockwait\|mon_lw_thresh"
Lock timeout events (MON_LOCKTIMEOUT) = HISTORY
Deadlock events (MON_DEADLOCK) = WITHOUT_HIST
Lock wait events (MON_LOCKWAIT) = HISTORY
Lock wait event threshold (MON_LW_THRESH) = 30000000
若原先DB CFG如下:
➜ ~ db2 get db cfg | grep -i "MON_LOCKTIMEOUT\|MON_DEADLOCK\|mon_lockwait\|mon_lw_thresh"
Lock timeout events (MON_LOCKTIMEOUT) = HIST_AND_VALUES
Deadlock events (MON_DEADLOCK) = HISTORY
Lock wait events (MON_LOCKWAIT) = NONE
Lock wait event threshold (MON_LW_THRESH) = 5000000
运行测试:
......
==> Preparing: select name, value from table(db_get_cfg()) t1 where name in ('mon_deadlock', 'mon_locktimeout')
==> Parameters:
<== Columns: NAME, VALUE
<== Row: mon_locktimeout, HIST_AND_VALUES
<== Row: mon_deadlock, HISTORY
<== Total: 2
==> Preparing: call sysproc.admin_cmd((select ' update db cfg using mon_lockwait HISTORY mon_lw_thresh 30000000 ' from sysibm.sysdummy1))
==> Parameters:
......
再次查看DB CFG:
➜ ~ db2 get db cfg | grep -i "MON_LOCKTIMEOUT\|MON_DEADLOCK\|mon_lockwait\|mon_lw_thresh"
Lock timeout events (MON_LOCKTIMEOUT) = HIST_AND_VALUES
Deadlock events (MON_DEADLOCK) = HISTORY
Lock wait events (MON_LOCKWAIT) = HISTORY
Lock wait event threshold (MON_LW_THRESH) = 30000000
可见, MON_LOCKTIMEOUT
和 MON_DEADLOCK
已经足够高,不需修改。
若原先DB CFG里 MON_LOCKTIMEOUT
和 MON_DEADLOCK
其中之一已经足够高,则运行测试后,只会修改另一个,不再赘述。