Can't connect sql sever via bpmn tasklistener

I have added this class in execution listener start class:
package ge.psda.camunda.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.impl.util.json.JSONArray;
import org.camunda.bpm.engine.impl.util.json.JSONObject;

public class ConnectToHoliDays implements JavaDelegate{
	public static Connection connObj;
	public static String JDBC_URL = "jdbc:sqlserver://localhost:1433;databaseName=tutorialDb;integratedSecurity=true";
    public static  String   startDate;
    public  static int  period;
    public static JSONObject json;
    public static JSONArray array;
	public static void getDbConnection() {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			connObj = DriverManager.getConnection(JDBC_URL);
			Statement stmtObj = connObj.createStatement();
			ResultSet resObj = stmtObj.executeQuery("SELECT startDate FROM testTable");
			while (resObj.next()) {
				startDate = resObj.getString("startDate");
				  json.put("startDate",startDate);
				  array.put(json);
			}
		} catch(Exception sqlException) {
			sqlException.printStackTrace();
		}
	}


	public void execute(DelegateExecution execution) throws Exception {
		execution.setVariable("array", array.toString());
	}
  
}

Can’t find and use array variable, what should i change to connect this database and export relative data to my camunda project?

Why do you use static fields and methods instead of local variables and instance methods when the values are mutable?

Hi @thorben thank you for your response, i use static variable because public static void getDbConnection() is static function , i have changed this but this didn’t have any effect on my code and on connection too

Okay. Please write a unit test that reproduces the problem and share it on github. You can adapt the unit testing available here: https://github.com/camunda/camunda-engine-unittest. Instead of using SQL server, use the built in H2 database.

@thorben here is git link:https://github.com/sally9961/camunda-test
in this test project i have added: sql server configuration because i don;t need embeded database i want to connect to real database beyond my task, and i also want to send mail to smtp,gmail.com but at this point i can’t conplete this task the first error which i see when i run mvn clean test is:

Bean ‘processEngineConfiguration’; nested exception is org.springframework.beans.factory.parsing.BeanDe
finitionParsingException: Configuration problem: Multiple ‘property’ definitions for property ‘history’

Offending resource: class path resource [camunda.cfg.xml]
Bean ‘processEngineConfiguration’
→ Property ‘history’
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemRepor
ter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParse
rDelegate.java:323)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElemen
t(BeanDefinitionParserDelegate.java:578)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElemen
t(BeanDefinitionParserDelegate.java:476)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElemen
t(BeanDefinitionParserDelegate.java:445)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinit
ion(DefaultBeanDefinitionDocumentReader.java:311)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElemen
t(DefaultBeanDefinitionDocumentReader.java:202)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitio
ns(DefaultBeanDefinitionDocumentReader.java:181)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefi
nitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefini
tions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBea
nDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanD
efinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDef
initionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDef
initionReader.java:302)
at org.camunda.bpm.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfiguration(Bea
nsConfigurationHelper.java:35)
at org.camunda.bpm.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfigurationFrom
Resource(BeansConfigurationHelper.java:50)
at org.camunda.bpm.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResour
ce(ProcessEngineConfiguration.java:303)
at org.camunda.bpm.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResour
ce(ProcessEngineConfiguration.java:299)
at org.camunda.bpm.engine.impl.test.TestHelper.getProcessEngine(TestHelper.java:428)
at org.camunda.bpm.engine.test.ProcessEngineRule.initializeProcessEngine(ProcessEngineRule.java
:172)
at org.camunda.bpm.engine.test.ProcessEngineRule.apply(ProcessEngineRule.java:154)
at org.junit.rules.RunRules.applyAll(RunRules.java:26)
at org.junit.rules.RunRules.(RunRules.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.withTestRules(BlockJUnit4ClassRunner.java:379)
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:340)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:256)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:18
9)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:1
65)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration prob
lem: Multiple ‘property’ defini

Do you have any idea , what should i change or improve to complete this task(p.s this is test case i can upload whole project if it isn’t enough)

Check https://github.com/sally9961/camunda-test/blob/master/src/test/resources/camunda.cfg.xml for duplicate properties.

Hi @thorben thank you for your response, i have changed my camunda.cfgx.xml , here it is:

<?xml version="1.0" encoding="UTF-8"?>

<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;databaseName=tutorialDb" />
<property name="jdbcDriver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="jdbcUsername" value="sa" />
<property name="jdbcPassword" value="Oboba!23" />

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

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

<property name="history" value="full" />


<!-- <property name="customPostBPMNParseListeners">
  <list>
    <bean class="org.camunda.bpm.engine.impl.bpmn.parser.FoxFailedJobParseListener" />
  </list>
</property> -->

<!-- <property name="failedJobCommandFactory" ref="foxFailedJobCommandFactory" /> -->

<!--<property name="idGenerator" ref="uuidGenerator" />-->

<!-- engine plugins -->
<property name="processEnginePlugins">
  <list>
    <ref bean="connectProcessEnginePlugin" />
    <ref bean="spinProcessEnginePlugin" />
  </list>
</property>

but when i run this mvn clean test i got this error:
Failed tests: shouldExecuteProcess(org.camunda.bpm.unittest.SimpleTestCase): Expecting actual ProcessInstance {id=‘107’, processDefinitionId=‘P
rocess_2:1:103’, businessKey=‘null’} to be ended, but it is not!. (Please make sure you have set the history service of the engine to at least ‘a
ctivity’ or a higher level before making use of this assertion!)

Here is my listener:

should i change lister type ?
what should i make to set the history service of the engine to at least ‘a
ctivity’ or a higher level ?

As i have seen through debug process it can’t end processInstance, in this case should i use this piece of coed or somehow make configuration which will help me end this task? ProcessEngine processEngine = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResourceDefault()
.setHistory(ProcessEngineConfiguration.HISTORY_FULL)
.buildProcessEngine();

Let’s have a look at what the test case does:

https://github.com/sally9961/camunda-test/blob/master/src/test/java/org/camunda/bpm/unittest/SimpleTestCase.java#L35-L48

So it starts a process instance and then completes one user task.

Now look at the process model. It contains a sequence of multiple user tasks, three in the shortest path. So it is not surprising that the process instance is not finished when you only complete the first task.

What you should do: Please try to create a minimal test case. This is important to study a problem in isolation, which makes it much easier to understand. In particular, you can follow these steps:

  • Remove all tasks except one user task
  • Add the task listener, in which you try to connect to the database, to that user task
  • Provide any variables that the task listener expects in the RuntimeService#startProcessInstanceByKey call

Cheers,
Thorben