Connecting Jbpm With External dataSource / External Database / Mysql Database
Published: 2022-10-29 14:03:09

_Writing this Article For Reference and this might help others ._

I, am New to JBPM , if Anything Mentioned In the article is Wrong Please Suggest it in the comments .

**Reference video:**
In Order Use Execute-Sql-Work-Item-Handler: 
Please Follow this Video Guide From YouTube:
[jBPM ExecuteSqlWorkItemHandler | KIE TUTORIALS][1]
- - - - -
**Installing**
 - - - - - - -

>  `Please make sure that you have toggled the Execute-Sql in the Custom Tasks Administration section.
Steps to Follow :
1. Click on the gear icon in the nav bar, scroll down and open Custom Tasks Administration . 
2. Search for Execute-Sql and toggle it to the yes state.
If prompted for jndi name just add : java:jboss/datasources/<databasename>
Adding Dependencies 
In order to add external Data source,
 Navigate to your:
1. **Project> Settings >Deployments> Work Item handlers >** 
2. Add a new Work Item Handler
 

```

- Name: **ExecuteSQL**
- Value:
new org.jbpm.process.workitem.executesql.ExecuteSqlWorkItemHandler("java:jboss/datasources/<databasename>")

- 
Resolver type: MVEL
```
3. Navigate to Dependencies and add a dependency From [Here][2] .
 Or else 
4. GroupID : **org.jbpm.contrib** | Artifact ID : **execute-sql-workitem** | Version : **7.42.0.Final**
5. Save the Configuration and the Create the Business Process as required.`
- - - - -
Drivers Deployment
 - - - - - - - - -
1. Download the Zip from below link 
- [mysqljdbcdriver.zip][3] 
2. Extract the zip and copy the **mysql-connector-java-5.1.47-bin.jar** file.
> Navigate **JBPM Installation Folder \standalone\deployments** 
> paste the file there. 
3. Navigate to **JBPM Installation Folder \modules\com**
> - Create folder named: **Mysql**
> - Inside mysql folder create a folder named **main**.
 The main Folder Directory would look like:
**`JBPM Installation Folder \modules\com\mysql\main`**
4. Navigate to **JBPM Installation Folder \bin\drivers**
> - Copy **mysql-connector-java.jar** file. 
> - Repeat the Steps **2** and **3** 
> - Navigate to **JBPM Installation Folder \modules\com\mysql\main** Paste the file.
> - Create a .xml file named **module.xml**.
> - The Contents of the module.xml file is:
<?xml version='1.0' encoding='UTF-8'?>
 <module xmlns="urn:jboss:module:1.1" name="com.mysql">
 <resources>
 <resource-root path="mysql-connector-java-5.1.47.jar"/>
 </resources>
 <dependencies>
 <module name="javax.api"/>
 <module name="javax.transaction.api"/>
 </dependencies>
 </module>
Adding The External Data Source
 - - - - - - - - - - - - - - - -
1. Now open Another Terminal in the bin and Execute the Following Command " `jboss-cli.bat -c` "
2. Configure Your DataSource From the Below Command :
> xa-data-source add - name=<datasource-Name>
 > - jndi-name="java:/jboss/datasources/<datasource-Name>" - user-name=jbpm - password=jbpm - driver-name=mysql-connector-java.jar - xa-datasource-class=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource - xa-datasource-properties=[{ServerName=localhost,PortNumber=3306,DatabaseName=<databasename>}]
**replace <datasource-Name> & <databasename>with the with database name.and the other parameters with their respective values .*
3. Now You can Use the External DataSource .
Reference Links
For PostgreSql : https://karinavarela.me/2020/06/17/persisting-custom-data-configuring-external-persistence/
MySql : http://www.mastertheboss.com/bpm/jbpm6/configuring-jbpm-to-use-mysql-as-database/
[1]: https://www.youtube.com/watch?v=cafxHv0neSM
 [2]: https://mvnrepository.com/artifact/org.jbpm.contrib/execute-sql-workitem/7.42.0.Final
 [3]: https://dbschema.com/jdbc-drivers/MySqlJdbcDriver.zip