# How to get the project default datasource inside module

**URL:** <https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086>\
**Category:** Module Development\
**Created:** [May 18, 2018, 6:02am UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086 "2018-05-18T06:02:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkumar](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@rkumar](https://forum.inductiveautomation.com/u/rkumar)\
**Post date:** [May 18, 2018, 6:02am UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/1 "2018-05-18T06:02:37Z")

</div>

I am trying to get the default datasource of the project from inside the module to create SRConnection to the right database dynamically. Please suggest the right approach  
thanks.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [May 22, 2018, 5:50pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/2 "2018-05-22T17:50:22Z")

</div>

What scope? If you are in the client or designer, you can call

`context.getDefaultDatasourceName()`.

From the gateway, you would do something similar to the following:

```auto
r = context.getProjectManager().getProject("acme_inc",ApplicationScope.parseScope("A"),ProjectVersion.Published).getResourceOfType("","sr.global.props")

gp = XMLDeserializer.deserialize(context.createDeserializer(), r.getData(), None)

return gp.getDefaultDatasourceName()

```

---

<div class="post-metadata">

**Author:** ![rkumar](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@rkumar](https://forum.inductiveautomation.com/u/rkumar)\
**Post date:** [June 2, 2018, 7:49am UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/3 "2018-06-02T07:49:16Z")

</div>

Thanks Kyle, But we are trying to create a module per our requirements and uploading it on Ignition gateway and accessing methods inside that module from the designer.  
To make this module generic, we are looking for a way to get the default datasource of the project. In the above approach I will have to hard code the project name. This should also be coming by default so that the module methods can be used by any project for their default database name.  
I got stuck into something else hence could not respond you.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 2, 2018, 2:35pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/4 "2018-06-02T14:35:12Z")

</div>

The GatewayHook method getRPCHandler() is given a session object and a projectId. You must instantiate your RPC handler with those as constructor parameters and save them for later use in your RPC method implementations.  
The simple example in the Module SDK Documentation for [Designer-Gateway communications](https://docs.inductiveautomation.com/pages/viewpage.action?pageId=4065865) uses a “throwaway” RPC implementation object. In your case, you should cache these objects based on the session, and maintain any client- or designer-specific state within. Use a [WeakHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html) for the cache so you don’t leak memory of dead sessions.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [June 4, 2018, 3:29pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/5 "2018-06-04T15:29:43Z")

</div>

Well, you need a way to dictate the desired project. There are a few ways to do that, but I can not help without further details.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 4, 2018, 3:37pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/6 "2018-06-04T15:37:25Z")

</div>

> [@Kyle\_Chase](#):
>
> Well, you need a way to dictate the desired project

You replace "acme\_inc" in your example with the `projectId` from the `getRPCHandler()` method. ProjectManager has an overload of `getProject()` for that.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [June 4, 2018, 3:49pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/7 "2018-06-04T15:49:13Z")

</div>

Yeah, I was just referring to the fact that, depending on the scope, there are numerous ways to get the project name or id.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 4, 2018, 4:52pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/8 "2018-06-04T16:52:32Z")

</div>

> [@Kyle\_Chase](#):
>
> depending on the scope

You keep mentioning this, but Datasource and SRConnection are gateway-only classes.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [June 4, 2018, 4:55pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/9 "2018-06-04T16:55:53Z")

</div>

You can use the RPC handler to send the project id down as you stated. You could also create a module where you define what project scope to run events in, from the gateway. Like I said, it all depends on what his module is doing. Also, projects exist in the gateway scope as well, so an RPC would not be needed.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 4, 2018, 5:00pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/10 "2018-06-04T17:00:23Z")

</div>

Kumar’s first follow-up answers this – he’s trying to get the right project for a designer session. To then use gateway classes. Which can only mean resolving the project on the gateway side of an RPC. (-:

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [June 4, 2018, 5:03pm UTC](https://forum.inductiveautomation.com/t/how-to-get-the-project-default-datasource-inside-module/18086/11 "2018-06-04T17:03:57Z")

</div>

Ah I missed that part. RPC handler it is!!
