Back to Contents


Database Connections

Summary:

See also: Transactions, Static SQL, Dynamic SQL, Result Sets, SQL Errors, Programs.


What is a database connection?

A Database Connection is a session of work, opened by the program to communicate with a specific database server, in order to execute SQL statements as a specific user.

The database user can be identified explicitly for each connection. Usually, the user is identified by a login and a password, or by using the authentication mechanism of the operating system (or even from a tier security system).

The database connection instructions can not be prepared as Dynamic SQL statements; you must use static SQL statements.

There are two kind of connection modes: unique-session and multi-session mode. When using the DATABASE and CLOSE DATABASE instructions, you are in unique-session mode. When using the CONNECT, SET CONNECTION and DISCONNECT instructions you are in multi-session mode. The modes are not compatible. It is strongly recommended that you choose the session mode and not mix both kinds of instructions.

In unique-session mode, you simply connect with the DATABASE instruction; that creates a current session. You disconnect from the current session with the CLOSE DATABASE instruction, or when another DATABASE instruction is executed, or when the program ends.

In multi-session mode, you open a session with the CONNECT instruction; that creates a current session. You can open other connections with subsequent CONNECT instructions. To switch to a specific session, use the SET CONNECTION instruction; this suspends other opened connections. Finally, you disconnect from the current, from a specific, or from all sessions with the DISCONNECT instruction. The end of the program disconnects all sessions automatically.

Once connected to a database server, you have a current database session. Any subsequent SQL statement is executed in the context of the current database session.

Warning:

  1. Before creating database connections, make sure you have properly installed and configured Genero BDL, using the correct database client environment and driver. For more information, see Installation and Setup.

Database Specification

The Database Specification identifies the data source (the database and database server) to which you want to connect. There are different ways to identify the data source, depending on the database interface architecture used by the runtime system.

Two database interface architectures are provided with the runtime system: the SDI (Standard Database Interface) and the ODI (Open Database Interface). These architectures manage database specification in different ways, because of database vendor connection rules and APIs constraints. For example, when you connect to Oracle, you cannot specify the database server as you do with Informix by using the 'dbname@dbserver' notation.

For portability reasons, it is not recommended that you use database vendor specific syntax in the database specification (like 'dbname@dbserver'). We recommend using a simple symbol instead, and configuring the connection parameters in external resource files. The ODI architecture allows this indirect database specification using the FGLPROFILE configuration file.

Database specification when using the Standard Database Interface (ix drivers) Informix only!

When using a native Informix database driver based on the Standard Database Interface architecture (i.e. runners linked with the libix* libraries), you can use the following syntax for the database specification:

Database Specification Description
dbname Connects to the database server identified by the Informix environment (for example, with the INFORMIXSERVER environment variable) and opens the database dbname.
@dbserver Connects to the database server identified by dbserver. This database specification does not select any database, the program is only connected to the database server.
dbname@dbserver Connects to the database server identified by dbserver and opens the database dbname.

Informix environment variables on Windows platforms

On Windows platforms, in a C console application, the Informix environment variables must be set with a call to ifx_putenv(). See INFORMIX ESQL/C documentation for more details about environment settings.

By default, the database driver automatically calls ifx_putenv() for all standard Informix environment variables such as INFORMIXDIR with the current value set in the console environment. You can specify additional environment variables to be set with the following FGLPROFILE entries:

dbi.stdifx.environment.count = max
dbi.stdifx.environment.index = "variable"

Database specification when using the Open Database Interface drivers

When using a database driver based on the Open Database Interface architecture (i.e. runners linked with the libifx*, libora*, libdb2* libraries), you can use either Direct Database Specification or Indirect Database Specification.

To connect to a database server, additional connection parameters are often required. Most database engines require a name to identify the server, a name to identify the database entity, a user name and a password. Some parameters might be omitted: For example, when using Oracle, the server can be implicitly defined by the ORACLE_SID environment variable if the program and the database server run on the same system. The ODI architecture allows you to define these parameters indirectly in the FGLPROFILE configuration file.

