flowable更换数据源与连接池

来源:互联网 发布:下载单机游戏的软件 编辑:程序博客网 时间:2024/06/10 23:01

Spring Boot的约定大于配置。默认情况下,如果classpath中只有H2,就会创建内存数据库,并传递给Flowable流程引擎配置。

可以简单地通过提供Datasource bean来覆盖默认配置,来更换数据源。我们在这里使用DataSourceBuilder类,这是Spring Boot的辅助类。如果classpath中有Tomcat, HikariCP 或者 Commons DBCP,就会(按照这个顺序,先是Tomcat)选择一个(作为连接池)。例如,要切换到MySQL数据库:

@Beanpublic DataSource database() {    return DataSourceBuilder.create()        .url("jdbc:mysql://127.0.0.1:3306/flowable-spring-boot?characterEncoding=UTF-8")        .username("flowable")        .password("flowable")        .driverClassName("com.mysql.jdbc.Driver")        .build();}

从Maven依赖中移除H2,并为classpath添加MySQL驱动与Tomcat连接池:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.34</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId><version>8.0.15</version></dependency>

应用这次启动后,可以看到使用了MySQL作为数据库(也使用了Tomcat连接池框架):

org.flowable.engine.impl.db.DbSqlSession   : performing create on engine with resource org/flowable/db/create/flowable.mysql.create.engine.sqlorg.flowable.engine.impl.db.DbSqlSession   : performing create on history with resource org/flowable/db/create/flowable.mysql.create.history.sqlorg.flowable.engine.impl.db.DbSqlSession   : performing create on identity with resource org/flowable/db/create/flowable.mysql.create.identity.sql

多次重启应用,会发现任务的数量增加了(H2内存数据库在关闭后会丢失,而MySQL不会)。