This tutorial discusses how to configure jBPM to use MySQL as database to store process data.
jBPM out of the box uses H2 database to store the state of processes using a file located under $JBOSS_HOME/standalone/data/jbpm-db. In order to switch to another database, you can use any of the following scripts which are available in $JBOSS_HOME/bin.
In order to switch to MySQL: you can use the jbpm-mysql-config.cli script. Before running the script, we recommend changing the Database password, as the latest versions of MySQL require to use a complex password. For example, set it to “jbpm7SecurePassword$”:
xa-data-source add --name=jBPMXADS --jndi-name="java:jboss/datasources/jBPMDS" --driver-name=mysql --user-name=jbpm --password=jbpm7SecurePassword$ --xa-datasource-class=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource --xa-datasource-properties=[ServerName=localhost, PortNumber=3306, DatabaseName=jbpm] --background-validation=true --background-validation-millis=120000
Now you can start the script as follows:
$ ./jboss-cli.sh --file=jbpm-mysql-config.cli (Unix / Linux)
The above commands needs to be executed when the server is stopped as an embedded server process will be used to update the configuration.
These scripts assume that database is installed and some default configuration is present:
- host → localhost
- port → 3306
- database name → jbpm
- user name → jbpm
- password →jbpm7SecurePassword$
Changing jBPM to MySQL
As an example, we will show how to change the default database to MySQL. Start MySQL server:
sudo systemctl start mysqld
Check from the status that the server is running:
sudo systemctl status mysqld ◠mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres> Active: active (running) since Fri 2021-06-18 08:51:08 CEST; 41min ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 1134 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0> Main PID: 1300 (mysqld) Status: "Server is operational" Tasks: 38 (limit: 4915) Memory: 515.6M CGroup: /system.slice/mysqld.service └─1300 /usr/sbin/mysqld
Then, run the jbpm-postgres-config.cli
$ ./jboss-cli.sh --file=jbpm-mysql
-config.cli (Unix / Linux)
Now start jBPM:
$ ./standalone.sh
As you can see from the XML configuration, the datasource named “jBPMXADS” has been added:
<subsystem xmlns="urn:jboss:domain:datasources:5.0"> <datasources> <xa-datasource jndi-name="java:jboss/datasources/jBPMDS" pool-name="jBPMXADS"> <xa-datasource-property name="ServerName"> localhost </xa-datasource-property> <xa-datasource-property name="PortNumber"> 3306 </xa-datasource-property> <xa-datasource-property name="DatabaseName"> jbpm </xa-datasource-property> <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> <driver>mysql</driver> <security> <user-name>jbpm</user-name> <password>jbpm7SecurePassword$</password> </security> <validation> <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/> <validate-on-match>true</validate-on-match> <background-validation>true</background-validation> <background-validation-millis>120000</background-validation-millis> <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/> </validation> </xa-datasource> <drivers> <driver name="mysql" module="com.mysql"> <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> </driver> </drivers> </datasources>
You can also log into MySQL and check that tables have been created:
mysql> use jbpm; Database changed mysql> show tables; +--------------------------------+ | Tables_in_jbpm | +--------------------------------+ | ATTACHMENT_ID_SEQ | | AUDIT_ID_SEQ | | Attachment | | AuditTaskImpl | | BAMTaskSummary | | BAM_TASK_ID_SEQ | | BOOLEANEXPR_ID_SEQ | | BooleanExpression | | CASE_FILE_DATA_LOG_ID_SEQ | | CASE_ID_INFO_ID_SEQ | | CASE_ROLE_ASSIGN_LOG_ID_SEQ | | COMMENT_ID_SEQ | | CONTENT_ID_SEQ | | CONTEXT_MAPPING_INFO_ID_SEQ | | CORRELATION_KEY_ID_SEQ | | CORRELATION_PROP_ID_SEQ | | CaseFileDataLog | | CaseIdInfo | | CaseRoleAssignmentLog | | Content | | ContextMappingInfo | | CorrelationKeyInfo | | CorrelationPropertyInfo | | DEADLINE_ID_SEQ | | DEPLOY_STORE_ID_SEQ | | Deadline | | Delegation_delegates | | DeploymentStore | | EMAILNOTIFHEAD_ID_SEQ | | ERROR_INFO_ID_SEQ | | ESCALATION_ID_SEQ | | EXEC_ERROR_INFO_ID_SEQ | | ErrorInfo | | Escalation | | EventTypes | | ExecutionErrorInfo | | I18NTEXT_ID_SEQ | | I18NText | | NODE_INST_LOG_ID_SEQ | | NOTIFICATION_ID_SEQ | | NodeInstanceLog | | Notification | | Notification_BAs | | Notification_Recipients | | Notification_email_header | | OrganizationalEntity | | PROCESS_INSTANCE_INFO_ID_SEQ | | PROC_INST_LOG_ID_SEQ | | PeopleAssignments_BAs | | PeopleAssignments_ExclOwners | | PeopleAssignments_PotOwners | | PeopleAssignments_Recipients | | PeopleAssignments_Stakeholders | | ProcessInstanceInfo | | ProcessInstanceLog | | QUERY_DEF_ID_SEQ | | QueryDefinitionStore | | REASSIGNMENT_ID_SEQ | | REQUEST_INFO_ID_SEQ | | Reassignment | | Reassignment_potentialOwners | | RequestInfo | | SESSIONINFO_ID_SEQ | | SessionInfo | | TASK_DEF_ID_SEQ | | TASK_EVENT_ID_SEQ | | TASK_ID_SEQ | | TASK_VAR_ID_SEQ | | Task | | TaskDef | | TaskEvent | | TaskVariableImpl | | VAR_INST_LOG_ID_SEQ | | VariableInstanceLog | | WORKITEMINFO_ID_SEQ | | WorkItemInfo | | email_header | | task_comment | +--------------------------------+ 78 rows in set (0.01 sec)
Configuring Applications to use jBPM with MySQL
Let’s see how to configure jBPM embedded in your applications. When using Spring Boot it’s pretty easy. What you need is to add the JDBC driver in your configuration:
<dependency> <groupId>org.kie</groupId> <artifactId>kie-server-spring-boot-starter</artifactId> <version>${version.org.kie}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
Then, you can configure the Datasource connectivity in your application.properties
server.address=0.0.0.0 server.port=8090 cxf.path=/rest #kie server config kieserver.serverId=business-application-service kieserver.serverName=business-application-service kieserver.location=http://localhost:8090/rest/server #kie server capabilities kieserver.drools.enabled=true kieserver.dmn.enabled=true kieserver.jbpm.enabled=true kieserver.jbpmui.enabled=true kieserver.casemgmt.enabled=true #jbpm configuration jbpm.executor.enabled=false #jbpm.executor.retries=5 #jbpm.executor.interval=3 #jbpm.executor.threadPoolSize=1 #jbpm.executor.timeUnit=SECONDS #data source configuration spring.datasource.username=jbpm spring.datasource.password=jbpm7SecurePassword$ spring.datasource.url=jdbc:mysql://localhost:3306/jbpm?&serverTimezone=UTC spring.datasource.driver-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource #hibernate configuration spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.show_sql=false spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #transaction manager configuration spring.jta.narayana.transaction-manager-id=1 #banner spring.banner.location=classpath:banner.txt narayana.transaction-manager-id=1 narayana.default-timeout=120 narayana.dbcp.enabled=true
Source code for this application: https://github.com/fmarchioni/mastertheboss/tree/master/jbpm/business-application-mysql