Direct Database Specification method

In the Direct Database Specification method, the database name used in the program is not used in FGLPROFILE to define the data source with a 'dbi.database.dbname.source' entry.  In this case, the database specification used in the connection instruction is used as the datasource.

This method is well known for standard Informix drivers, where you directly specify the database name and, if needed, the Informix server:

01 MAIN
02    DATABASE stores@orion
03    ...
04 END MAIN

In the next example, the database server is PostgreSQL. The string used in the connection instruction defines the PostgreSQL database (stock), the host (localhost), and the TCP service (5432) the postmaster is listening to. As PostgreSQL syntax is not allowed in standard BDL, a CHAR variable must be used:

01 MAIN
02    DEFINE db CHAR(50)
03    LET db = "stock@localhost:5432"
04    DATABASE db
05    ...
06 END MAIN

Indirect Database Specification method

In Indirect Database Specification method, the database specification used in the connection instruction corresponds to a 'dbi.database.dbname.source' entry defined in the FGLPROFILE configuration file. In this case, the database specification is considered a key to read the connection information from the configuration file:

Program:

01 MAIN
02    DATABASE stores
03    ...
04 END MAIN

FGLPROFILE file:

dbi.database.stores.source   = "stock@localhost:5432"
dbi.database.stores.driver   = "dbmpgs721"
dbi.database.stores.username = "postgres"

This technique is flexible: The database name in programs is a kind of alias used to define the real database. Using this method, you can develop your application with the database name "stores" and connect to the real database "stores1" in a production environment.

In FGLPROFILE, the entries starting with 'dbi.database' group information defining data sources by name:

dbi.database.dsname.source   = "value"
dbi.database.dsname.schema   = "value"
dbi.database.dsname.driver   = "value"
dbi.database.dsname.username = "value"
dbi.database.dsname.password = "value"

The "source" entry identifies the database vendor data source name. The following table shows some examples of this parameter for some well-known database vendors:

Database Server Value of "source" entry Description
Genero DB datasource ODBC Data Source
Generic ODBC datasource ODBC Data Source
IBM DB2 dsname DB2 Catalogued Database
INFORMIX dbname[@dbserver] Database Name @ Informix Server
MySQL dbname[@host[:port]]
or
dbname[@localhost~socket]
Database Name @ Host Name : TCP Port
or
Database Name @ Local host ~ Unix socket file
MySQL dbname[@host[:port]] Database Name @ Host Name : TCP Port
ORACLE tnsname Oracle TNS Service name
PostgreSQL dbname[@host[:port]] Database Name @ Host Name : TCP Port
SQL Server datasource ODBC Data Source
Sybase ASA dbname[@engine] Database Name @ Engine Name

If the "source" entry is defined with an empty value (""), the database interface connects to the default database server, which is usually the local server. If this entry is not present in FGLPROFILE, the Direct Database Specification method takes place (see above for more details).

The "schema" entry can be used to specify a database schema name to be selected when connection is established. This is useful for example with Oracle or DB2 databases, where tables are not visible to the user if  the table prefix is not specified (schemaname.tablename) and the current schema is not the schema of the table. 

The "driver" entry identifies the shared library or DLL to be used if the runner was created with the 'dynamic' database interface. The driver definition is mandatory when using runners created with dynamic database interface. Driver file names do not have to be specified with a file extension.

If you have a lot of databases, you can define a default driver with the Default Database Driver entry.

Database drivers shared libraries are located in FGLDIR/bin. You can create a database driver shared library by using the fglmksdl tool. Some drivers may not be available on a specific platform (for example if the target database client does not exist). Contact your support if you do not find the driver you are looking for.

The following table defines the database driver names according to the database client type:

Database Type Driver library prefix Example
Genero DB dbmads dbmads3x.so
Generic ODBC dbmodc dbmodc3x.dll
Informix dbmifx dbmifx9xx.so
IBM DB2 dbmdb2 dbmdb28x.so
MySQL dbmmys dbmmys41x.so
ORACLE dbmora dbmora92x.so
PostgreSQL dbmpgs dbmpgs80x.so
SQL Server dbmmsv dbmmsv80.dll
Sybase ASA dbmasa dbmasa8x.so

