• 使用Mybatis调整Db2的cfg


    环境

    • Ubuntu 22.04
    • IntelliJ IDEA 2022.1.3
    • JDK 17.0.3.1
    • MyBatis 3.5.10

    需求

    调整如下Db2 CFG参数:

    • mon_locktimeoutHISTORY
    • mon_deadlockWITHOUT_HIST
    • mon_lockwaitHISTORY
    • mon_lw_thresh30000000

    要求:若 mon_locktimeoutmon_deadlock 本身的设置已经高于 HISTORYWITHOUT_HIST ,则无需修改这两个参数。

    对于这几参数的说明,详见Db2的Knowledge Center(例如: https://www.ibm.com/docs/en/db2/11.5?topic=parameters-mon-locktimeout-monitoring-lock-timeout )。

    • mon_locktimeout :默认值是 NONE ,可选值为 NONEWITHOUT_HISTHISTORYHIST_AND_VALUES
    • mon_deadlock :默认值是 WITHOUT_HIST ,可选值为 NONEWITHOUT_HISTHISTORYHIST_AND_VALUES
    • mon_lockwait :默认值是 NONE ,可选值为 NONEWITHOUT_HISTHISTORYHIST_AND_VALUES
    • mon_lw_thresh :默认值是 5000000 ,可选值为 1000MAX_INT (单位是微秒)

    要点

    获取DB CFG设置

    select name, value from table(db_get_cfg()) t1
    
    • 1

    更新DB CFG设置

    call sysproc.admin_cmd((select '
    	update db cfg using
    	 ,
    	 
    	......
    	' from sysibm.sysdummy1))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    可以一次性更新多个参数。

    动态生成SQL语句

    xxxxxx
    <if test="xxx">xxxxxxif>
    xxxxxx
    
    • 1
    • 2
    • 3

    test 结果为true时,SQL语句里就会包含对应的 xxxxxx

    打印SQL语句

    为了确认 if 动态生成的SQL语句是否有误,可以在运行SQL之前,打印出SQL语句,方便检查。在 mybatis-config.xml 里添加如下配置:

        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
        settings>
    
    • 1
    • 2
    • 3

    代码

    代码结构如下:

    在这里插入图片描述

    完整代码如下:

    • 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);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 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();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    运行

    场景1

    首先查看一下相关的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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行测试:

    /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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    再次查看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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    场景2

    若原先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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行测试:

    ......
    ==>  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: 
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    再次查看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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    可见, MON_LOCKTIMEOUTMON_DEADLOCK 已经足够高,不需修改。

    场景3

    若原先DB CFG里 MON_LOCKTIMEOUTMON_DEADLOCK 其中之一已经足够高,则运行测试后,只会修改另一个,不再赘述。

  • 相关阅读:
    Linux的一些知识(7)
    开发工程师必备————【Day12】MySQL数据库基础操作
    程序员天天 CURD,怎么才能成长,职业发展的思考(2)
    独立站运营优化方法
    邯郸百亿斤粮食生产 国稻种芯·中国水稻节:河北大市粮食经
    【JAVA】HTTP协议
    政安晨:【Keras机器学习示例演绎】(十六)—— 用于图像分类的混合增强
    vue3(element-plus)+多语言切换实现
    官网删除「儿童性虐待内容检测方案」,但苹果仍未放弃该计划
    C++学习笔记——链表基础算法
  • 原文地址:https://blog.csdn.net/duke_ding2/article/details/126492697