Camunda如何适配国产数据库达梦

前言

camunda流程引擎官方支持的数据库有:MySQL 、MariaDB 、Oracle 、DB2 、PostgreSQL 、SQL Server、H2。对于其他类型的数据库如何支持,尤其是国产数据库的支持,下面以达梦数据库为例进行介绍。对于其他国产数据库如人大金仓、神州通用等也是同样的修改方法,只是需要注意一下对应数据库的方言即可。

一、引入达梦数据库驱动包

在maven工程的Resources目录下建lib目录,放入达梦数据库驱动包Dm7JdbcDriver17.jar,并配置maven为本地引用

dm.jdbc.driver.DmDriver
jdbc
system
${project.basedir}/src/main/resources/lib/Dm7JdbcDriver17.jar

二、配置达梦数据源

url: jdbc:dm://127.0.0.1:5236
username: oa
password: 12345678aA
driver-class-name: dm.jdbc.driver.DmDriver

三、修改camunda代码,支持达梦数据库

1. 修改ProcessEngineConfigurationImpl类的代码

在org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl类的getDefaultDatabaseTypeMappings方法里增加
databaseTypeMappings.setProperty(“DM DBMS”, “dm”);

protected static Properties getDefaultDatabaseTypeMappings() {
   
    Properties databaseTypeMappings = new Properties();
    databaseTypeMappings.setProperty("H2", "h2");
    databaseTypeMappings.setProperty(MY_SQL_PRODUCT_NAME, "mysql");
    databaseTypeMappings.setProperty(MARIA_DB_PRODUCT_NAME, "mariadb");
    databaseTypeMappings.setProperty("Oracle", "oracle");
    databaseTypeMappings.setProperty("PostgreSQL", "postgres");
    databaseTypeMappings.setProperty("Microsoft SQL Server", "mssql");
    databaseTypeMappings.setProperty("DB2", "db2");
    databaseTypeMappings.setProperty("DB2", "db2");
    databaseTypeMappings.setProperty("DB2/NT", "db2");
    databaseTypeMappings.setProperty("DB2/NT64", "db2");
    databaseTypeMappings.setProperty("DB2 UDP", "db2");
    databaseTypeMappings.setProperty("DB2/LINUX", "db2");
    databaseTypeMappings.setProperty("DB2/LINUX390", "db2");
    databaseTypeMappings.setProperty("DB2/LINUXX8664", "db2");
    databaseTypeMappings.setProperty("DB2/LINUXZ64", "db2");
    databaseTypeMappings.setProperty("DB2/400 SQL", "db2");
    databaseTypeMappings.setProperty("DB2/6000", "db2");
    databaseTypeMappings.setProperty("DB2 UDB iSeries", "db2");
    databaseTypeMappings.setProperty("DB2/AIX64", "db2");
    databaseTypeMappings.setProperty("DB2/HPUX", "db2");
    databaseTypeMappings.setProperty("DB2/HP64", "db2");
    databaseTypeMappings.setProperty("DB2/SUN", "db2");
    databaseTypeMappings.setProperty("DB2/SUN64", "db2");
    databaseTypeMappings.setProperty("DB2/PTX", "db2");
    databaseTypeMappings.setProperty("DB2/2", "db2");
    databaseTypeMappings.setProperty("DM DBMS", "dm"); //达梦
    return databaseTypeMappings;
  }

2. 修改DbSqlSessionFactory类代码

org.camunda.bpm.engine.impl.db.sql.DbSqlSessionFactory类里增加对达梦数据库的语法解析。
public static final String DMDBMS = “dm”;//达梦数据库
public static final String[] SUPPORTED_DATABASES = {MSSQL, DB2, ORACLE, H2, MYSQL, POSTGRES, MARIADB, DMDBMS};

public class DbSqlSessionFactory implements SessionFactory {
   

  public static final String MSSQL = "mssql";
  public static final String DB2 = "db2";
  public static final String ORACLE = "oracle";
  public static final String H2 = "h2";
  public static final String MYSQL = "mysql";
  public static final String POSTGRES = "postgres";
  public static final String MARIADB = "mariadb";
  public static final String DMDBMS = "dm";//达梦数据库
  public static final String[] SUPPORTED_DATABASES = {
   MSSQL, DB2, ORACLE, H2, MYSQL, POSTGRES, MARIADB, DMDBMS};

并在static方法里增加:

databaseSpecificLimitBeforeStatements.put(DMDBMS, "select * from ( select a.*, ROWNUM rnum from (");
    optimizeDatabaseSpecificLimitBeforeWithoutOffsetStatements.put(DMDBMS, "select * from ( select a.*, ROWNUM rnum from (");
    databaseSpecificLimitAfterStatements.put(DMDBMS, " ) a where ROWNUM < #{lastRow}) where rnum >= #{firstRow}");
    optimizeDatabaseSpecificLimitAfterWithoutOffsetStatements.put(DMDBMS, " ) a where ROWNUM <= #{maxResults})");
    databaseSpecificLimitBeforeWithoutOffsetStatements.put(DMDBMS, "");
    databaseSpecificLimitAfterWithoutOffsetStatements.put(DMDBMS, "AND ROWNUM <= #{maxResults}");
    databaseSpecificInnerLimitAfterStatements.put(DMDBMS, databaseSpecificLimitAfterStatements.get(DMDBMS));
    databaseSpecificLimitBetweenStatements.put(DMDBMS, "");
    databaseSpecificLimitBetweenFilterStatements.put(DMDBMS, "");
    databaseSpecificOrderByStatements.put(DMDBMS, defaultOrderBy);
    databaseSpecificLimitBeforeNativeQueryStatements.put(DMDBMS, "");
    databaseSpecificDistinct.put(DMDBMS, "distinct");