The "username" and "password" entries define the default database user, when the program uses the DATABASE instruction or the CONNECT TO instruction without the USER clause.

The "username" and "password" entries take effect based on the connection instruction as described in the following table:

Connection Instruction FGLPROFILE Effect
DATABASE dbname
   or
CONNECT TO "dbname"
No default user definition

 

No user specification is provided to the database server. Usually, the Operating System authentication takes place.
DATABASE dbname
   or
CONNECT TO "dbname"
With default user definition

 

The FGLPROFILE user name and password are used to connect to the database server.
CONNECT TO "dbname"
   USER "username"
   USING "password"
N/A

 

The user information of the CONNECT instruction are used to identify the actual user

Defining database vendor specific parameters in FGLPROFILE

Some database vendor specific connection parameters can be configured by using FGLPROFILE entries with the following syntax:

dbi.database.dsname.dbtype.param.[.subparam] = "value"

The table below describes all database vendor specific parameters supported:

Database Server Parameters
IBM DB2  
dbi.database.dsname.db2.prepare.deferred
True/False Boolean to enable/disable deferred prepare.
Example:
dbi.database.stores.db2.prepare.deferred=true
INFORMIX  
Warning: For Windows platforms only.
dbi.database.dsname.ifx.environment.count = max
dbi.database.dsname.ifx.environment.index = "variable"
By default, the database driver automatically calls ifx_putenv() for all standard Informix environment variables such as INFORMIXDIR with the current value set in the console environment. You can specify additional environment variables to be set with these entries.
See INFORMIX ESQL/C documentation for more details about environment settings.
Example:
dbi.database.stores.ifx.environment.count = 2
dbi.database.stores.ifx.environment.1 = "SOMEIFXENVVAR"
dbi.database.stores.ifx.environment.2 = "ANOTHERIFXENVVAR"
ORACLE  
dbi.database.dsname.ora.prefetch.rows
Maximum number of rows to be pre-fetched.
Example:
dbi.database.stores.ora.prefetch.rows=500
dbi.database.dsname.ora.prefetch.memory
Maximum buffer size pre-fetching (in bytes).
Example:
dbi.database.stores.ora.prefetch.memory=4096
SQL Server  
dbi.database.dsname.msv.logintime
Connection timeout (in seconds).
Example:
dbi.database.stores.msv.logintime=5
dbi.database.dsname.msv.prefetch.rows
Maximum number of rows to be pre-fetched.
Example:
dbi.database.stores.msv.prefetch.rows = 50
Usage:
You can use this parameter to increase performance. However, the bigger this parameter is, the more memory is used by each program. The default is 10 rows.
dbi.database.dsname.msv.logintime
Connection timeout (in seconds).
Example:
dbi.database.stores.msv.logintime=5
Sybase ASA  
dbi.database.dsname.asa.logintime
Connection timeout (in seconds).
Example:
dbi.database.stores.asa.logintime=10

Specifying connection parameters in the connection string

Although this is not recommended for abstract programming reasons, you can specify connection parameters in the string used by the connection instructions.

This behavior is enabled when you use a plus sign in the connection string:

dbname+property='value'[,...]

In this syntax, property can be one of the data source definition parameters (driver, source, username) described in Indirect Database Specification, plus the 'resource' property:

resource='resourcename'

The 'resource' property can be used to specify which 'dbi.database' entries have to be read from the FGLPROFILE configuration file. When this property is not used, the database interface reads dbi.database.resourcename entries, as when using the Data Source Configuration method. 

In the following example, driver, source and resource are specified in the connection string:

01 MAIN
02    DEFINE db CHAR(50)
03    LET db = "stores+driver='dbmora',source='orcl',resource='ora'"
04    DATABASE db
05    ...
06 END MAIN

Informix emulation parameters in FGLPROFILE

