Activiti6.0流程引擎学习——(14)Activiti流程引擎的数据库配置

这里我们来了解一下Activiti流程引擎的数据库配置。

我们从下面三个方面来讲解数据库的配置:

1、了解缺省时的默认配置,使用H2内存数据库;

2、配置JDBC属性,使用mybatis提供的连接池;

3、配置DataSource,可自选第三方实现(这里我配置了阿里的Druid的数据源);

首先我们先了解JDBC属性使用mybatis提供的连接池的相关配置:

基本配置连接池配置
jdbcUrljdbcMaxActiveConnections(最大活跃数)
jdbcDriverjdbcMaxIdleConnections(最大允许空闲数)
jdbcUsernamejdbcMaxCheckoutTime(最长的检验时间)
jdbcPasswordjdbcMaxWaitTime(最长的等待时间)

 

 

 

 

 

Activiti支持的数据库类型(配置databaseType):

接下来我们开始测试数据库配置。

1、创建ConfigDBTest

在我们的maven工程的test文件中创建测试类,如下图:

2、测试缺省时默认数据库配置

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.sqlcreate.identity.sql。等流程结束后再执行删除脚本,分别是drop.engine.sql、drop.history.sqldrop.identity.sql

3、查看activiti.cfg.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
    <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000;MVCC=TRUE" />
    <property name="jdbcDriver" value="org.h2.Driver" />
    <property name="jdbcUsername" value="sa" />
    <property name="jdbcPassword" value="" />
  </bean>     <!-- 默认数据库的配置  h2数据库 -->


</beans>

可以看到它默认配置的就是h2内存数据库。

接下来我们使用mybatis提供的连接池来配置mysql数据库。

4、修改activiti.cfg.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti6" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="0425" />
    </bean>   <!-- Mysql数据库配置信息 -->

</beans>

这里我们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.<init>(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的依赖。

5、在pom.xml文件中添加mysql依赖

添加依赖如下:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.38</version>
</dependency>

<dependency>
	<groupId>com.alibaba</groupId>     <!-- 阿里Druid数据源配置,为监控而生的数据库连接池 -->
	<artifactId>druid</artifactId>
	<version>1.0.13</version>
</dependency>

但是再次执行还是会报一个异常,如下:

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即可。

6、再次修改activiti.cfg.xml配置文件

我们连接后直接添加一个useSSL=false:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti6?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="0425" />
        <property name="databaseSchemaUpdate" value="create-drop"/>     <!-- activiti6中不许有任何的表,所以使用这个参数create-drop 测试时候创建表然后删除表,还有true,false -->
    </bean>   <!-- Mysql数据库配置信息 -->

</beans>

这里不仅添加了这个参数,我还添加了一个databaseSchemaUpdate的配置信息。这个是数据库的更新策略:

value说明
false启动时检查数据库版本,发生不匹配抛异常

true

启动时自动检查并更新数据库表,不存在则会创建
create-drop启动时创建数据库表结构,结束时删除表结构

 

 

 

 

然后我们再次执行测试文件:

可以看到我们成功连接了mysql数据库。

7、创建阿里Druid的数据源配置

这里我们重新复制创建一个cfg.xml文件,命名为activiti_druid.cfg.xml:

其中配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        <property name="databaseSchemaUpdate" value="true"/>
        <property name="dataSource" ref="dataSource"/>    <!-- 配置DataSource,按照我们下面写的Druid数据源配置 -->
    </bean>   <!-- Mysql数据库配置信息 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/activiti6?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="username" value="root" />
        <property name="password" value="0425" />
        <property name="initialSize" value="1"/>    <!-- 初始化连接数 -->
        <property name="maxActive" value="10"/>    <!-- 最大连接数 -->
        <property name="filters" value="stat,slf4j"/>      <!-- 监控 -->
    </bean>

</beans>

这个的测试函数不再是 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配置文件。测试结果如下:

 

拓展:

流程引擎配置中其他属性:

<property name="dbHistoryUsed" value="true"></property>   这个引擎是否需要的历史数据,默认是true
<property name="dbIdentityUsed" value="true"></property>  这个引擎是否需要的身份数据
<property name="databaseTablePrefix" value="A_"/>    每个创建的表名以 A_ 开头

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青山孤客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值