場景
項目需要使用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的包里新增一個文件:
org\activiti\db\properties\
下新增Zenith.properties配置文件,主要作用是為不同數據庫配置分頁語法,GuassDB是支持類似Mysql的分頁的,所以直接使用Mysql的即可。內容如下:
limitAfter=LIMIT #{maxResults} OFFSET #{firstResult}