To simplify the migration process to other databases, the database interface and drivers can emulate some Informix-specific features like SERIAL columns and temporary tables; the drivers can also do some SQL syntax translation.

Warning: Avoid using Informix emulations; write portable SQL code instead as described in SQL Programming. Informix emulations are only provided to help you in the migration process. Disabling Informix emulation improves performance, because SQL statements do not have to be parsed to search for Informix-specific syntax.

Emulations can be controlled with FGLPROFILE parameters. You can disable all possible switches step- by- step, in order to test your programs for SQL compatibility.

Global switch to enable or disable Informix emulations:

dbi.database.dbname.ifxemul = { true | false }

Feature-specific switches:

The 'ifxemul.datatype' switches define if the specified data type must be converted to a native type (for example, when creating a table):

dbi.database.dbname.ifxemul.datatype.type = { true | false }

Here, type can be one of: char, varchar, datetime, decimal, momey, float, real, integer, smallint, serial, text, byte.

To control SERIAL generation type, you can use the following switch:

dbi.database.dbname.ifxemul.datatype.serial.emulation = { "native" | "regtable" | "trigseq" }

When using "native", the database driver creates a native sequence generator - it is fast, but not fully compatible with Informix SERIAL. When using "regtable", you must have the SERIALREG table created - it is slower than the "native" emulation, but compatible to Informix SERIAL. The serial emulation "trigseq", can be used by some database drivers, to use triggers with native sequence generators. See ODI guides for more details.

The 'temptables' switch can be used to control temporary table emulation:

dbi.database.dbname.ifxemul.temptables = { true | false }

The 'dblquotes' switch can be used to define whether double quoted strings must be converted to single quoted strings:

dbi.database.dbname.ifxemul.dblquotes = { true | false }

If this emulation is enabled, all double quoted strings are converted, including database object names.

The 'outers' switch can be used to control Informix OUTER specification:

dbi.database.dbname.ifxemul.outers = { true | false }

It is better to use standard ISO outer joins in your SQL statements.

The 'today' switch can be used to convert the TODAY keyword to a native expression returning the current date:

dbi.database.dbname.ifxemul.today = { true | false }

The 'current' switch can be used to convert the CURRENT X TO Y expressions to a native expression returning the current time:

dbi.database.dbname.ifxemul.current = { true | false }

The 'selectunique' switch can be used to convert the SELECT UNIQUE to SELECT DISTINCT:

dbi.database.dbname.ifxemul.selectunique = { true | false }

It is better to replace all UNIQUE keywords by DISTINCT.

The 'colsubs' switch can be used to control column sub-strings expressions (col[x,y]) to native sub-string expressions:

dbi.database.dbname.ifxemul.colsubs = { true | false }

The 'matches' switch can be used to define if MATCHES expressions must be converted to LIKE expressions:

dbi.database.dbname.ifxemul.matches = { true | false }

It is better to use the LIKE operator in your SQL statements.

The 'length' switch can be used to define if LENGTH function names have to be converted to the native equivalent:

dbi.database.dbname.ifxemul.length = { true | false }

The 'rowid' switch can be used to define if ROWID keywords have to be converted to the native equivalent:

dbi.database.dbname.ifxemul.rowid = { true | false }

It is better to use primary keys instead.

The 'listupdate' switch can be used to convert the UPDATE statements using non-ANSI syntax:

dbi.database.dbname.ifxemul.listupdate = { true | false }

Defining database vendor specific parameters in FGLPROFILE

Some database vendor specific connection parameters can be configured by using FGLPROFILE entries with the following syntax:

dbi.database.dsname.dbtype.param.[.subparam] = "value"

Global Configuration Parameters

Default Database Driver

With the following entry, you can define a default driver identifying the shared library or DLL to be used to connect to the database:

dbi.default.driver = "value"

Optimizing Static SQL

When you write a program using Static SQL, the runtime system prepares and executes the statements each time the program line is reached:

01 FOR i=1 TO 1000
02    INSERT INTO tab VALUES (i,"xxxx") 
03 END FOR

