# Temporarily enable DB connection

**URL:** <https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414>\
**Category:** Ignition\
**Tags:** database\
**Created:** [December 3, 2019, 6:59pm UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414 "2019-12-03T18:59:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [December 3, 2019, 6:59pm UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414/1 "2019-12-03T18:59:39Z")

</div>

Hello community,

Recently a DBA asked me about a “live” connection to a database, and this was a connection I had set up on one of our gateways for testing purposes. Since I was not going to use it, I decided to disable it, which gave me an idea: I could enable the connection when needed, and disable it when I’m done; that way I don’t keep the connection alive for more than needed.

So I came up with the following code:

```auto
class Connection(object):
    def __init__ (self, db):
        self.db = db
        self.enabled = False

    def __enter__ (self):
        import time
        self.enabled = True
        system.db.setDatasourceEnabled(self.db, self.enabled)
        # Wait for the connection to be Valid.
        while self.status != "Valid":
            time.sleep(1)

    def __exit__ (self, type, value, traceback):
        self.enabled = False
        system.db.setDatasourceEnabled(self.db, self.enabled)

    @property
    def status(self):
        ci = system.db.getConnectionInfo(self.db)
        return ci.getValueAt(0, "Status")

db = "test"
conn = Connection(db)

with conn:
    data = system.db.runQuery(query="SELECT 1", database=db)
    print data.getValueAt(0, 0)

```

Using the `with` statement I don’t have to worry about enabling and disabling the connection every time I run a query against that connection.

It is a very rough approach, as I am sure there is room for improvement, but I wanted to share and ask if someone else has done something similar.

---

<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:** [December 3, 2019, 7:18pm UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414/2 "2019-12-03T19:18:05Z")

</div>

Other than the potential for clashes on that too-generic classname, looks pretty good. Consider limiting the number of loops in **enter** waiting for “Valid”–there might be reasons it cannot be achieved.

---

<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:** [December 3, 2019, 7:20pm UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414/3 "2019-12-03T19:20:41Z")

</div>

Also, consider maintaining a map of concurrent users in the gateway, keyed to client IDs perhaps, to prevent problems when one user exits just after a different user enters. You’d need a gateway message handler to support client-scoped usage.

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [December 3, 2019, 7:27pm UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414/4 "2019-12-03T19:27:55Z")

</div>

Thanks for the suggestions, @pturmel .

---

<div class="post-metadata">

**Author:** ![thecesrom.git](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@thecesrom.git](https://forum.inductiveautomation.com/u/thecesrom.git)\
**Post date:** [April 19, 2023, 12:50am UTC](https://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414/5 "2023-04-19T00:50:28Z")

</div>

📢 Announcing `incendium` v2023.4.0 which addresses this concern using `system.util.globals` introduced in Ignition 8.1.0 (see [docs](https://docs.inductiveautomation.com/display/DOC81/system.util.getGlobals)).

It took me a while to add that feature ([Add mechanism for handling concurrent DisposableConnection connections · Issue #12 · thecesrom/incendium · GitHub](https://github.com/thecesrom/incendium/issues/12)), but I'm pleased to announce it's been done.

To learn more about `incendium`'s `DisposableConnection`, head over to the wiki:

> **[incendium.db.DisposableConnection](https://github.com/thecesrom/incendium/wiki/incendium.db.DisposableConnection)**
>
> 📦 Package that extends and wraps Ignition scripting API - thecesrom/incendium

🚨 ⚠ Just consider that this will only work in Ignition 8.1. ⚠🚨

You may download incendium 2023.4.0 from:

- For the project....
  - Ignition Exchange (once it's approved) [[link](https://inductiveautomation.com/exchange/2104/overview)] or directly from GitHub releases [[link](https://github.com/thecesrom/incendium-project/releases/tag/v2023.4.0)]

- For the Python package
  - [https://pypi.org/project/incendium/](https://pypi.org/project/incendium/)

- For the Python stubs
  - [https://pypi.org/project/incendium-stubs/](https://pypi.org/project/incendium-stubs/)

Happy coding!
