Configuring Activiti Database

In this example we will learn how to configure Activiti to use another Relational Database. We will switch from the default H2 database to PostgreSQL.

You can configure a different database using two main strategies:

Programmatic configuration

Using a Programmatic configuration, you will define the ProcessEngineConfiguration and set JDBC parameters as follows:

ProcessEngineConfiguration config = (StandaloneProcessEngineConfiguration)ProcessEngineConfiguration
   	   .createStandaloneProcessEngineConfiguration();   
	    config.setJdbcDriver(JDBC_DRIVER);
	    config.setJdbcPassword(JDBC_PWD);
	    config.setJdbcUrl(JDBC_URL);
	    config.setJdbcUsername(JDBC_USERNAME);
	    config.setDatabaseType(DB_TYPE);
	    config.setDatabaseSchemaUpdate("true");
        ProcessEngine processEngine = config.buildProcessEngine();

Declarative configuration in activiti.cxf.xml

On the other hand, you can use the main Activiti configuration file and define the JDBC settings in the StandaloneProcessEngineConfiguration Bean as in the following example:

<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.StandaloneProcessEngineConfiguration">

		<property name="databaseType" value="postgres" />
		<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/activiti" />
		<property name="jdbcDriver" value="org.postgresql.Driver" />
		<property name="jdbcUsername" value="postgres" />
		<property name="jdbcPassword" value="postgres" />


		<!-- <property name="dataSource" ref="dataSource" /> -->

		<!-- Database configurations -->
		<property name="databaseSchemaUpdate" value="create-drop" />

		<!-- job executor configurations -->
		<property name="jobExecutorActivate" value="true" />

		<property name="createDiagramOnDeploy" value="false" />

	</bean>

</beans>

In terms of code, you will need to configure the RespositoryService to use the configuration provided by Activiti configuration as follows:

final RepositoryService repositoryService = processEngine.getRepositoryService();

Here is where you can place the activiti.cfg.xml in the DemoActiviti project that we have uploaded on github at: https://github.com/fmarchioni/mastertheboss/tree/master/activiti/standalone/DemoActiviti

DemoActiviti
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── mastertheboss
    │   │           ├── activiti
    │   │           │   └── DemoActiviti
    │   │           │       └── App.java
    │   │           └── task
    │   │               └── JavaService1.java
    │   └── resources
    │       ├── activiti.cfg.xml
    │       └── DemoProcess.bpmn

Adding the JDBC Driver in the pom.xml

Finally, whahever is your approach to configuring JDBC, you have to include the driver libraries in your application. If you are using Maven, then include the database JDBC driver in the pom.xml

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901-1.jdbc4</version>
</dependency>
Found the article helpful? if so please follow us on Socials