爱玩科技网
您的当前位置:首页【小笔记】Activiti扩展数据库支持类型

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

来源:爱玩科技网
【⼩笔记】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\ 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: '{}'\

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: {}\ 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\ } finally { try {

if (connection != null) { connection.close(); }

} catch (SQLException var11) {

log.error(\"Exception while closing the Database connection\ } } }}

将其注⼊:

@Bean

public SpringProcessEngineConfigurationWithGauss processEngineConfiguration() {

SpringProcessEngineConfigurationWithGauss configuration = new SpringProcessEngineConfigurationWithGauss(); ...

省略其他配置 ...

return configuration; }

其次需要在activiti的包⾥新增⼀个⽂件:

org\\activiti\\db\\properties\\下新增Zenith.properties配置⽂件,主要作⽤是为不同数据库配置分页语法,GuassDB是⽀持类似Mysql的分页的,所以直接使⽤Mysql的即可。内容如下:limitAfter=LIMIT #{maxResults} OFFSET #{firstResult}

因篇幅问题不能全部显示,请点此查看更多更全内容