Normally you should optimize such code with Dynamic SQL. However, the execution of static SQL statements can be optimized with the following FGLPROFILE entry:

dbi.sql.static.optimization.cache.size = max

The value of this entry defines the maximum number of static SQL statements that can be prepared and kept in a cache, to avoid re-preparing the statement each time.

This entry is set to zero (no cache) by default. 

Warnings:

  1. This option is provided to optimize existing programs without touching the code. It is strongly recommended that you review the code and use Dynamic SQL to optimize the program.
  2. It is not recommended that you mix DDL statements with DML statements when this entry is used. For example, prepared statements using a table that is altered or dropped raise Informix SQL error -710 at execution and must be re-prepared. This is done automatically, but optimization is lost.
  3. When using non-Informix databases and emulated Informix DDL statements such as temporary table creation (CREATE TEMP TABLE), you will also get problems if the cache is enabled, because additional information is managed by the database-specific driver during the "prepare phase" (mapping of program tables to real table names are handled at that level). Since the "prepare phase" is done only once with cache, you can get invalid statement information when the following sequence of static SQL statements is executed: CREATE TEMP TABLE x + INSERT INTO x + DROP TABLE x + CREATE TEMP TABLE x + INSERT INTO x.
  4. We experienced problems with Informix ESQL 9.14, 9.16; do not set this entry if you are using these versions of ESQL.
  5. This option may be removed in future versions.

Informix Environment Variables on Windows

When using Informix on Windows platforms, a client program must set the Informix environment variables with the ifx_putenv() function, otherwise it cannot connect to the database engine.

If you have created an Informix runner (with the fglmkrun option -d ifx930 for example), the Informix variables are automatically set with the ifx_putenv() function.


Database Client Environment

To connect to a database server, the FGL programs must be executed in the correct database client environment. The database client software is usually included in the database server software, so you do not need to install it when your programs are executed on the same machine as the database server. However, you may need to install the database client software in three-tier configurations, where applications and database servers run on different systems.

This section describes basic configuration elements of the database client environment for some well known database servers.

Genero DB

  1. The ANTSHOME environment variable must define the Genero DB software installation path.
  2. The PATH environment variable must define the access path to database client programs.
  3. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $ANTSHOME/antsodbc.
  4. The ANTS ODBC client library 'libaodbc*' must be available.
  5. You can make a connection test with the ANTS antscmd tool.

IBM DB2 Universal Database

  1. The DB2DIR environment variable must define the DB2 software installation path.
  2. The PATH environment variable must define the access path to database client programs.
  3. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $DB2DIR/lib.
  4. The DB2 client library 'DB2DIR/lib/libdb2*' must be available.
  5. The remote server node and the remote database must be declared locally with the CATALOG db2 command.
  6. You can make a connection test with the IBM db2 tool.

IBM Informix Dynamic Server

  1. The INFORMIXDIR environment variable must define the Informix software installation path.
  2. The PATH environment variable must define the access path to database client programs.
  3. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $INFORMIXDIR/lib:$INFORMIXDIR/lib/esql.
  4. The Informix client libraries 'INFORMIXDIR/lib/*' must be available.
  5. The INFORMIXSERVER environment variable can be used to define the name of the database server.
  6. The sqlhost file must define the database server identified by INFORMIXSERVER.
  7. You can make a connection test with the Informix dbaccess tool.

MySQL

  1. The MYSQLDIR environment variable must define the MySQL software installation path.
  2. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $MYSQLDIR/lib.
  3. The PATH environment variable must define the access path to database client programs.
  4. You can make a connection test with the mysql tool.

Oracle

  1. The ORACLE_HOME environment variable must define the Oracle software installation path.
  2. The ORACLE_SID environment variable can be used to define the name of the local database instance.
  3. The PATH environment variable must define the access path to database client programs.
  4. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $ORACLE_HOME/lib.
  5. The Oracle client library 'ORACLE_HOME/lib/libclntsh*' must be available.
  6. The TNSNAMES.ORA file must define the database server identifiers for remote connections (the Oracle Listener must be started on the database server to allow remote connections).
  7. The SQLNET.ORA file must define network settings for remote connections.
  8. You can make a connection test with the Oracle sqlplus tool.

PostgreSQL

  1. The PGDIR environment variable must define the PostgreSQL software installation path.
  2. The PATH environment variable must define the access path to database client programs.
  3. On UNIX, LD_LIBRARY_PATH (or equivalent) must hold the path to $PGDIR/lib.
  4. The PostgreSQL client library 'PGDIR/lib/libpq*' must be available.
  5. On the database server, the pg_hba.conf file must define security policies.
  6. You can make a connection test with the PostgreSQL psql tool.

SQL Server

  1. The PATH environment variable must define the access path to database client programs (ODBC32.DLL).
  2. Check the SQL Server Client configuration with the Client Network Utility tool: Verify that the ANSI to OEM conversion corresponds to the execution of FGL applications in a CONSOLE environment.
  3. Make sure that ODBC data source is defined on database client and database server systems.
  4. You can make a connection test with the Microsoft Query Analyzer tool.

Sybase ASA

  1. The ASADIR environment variable must define the Sybase ASA software installation path.
  2. The PATH environment variable must define the access path to database client programs.
  3. Check the Sybase Client configuration.  
  4. You can make a connection test with the Sybase ISQL tool.

FGLSQLDEBUG

You can set the FGLSQLDEBUG environment variable to get debug information on SQL instructions. This variable can be set to an integer value from 0 to 10, according to the debugging details you want to see. The debug messages are sent to the standard error stream. If needed, you can redirect the standard error output into a file.

Unix (shell) example:

FGLSQLDEBUG=3
export FGLSQLDEBUG
fglrun myprog 2>sqldbg.txt


SQLCA

Purpose:

The SQLCA variable is a predefined record containing information about the execution of an SQL statement.

Syntax:

SQLCA

Definition:

DEFINE SQLCA RECORD
  SQLCODE INTEGER,
  SQLERRM CHAR(71),
  SQLERRP CHAR(8),
  SQLERRD ARRAY[6] OF INTEGER,
  SQLAWARN CHAR(8)
END RECORD

Notes:

  1. The SQLCA record is filled after any SQL statement execution.
  2. The "SQLCA" name stands for "SQL Communication Area".
  3. SQLCODE contains the SQL execution code ( 0 = OK, 100 = not row found, <0 = error ).
  4. SQLERRM contains the Informix error message parameter.
  5. SQLERRP is not used at this time.
  6. SQLERRD[1] is not used at this time.
  7. SQLERRD[2] contains the last SERIAL or the native SQL error code.
  8. SQLERRD[3] contains the number of rows processed in the last statement (server dependent).
  9. SQLERRD[4] contains the estimated CPU cost for the query (server dependent).
  10. SQLERRD[5] contains the offset of the error in the SQL statement text (server dependent).
  11. SQLERRD[6] contains the ROWID of the last row that was processed (server dependent).
  12. SQLAWARN contains the ANSI warning represented by a W character at a given position in the string.
  13. SQLAWARN[1] is set to W when any of the other warning characters have been set to W.
  14. SQLAWARN[2] is set to W when:
  15. SQLAWARN[3] is set to W when:
  16. SQLAWARN[4] is set to W when:
  17. SQLAWARN[5] is set to W when:
  18. SQLAWARN[6] is set to W when:
  19. SQLAWARN[7] is set to W when: 
  20. SQLAWARN[8] is not used at this time .

Warnings:

  1. SQLCA can be modified by hand, but this is not recommended because it may become read-only in a later release.

Example:

01 MAIN
02   WHENEVER ERROR CONTINUE
02   DATABASE stores
03   SELECT COUNT(*) FROM foo   -- Table should not exist!
04   DISPLAY SQLCA.SQLCODE, SQLCA.SQLERRD[2]
05 END MAIN

STATUS, SQLCA.SQLCODE, SQLSTATE and SQLERRMESSAGE

If an error occurs during an SQL statement execution, you can get the error description in the STATUS, SQLCA.SQLCODE, SQLSTATE and SQLERRMESSAGE built-in registers.

STATUS is the global language error code register, set for any kind of error (even non-SQL). When an SQL error occurs, the Informix SQL error code held by SQLCA.SQLCODE is copied into STATUS. The register SQLCA.SQLCODE returns the Informix error code. SQLSTATE returns the standard ANSI error code and SQLERRMESSAGE returns the database specific error message.

Warnings:

  1. SQLSTATE is an ANSI standard specification, but not all database servers support this register. For example, Oracle 8.x and 9.0 engines do not support this 

See also: STATUS, SQLCA, SQLSTATE, SQLERRMESSAGE, Exceptions.


Interrupting SQL Statements

Syntax:

OPTIONS SQL INTERRUPT { ON | OFF }

Notes:

  1. By default, SQL interruption is OFF.

Warnings:

  1. Not all database servers support SQL interruption.

Usage:

Typical BDL programs control the interrupt signals, by using the following instructions:

DEFER INTERRUPT
DEFER QUIT

If the database server supports SQL interruption, the runtime system can enable interruption of long SQL queries when you set the SQL INTERRUPT program option. When the program gets an interrupt signal from the system, the running SQL statement is stopped and the INT_FLAG global variable is set to TRUE.

Example:

01 MAIN
02    DATABASE stock
03    OPTIONS SQL INTERRUPT ON
04    LET INT_FLAG = FALSE
05    SELECT COUNT(*) FROM items WHERE items_value > 100
06    IF INT_FLAG THEN
07       DISPLAY "Query was interrupted by user"
08    END IF
09 END MAIN

DATABASE

Purpose:

Opens a new database connection in unique-session mode.

Syntax:

DATABASE { dbname[@dbserver] | variable | string } [EXCLUSIVE]

Notes:

  1. dbname identifies the database name.
  2. dbserver identifies the Informix database server (INFORMIXSERVER). Informix only!
  3. variable can be any character string defined variable containing the database specification.
  4. string can be a string literal containing the database specification.
  5. The EXCLUSIVE keyword can be used to open an Informix database in exclusive mode to prevent access by anyone but the current user. Informix only!
  6. If a current connection exists, it is automatically closed before connecting to the new database. 
  7. If the connection could not be established, the instruction raises an exception.  For example, if you specify a database that the runtime system cannot locate, or cannot open, or for which the user of your program does not have access privileges, an exception is raised.
  8. By default the database user is identified by the current operating system user, but it can be authenticated according to database specification parameters.

Warnings:

  1. When used in a program block, the DATABASE instruction has a different meaning than when it is used outside a program block. See Database Schema Specification for more details.
  2. The EXCLUSIVE keyword is specific to Informix databases; do not use this keyword when programming for non-Informix databases.

Tips:

  1. The CONNECT instructions allow better control over database connections; you should use these instructions instead of DATABASE and CLOSE DATABASE.

Example 1: Using a static database name.

01 MAIN
02    DATABASE stores
03    SELECT COUNT(*) FROM customer
04 END MAIN

Example 2: Using a variable.

01 MAIN
02    DEFINE dbname VARCHAR(100)
03    LET dbname = arg_val(1)
04    DATABASE dbname
05    SELECT COUNT(*) FROM customer
06 END MAIN

CLOSE DATABASE

Purpose:

Closes the current database connection when in unique-session mode.

Syntax:

CLOSE DATABASE

Notes:

  1. The current connection opened by a DATABASE instruction is closed.

Warnings:

  1. Current connection is automatically closed when the program ends. 

Example:

01 MAIN
02    DATABASE stores1
03    CLOSE DATABASE
04    DATABASE stores2
05    CLOSE DATABASE
06 END MAIN

CONNECT TO

Purpose:

Opens a new database session in multi-session mode.

Syntax:

CONNECT TO dbname [ AS session ]
    [ USER username USING password ]
    [ WITH CONCURRENT TRANSACTION ]

