# Database Connections in module

**URL:** <https://forum.inductiveautomation.com/t/database-connections-in-module/68178>\
**Category:** Module Development\
**Tags:** vision, ignition81\
**Created:** [January 3, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/database-connections-in-module/68178 "2023-01-03T18:55:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NA\_NA](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@NA\_NA](https://forum.inductiveautomation.com/u/NA_NA)\
**Post date:** [January 3, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/database-connections-in-module/68178/1 "2023-01-03T18:55:07Z")

</div>

Hello,  
Trying to build a custom scripting module and have the scripting module make connections to one of many different databases already setup inside of Ignition. I also want to try to take advantage of the default project database.

IE if I run `system.mycustommodule.customscript(param1)` I want it to use the projects default database whereas if I want to run `system.mycustommodule.customscript(param1, db1)` I want it to use db1 as the database.

So far I understand that the code for this would be pretty different for both gateway and client/designer scopes (vision) which means that I am limited in what I can share between the two scopes codewise (in the common artifact).

I know that I need the following code to run the query but I am missing how I can get the default connections.

```auto
        SRConnect con = null;
        try{
            con = context.getDatasourceManager().getConnection("customdatasource");
            // data logic 
            } finally {
                DBUtilities.close(con);
        }

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 10, 2023, 5:22pm UTC](https://forum.inductiveautomation.com/t/database-connections-in-module/68178/2 "2023-01-10T17:22:31Z")

</div>

You can generally expect the [`ScriptContext`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.23/com/inductiveautomation/ignition/common/script/ScriptContext.html#defaultDatasource()) threadlocal to have the project's current datasource (if your script is being executed in a context which _has_ a default datasource).

> [@NA\_NA](#):
>
> So far I understand that the code for this would be pretty different for both gateway and client/designer scopes (vision) which means that I am limited in what I can share between the two scopes codewise (in the common artifact).

This is broadly true, yes. A common pattern in our scripting function implementations is to have the 'common' code do the argument parsing/handling, to ensure things are consistent and, if you're accepting keyword arguments, to "unpack" them to plain Java values. Then, from common, invoke an protected abstract `doSomethingImpl` method that can be filled in for each scope.
