Querying a database from within a groovy script

Hi, I’m new to Camunda, just trying out the community edition at the moment. I’m running it as a shared engine behind tomcat and have configured MSSQL as my database. I’ve started experimenting with groovy script tasks, accessing another MSSQL database (not the one I’m using for Camunda) to run queries and stored procedures. But to do it, I am creating a connection string (complete with username and password) within the script. This obviously isn’t a secure way to handle data sources… So my question is, how should I be accessing external databases from within a groovy script task? Is there a recommended way to store the connection strings and credentials where I can use them from a script? I searched this forum, but if the answer was here I was unable to recognize it as such.

Thanks,
Jason

You should be storing your connection as a shared datasource, and then access that existing data source. This will require you add some code into the engine. So you can create a “camunda plugin” or just create a component in the Camunda SpringBoot distro, and setup the sql datasource to your custom DB. Then in your groovy script you load that data source Sql(...) and execute your query.

Thanks Stephen! That’s essentially what I ended up doing. I ended up putting the datasource in the tomcat server.xml, since camunda is the only app I plan to run. I was able to grab the context, pull in the datasource, and run a query from a script, so I’m happy. :slight_smile: