jBPM Persistence and Transaction
jBPM allows the storage of certain information. jBPM persistence is a runtime persistence which persists the state of all our process instances and log audit information that is happening at runtime of the processes. The main purpose of persistence is to store process executions during wait states. This section describes the different types of persistence and how to configure them.
Runtime State Persistence
When a process is started, immediately its instance is created which represents the current execution state in that specific context. If something goes wrong during the execution of a process then it is necessary to store the process runtime state which helps to continue the execution of a process instance. For example, when we execute a process for sales order, one process instance is created for every sales request.
jBPM provides plug-in for different persistence strategies. The runtime state of an executing process can be made persistent. The persistence strategies allow restoring the state of execution of all running processes in case of unexpected failure.
To use persistence, we have to configure the process engine. This configuration will automatically store the runtime state into the database. We cannot trigger persistence itself. The process engine will take care of this when persistence is enabled. The engine will automatically reload process instances on demand.
Binary persistence mechanism is used to convert the process state instance information into a binary dataset. This mechanism is used to save or retrieve the process instance state from the database.
In Binary Persistence, two things happen when the process instance state is persisted-
The jBPM audit log stores the history log of execution information in a database using JPA. In many cases, it is useful to store information about the execution of process instances so that this information can be used later. For example, sometimes we need to-
The history log of execution information is created based on events. It is possible because the jBPM runtime engine provides an appropriate mechanism to listen to events. The necessary information can easily be extracted from these events and then persisted to a database.
The jBPM process engine supports JTA(Java Transaction API) transactions. It also supports local transactions by using Spring but does not support pure local transactions. In jBPM, it is necessary to provide transaction boundaries inside our application. If we do not give transaction boundaries inside our application, the engine will automatically invoke each executes method in a separate transaction. However, we can also specify the transaction boundaries itself. This allows us to combine multiple commands into one transaction.
To create user-defined transactions we need to register a transaction manager at the environment. The following sample code understand how to create transaction manager.
Persistence can be used by configuring the process engine. Persistence configuration requires adding the necessary dependencies, configuring a data source and creating the engine with persistence configured.
If we are using jBPM Eclipse plug-in, we have to make sure that the necessary JARs are added to our jBPM runtime directory. We can manually add the necessary dependencies in our project. To add dependencies manually, perform the following steps -
Manually configuring the engine to use persistence
Configure process engine, create a new knowledge session using JPAKnowledgeService based on knowledge base, a knowledge session configuration and an environment.