这里我们来了解一下Activiti流程引擎的数据库配置。
我们从下面三个方面来讲解数据库的配置:
1、了解缺省时的默认配置,使用H2内存数据库;
2、配置JDBC属性,使用mybatis提供的连接池;
3、配置DataSource,可自选第三方实现(这里我配置了阿里的Druid的数据源);
首先我们先了解JDBC属性使用mybatis提供的连接池的相关配置:
基本配置 | 连接池配置 |
jdbcUrl | jdbcMaxActiveConnections(最大活跃数) |
jdbcDriver | jdbcMaxIdleConnections(最大允许空闲数) |
jdbcUsername | jdbcMaxCheckoutTime(最长的检验时间) |
jdbcPassword | jdbcMaxWaitTime(最长的等待时间) |
Activiti支持的数据库类型(配置databaseType):
接下来我们开始测试数据库配置。
在我们的maven工程的test文件中创建测试类,如下图:
ConfitDBTest.class 文件内容:
package com.jjf.activiti.config;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 数据库配置测试
*/
public class ConfigDBTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigDBTest.class);
@Test
public void testConfig1(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResourceDefault();
LOGGER.info("configuration = [{}]",configuration);
ProcessEngine processEngine = configuration.buildProcessEngine(); //获取引擎
LOGGER.info("获取流程引擎 [{}]",processEngine.getName());
processEngine.close(); //将创建的引擎关闭掉
}
}
我们进行测试:
我们可以看到这个过程加载的是 activiti.cfg.xml配置文件。首先先创建我们的表结构,如果不存在,则会执行三个脚本,分别是create.engine.sql、create.history.sql与create.identity.sql。等流程结束后再执行删除脚本,分别是drop.engine.sql、drop.history.sql与drop.identity.sql。
可以看到它默认配置的就是h2内存数据库。
接下来我们使用mybatis提供的连接池来配置mysql数据库。
这里我们mysql的数据库是activiti6,之前要使用mysql先创建一个新的activiti6数据库。
然后我们再回ConfigDBTest测试类测试是否成功使用mysql。但是报错,如下:
org.activiti.engine.ActivitiException: couldn't check if tables are already present using metadata:
### Error getting a new connection. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.activiti.engine.impl.db.DbSqlSession.isTablePresent(DbSqlSession.java:1051)
at org.activiti.engine.impl.db.DbSqlSession.isEngineTablePresent(DbSqlSession.java:991)
at org.activiti.engine.impl.db.DbSqlSession.dbSchemaCreate(DbSqlSession.java:847)
at org.activiti.engine.impl.db.DbSqlSession.performSchemaOperationsProcessEngineBuild(DbSqlSession.java:1312)
at org.activiti.engine.impl.SchemaOperationsProcessEngineBuild.execute(SchemaOperationsProcessEngineBuild.java:28)
at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:37)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:78)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42)
at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:63)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44)
at org.activiti.engine.impl.ProcessEngineImpl.(ProcessEngineImpl.java:81)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:665)
at com.jjf.activiti.config.ConfigDBTest.testConfig1(ConfigDBTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error getting a new connection. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getConnection(DefaultSqlSession.java:300)
at org.activiti.engine.impl.db.DbSqlSession.isTablePresent(DbSqlSession.java:1011)
... 37 more
Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
看到后面我们就知道了,因为我们没有在pom.xml中添加mysql的依赖。
添加依赖如下:
mysql
mysql-connector-java
5.1.38
com.alibaba
druid
1.0.13
但是再次执行还是会报一个异常,如下:
WARN: Establishing SSL connection without server's identity verification is not recommended.
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set.
For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
这个原因是是Mysql数据库的SSL连接问题,提示警告不建议使用没有带服务器身份验证的SSL连接,是在MYSQL5.5.45+, 5.6.26+ and 5.7.6+版本中才有的这个问题。我的版本正好是5.7.6+。解决办法如下:
1.在数据库连接的url中添加useSSL=false;
2.url中添加useSSL=true,并且提供服务器的验证证书。
然而我们这里只是做一个测试,没必要搞证书那么麻烦,在连接后添加一个useSSL=false即可。
我们连接后直接添加一个useSSL=false:
这里不仅添加了这个参数,我还添加了一个databaseSchemaUpdate的配置信息。这个是数据库的更新策略:
value | 说明 |
false | 启动时检查数据库版本,发生不匹配抛异常 |
true |
启动时自动检查并更新数据库表,不存在则会创建 |
create-drop | 启动时创建数据库表结构,结束时删除表结构 |
然后我们再次执行测试文件:
可以看到我们成功连接了mysql数据库。
这里我们重新复制创建一个cfg.xml文件,命名为activiti_druid.cfg.xml:
其中配置文件内容如下:
这个的测试函数不再是 ConfitDBTest.class 中的ConfigDBTest函数,新建一个测试函数如下:
@Test
public void testConfig2(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activiti_druid.cfg.xml"); //按照配置文件名配置流程引擎
LOGGER.info("configuration = [{}]",configuration);
ProcessEngine processEngine = configuration.buildProcessEngine(); //获取引擎
LOGGER.info("获取流程引擎 [{}]",processEngine.getName());
processEngine.close(); //将创建的引擎关闭掉
}
这里将配置文件设为我们上面创建的activiti_druid.cfg.xml配置文件。测试结果如下:
拓展:
流程引擎配置中其他属性: