解决SpringBatch/Cloud Task的SafeMode下的报错问题

当DBA开启Safe mode时,Spring Batch/Cloud Task的JOB_SEQ或TASK_SEQ表无法进行无索引条件的更新操作。本文提出通过替换Spring JDBC的MySQLMaxValueIncrementer,创建SafeModeMysqlMaxValueIncreamenter类,修改SQL语句并确保字段为主键,从而避免Safe Mode检查。详细解决方案和使用方法可参考slankka/spring-batch-safemode-patch。
摘要由CSDN通过智能技术生成

问题描述

一般公司都有DBA,DBA极有可能开启了Safe mode,也就是不支持不带索引条件过滤的update操作。
而Spring Batch /Cloud Task就有一张表 JOB_SEQ或者 TASK_SEQ的表,只有一条数据,也无法完成update操作。

Could not increment ID for BATCH_JOB_SEQ sequence table; nested exception is java.sql.SQLException:
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

解决方案

如果是DBA提供的数据库,那么无论怎么换数据库都无法解决。除非自己提供一个开发者自建的私库(不在DBA管理中的)。
更好的解决方案是,替换Spring JDBC的MySQLMaxValueIncrementer

这个类由 DefaultDataFieldMaxValueIncrementerFactory 创建:

//DefaultDataFieldMaxValueIncrementerFactory.java
@Override
    public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) {
        DatabaseType databaseType = DatabaseType.valueOf(incrementerType.toUpperCase());

        if (databaseType == DB2 || databaseType == DB2AS400) {
            return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName);
        }
        else if (databaseType == DB2ZOS) {
            return new DB2MainframeSequenceMaxValueIncrementer(dataSource, incrementerName);
        }
        else if (databaseType == DERBY) {
            return new DerbyMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
        }
        else if (databaseType == HSQL) {
            return new HsqlMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
        }
        else if (d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值