    databaseSpecificEscapeChar.put(DMDBMS, defaultEscapeChar);

    databaseSpecificDummyTable.put(DMDBMS, "FROM DUAL");
    databaseSpecificBitAnd1.put(DMDBMS, "BITAND(");
    databaseSpecificBitAnd2.put(DMDBMS, ",");
    databaseSpecificBitAnd3.put(DMDBMS, ")");
    databaseSpecificDatepart1.put(DMDBMS, "to_number(to_char(");
    databaseSpecificDatepart2.put(DMDBMS, ",");
    databaseSpecificDatepart3.put(DMDBMS, "))");

    databaseSpecificTrueConstant.put(DMDBMS, "1");
    databaseSpecificFalseConstant.put(DMDBMS, "0");
    databaseSpecificIfNull.put(DMDBMS, "NVL");

    databaseSpecificDaysComparator.put(DMDBMS, "${date} <= #{currentTimestamp} - ${days}");

    databaseSpecificCollationForCaseSensitivity.put(DMDBMS, "");

    addDatabaseSpecificStatement(DMDBMS, "selectHistoricProcessInstanceDurationReport", "selectHistoricProcessInstanceDurationReport_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricTaskInstanceDurationReport", "selectHistoricTaskInstanceDurationReport_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricTaskInstanceCountByTaskNameReport", "selectHistoricTaskInstanceCountByTaskNameReport_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectFilterByQueryCriteria", "selectFilterByQueryCriteria_oracleDb2");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricProcessInstanceIdsForCleanup", "selectHistoricProcessInstanceIdsForCleanup_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricDecisionInstanceIdsForCleanup", "selectHistoricDecisionInstanceIdsForCleanup_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricCaseInstanceIdsForCleanup", "selectHistoricCaseInstanceIdsForCleanup_oracle");
    addDatabaseSpecificStatement(DMDBMS, "selectHistoricBatchIdsForCleanup", "selectHistoricBatchIdsForCleanup_oracle");

    addDatabaseSpecificStatement(DMDBMS, "deleteAttachmentsByRemovalTime", "deleteAttachmentsByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteCommentsByRemovalTime", "deleteCommentsByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricActivityInstancesByRemovalTime", "deleteHistoricActivityInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricDecisionInputInstancesByRemovalTime", "deleteHistoricDecisionInputInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricDecisionInstancesByRemovalTime", "deleteHistoricDecisionInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricDecisionOutputInstancesByRemovalTime", "deleteHistoricDecisionOutputInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricDetailsByRemovalTime", "deleteHistoricDetailsByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteExternalTaskLogByRemovalTime", "deleteExternalTaskLogByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricIdentityLinkLogByRemovalTime", "deleteHistoricIdentityLinkLogByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricIncidentsByRemovalTime", "deleteHistoricIncidentsByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteJobLogByRemovalTime", "deleteJobLogByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricProcessInstancesByRemovalTime", "deleteHistoricProcessInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricTaskInstancesByRemovalTime", "deleteHistoricTaskInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricVariableInstancesByRemovalTime", "deleteHistoricVariableInstancesByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteUserOperationLogByRemovalTime", "deleteUserOperationLogByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteByteArraysByRemovalTime", "deleteByteArraysByRemovalTime_oracle");
    addDatabaseSpecificStatement(DMDBMS, "deleteHistoricBatchesByRemovalTime", "deleteHistoricBatchesByRemovalTime_oracle");

    constants = new HashMap<String, String>();
    constants.put("constant.event", "cast('event' as nvarchar2(255))");
    constants.put("constant.op_message", "NEW_VALUE_ || '_|_' || PROPERTY_");
    constants.put("constant_for_update", "for update");
    constants.put("constant.datepart.quarter", "'Q'");
    constants.put("constant.datepart.month", "'MM'");
    constants.put("constant.datepart.minute", "'MI'");
    constants.put("constant.null.startTime", "null START_TIME_");
    constants.put("constant.varchar.cast", "'${key}'");
    constants.put("constant.integer.cast", "NULL");
    constants.put("constant.null.reporter", "NULL AS REPORTER_");
    dbSpecificConstants.put(DMDBMS, constants);

四、修改数据库连接池

如果工程中使用的是阿里巴巴的druid连接池,可能会报如下错误:
Cause: java.lang.IllegalStateException: dbType not support : dm
需要把sql防火墙等功能关闭掉即解决该问题:
spring.datasource.filters=stat,wall,log4j
把这个配置里的wall去掉。

posted @ 2021-06-21 10:45  大龄码农有梦想  阅读(2973)  评论(1编辑  收藏  举报