Notes:

  1. dbname is a string expression identifying the database specification.
  2. If dbname is not a string expression but is the DEFAULT keyword, you connect to the default Informix database server, identified by the INFORMIXSERVER environment variable, without any database selection. Informix only!
  3. session is a string expression identifying the database session. By default, it is dbname.
  4. username is a string expression identifying the name of the database user.
  5. password is a string expression identifying the password of the database user.
  6. If the instruction successfully connects to the database environment, the connection becomes the current database session for the application.
  7. An application can connect to several database environments at the same time, and it can establish multiple connections to the same database environment, provided each connection has a unique connection name.
  8. By default the database user is identified by the current operating system user, but it can be authenticated according to database specification parameters.
  9. When the USER username USING password clause is specified, the database user is identified by username and password, ignoring all other settings defined by the database specification.
  10. The WITH CONCURRENT TRANSACTION clause allows the program to open several transactions concurrently in different database sessions.

Warnings:

  1. The session name is case-sensitive.
  2. You cannot include a CONNECT statement within a PREPARE instruction.
  3. When using Informix databases on UNIX, the only restriction on establishing multiple connections to the same database environment is that an application can establish only one connection to each local server that uses the shared-memory connection mechanism. To find out whether a local server uses the shared-memory connection mechanism or the local-loopback connection mechanism, examine the $INFORMIXDIR/etc/sqlhosts file.

Example:

01 MAIN
02    CONNECT TO "stores1" -- Session name is "stores1"
03    CONNECT TO "stores1" AS "SA" -- Session name is "SA"
04    CONNECT TO "stores2" AS "SB" USER "scott" USING "tiger"
05 END MAIN

SET CONNECTION

Purpose:

Selects the current session when in multi-session mode.

Syntax:

SET CONNECTION { session [DORMANT] | CURRENT DORMANT }

Notes:

  1. session is a string expression identifying the name of the database session to be set as current.
  2. If session is not a string expression but is the DEFAULT keyword, it identifies the default database server connection established with a CONNECT TO DEFAULT or a DATABASE instruction. Informix only!
  3. To make the current connection dormant, use CURRENT DORMANT keyword. Informix only!

Warnings:

  1. The session name is case-sensitive.
  2. You cannot include a SET CONNECTION statement within a PREPARE instruction.
  3. The CURRENT DORMANT option is only supported for compatibility with Informix; there is no need to make a connection dormant in BDL programs.

Example:

01 MAIN
02    CONNECT TO "stores1"
03    CONNECT TO "stores1" AS "SA"
04    CONNECT TO "stores2" AS "SB"
05    SET CONNECTION "stores1"    -- Select first session
06    SET CONNECTION "SA"         -- Select second session
07    SET CONNECTION "stores1"    -- Select first session again
08 END MAIN

DISCONNECT

Purpose:

Terminates database sessions when in multi-session mode.

Syntax:

DISCONNECT { ALL | CURRENT | session }

Notes:

  1. session is a string expression identifying the name of the database session to be terminated.
  2. If session is not a string expression but is the DEFAULT keyword, it identifies the default database server connection established with a CONNECT TO DEFAULT or a DATABASE instruction. Informix only!
  3. Use the ALL keyword to terminate all opened connections. From that point, you must establish a new connection to execute SQL statements.
  4. Use the CURRENT keyword to terminate the current connection only. From that point, you must select another connection or establish a new connection to execute SQL statements.

Warnings:

  1. The session name is case-sensitive.
  2. You cannot include a DISCONNECT statement within a PREPARE instruction.
  3. If a DISCONNECT statement is used while a transaction is active, it is automatically rolled back.

Example:

01 MAIN
02    CONNECT TO "stores1"
03    CONNECT TO "stores1" AS "SA"
04    CONNECT TO "stores2" AS "SB" USER "scott" USING "tiger"
05    -- SB is the current database session
06    DISCONNECT "stores1" -- Continue with SB
07    DISCONNECT "SB" -- SB is no longer the current session 
08    SET CONNECTION "SA" -- Select second session
09 END MAIN