【小笔记】Activiti扩展数据库支持类型

场景

项目需要使用GaussDB,Activiti默认支持的数据库中不包含GaussDB,需要对其进行扩展。

分析

在其源码org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.getDefaultDatabaseTypeMappings()中,写明了支持的数据库的类型:

h2、hsql、mysql、oracle、postgres、mssql、db2

并在初始化时进行了初始化:

protected static Properties databaseTypeMappings = getDefaultDatabaseTypeMappings();

public void initDatabaseType() {
	...
	省略内容
	...
}

如果获取到的数据库类型不在支持列表中,则会抛错:

connection = this.dataSource.getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            String databaseProductName = databaseMetaData.getDatabaseProductName();
this.databaseType = databaseTypeMappings.getProperty(databaseProductName);
            if (this.databaseType == null) {
                throw new ActivitiException("couldn't deduct database type from database product name '" + databaseProductName + "'");
            }

查看它的实现类,发现有个Spring实现的:

在这里插入图片描述

所以继承这个SpringProcesEnineConfiguration,重写一下initDatabaseType(),将其注入为Bean,即可进行扩展。

实现

重写initDatabaseType()

public class SpringProcessEngineConfigurationWithGauss extends SpringProcessEngineConfiguration {
    private static Logger log = LoggerFactory.getLogger(SpringProcessEngineConfigurationWithGauss.class);

    private static Properties databaseTypeMappings = getDefaultDatabaseTypeMappings();

    public static Properties getDefaultDatabaseTypeMappings() {
        Properties databaseTypeMappings = new Properties();
        ...
        省略其他
		...
        databaseTypeMappings.setProperty("Zenith", "Zenith");
        return databaseTypeMappings;
    }

    @Override
    public void initDatabaseType() {
        Connection connection = null;

        try {
            connection = this.dataSource.getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            String databaseProductName = databaseMetaData.getDatabaseProductName();
            log.debug("database product name: '{}'", databaseProductName);
            this.databaseType = databaseTypeMappings.getProperty(databaseProductName);
            if (this.databaseType == null) {
                throw new ActivitiException("couldn't deduct database type from database product name '" + databaseProductName + "'");
            }

            log.debug("using database type: {}", this.databaseType);
            if ("mssql".equals(this.databaseType)) {
                this.maxNrOfStatementsInBulkInsert = this.DEFAULT_MAX_NR_OF_STATEMENTS_BULK_INSERT_SQL_SERVER;
            }
        } catch (SQLException var12) {
            log.error("Exception while initializing Database connection", var12);
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException var11) {
                log.error("Exception while closing the Database connection", var11);
            }

        }
    }
}

将其注入:

@Bean
    public SpringProcessEngineConfigurationWithGauss processEngineConfiguration() {
        SpringProcessEngineConfigurationWithGauss configuration = new SpringProcessEngineConfigurationWithGauss();
        ...
        省略其他配置
		...
        return configuration;

    }

其次需要在activiti的包里新增一个文件:

orgactivitidbproperties下新增Zenith.properties配置文件,主要作用是为不同数据库配置分页语法,GuassDB是支持类似Mysql的分页的,所以直接使用Mysql的即可。内容如下:

limitAfter=LIMIT #{maxResults} OFFSET #{firstResult}
原文地址:https://www.cnblogs.com/cnsec/p/13286592.html