Database Connection

Introduction

The Database Connection module makes connecting to databases much more reliable — and as a bonus it simplifies the process.

The module automatically handles database connections and re-connections, and improves performance by keeping the connection open between queries.

Using the Code [top]

  • Import the Database Connection channel from the Interfaceware Support repository
  • Decide your database connection options:
    • Option 1: persisted database connection that does not require connection close.
    • Option 2: new database connection that requires to initiate and close connection in side the Main function.
  • Decide database retry time period (default 16 minutes) in db2.lua module


This is the github code for the main module:

How it works [top]

The db.Connect module handles database connections to a database and re-connections automatically. It does this by keeping this database connection object open in the background whenever the channel is running. This connection object is automatically created by the first query the channel runs (e.g. db.Connect.query or db.Connect.Execute) and thereafter whenever a query is run the connection is checked and re-opened if necessary.

This approach improves performance because you no longer need to open a connection each time you run a query. It also simplifies your code as you no longer need to use the db.connect{} command to create a database connection object, as this is now handled behind the scenes.

The db.Connect module contains equivalents for most of the familiar builtin “conn:xxx{}” database functions — with a few exceptions such as conn:check(). You also never need to use the db.connect{} function (this is because connections/re-connections are handled automatically).

The sample code in main() is very simple: It queries the patient table, inserts a new row and then deletes the inserted row:

More information [top]

Tagged:

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.