informix
Informix DataBlade API Programmer's Manual
Establishing a Connection

Initializing a Connection

Before a client LIBMI application can establish a connection, it must initialize the connection with the name of the database server and database to which it needs to connect. This initialization occurs in the following steps.

Connection-Initialization Steps DataBlade API Task
1. Indicate the database server to which you want to connect. Set connection parameters in the connection-information descriptor.
2. Indicate the database to which you want to connect and user you want to log in as. Set database parameters in the database-information descriptor.
3. Indicate settings for session-specific features (optional). Set the session parameters in the parameter-information descriptor.

Using Connection Parameters

To indicate which database server it needs to connect to, the client LIBMI application uses connection parameters. The DataBlade API provides a connection-information descriptor, MI_CONNECTION_INFO, to access connection parameters. This data structure is similar in concept to a file descriptor in UNIX. It identifies the database server for a particular session.

Unlike most DataBlade API structures, the connection-information descriptor is not an opaque C data structure. To access connection parameters, you must allocate a connection-information descriptor and directly access its fields. Figure 7-1 shows the fields in the connection-information descriptor.

Figure 7-1
Fields in the Connection-Information Descriptor

Field Data Type Description
server_name char * The name of the default database server. This field corresponds to the value of the INFORMIXSERVER environment variable.
server_port mi_integer The name of a relative location in shared memory for this particular database server. This field corresponds to the value of the SERVERNUM configuration parameter of the ONCONFIG file.
locale char * The name of the server locale. This field corresponds to the SERVER_LOCALE environment variable (as set on the computer with the database server)
reserved1 mi_integer Reserved for future use.
reserved2 mi_integer Reserved for future use.

The milib.h header file defines the MI_CONNECTION_INFO structure.

With the connection-information descriptor, you can use the following DataBlade API functions to perform the connection-parameter tasks.

Connection-Parameter Task DataBlade API Function
Access the default connection parameters to determine the database server for the connection mi_set_default_connection_info(), mi_get_default_connection_info()
Obtain current connection parameters for an open connection mi_get_connection_info()

Establishing Default Connection Parameters

The default connection parameters identify to which database server to connect. Before you establish a connection, determine which of the following connection parameters to use:

System-Default Connection Parameters

The database server obtains values for the system-default connection parameters from the execution environment of the client LIBMI application. When you use system-default connection parameters, you enable your application to be portable across client/server environments. However, before the application begins execution, you must ensure that the client/server environment is correctly initialized.

Figure 7-2 shows the system-default connection parameters that the database server uses to establish a connection.

Figure 7-2
System-Default Connection Parameters

System-Default Connection Parameter System-Default Value
Database server name INFORMIXSERVER environment variable
Server port SERVERNUM configuration parameter
Server locale SERVER_LOCALE environment variable or default locale (en_us)

The system-default connection parameters provide connection information for all connections made within a client LIBMI application unless you explicitly override them within the application.

User-Defined Connection Parameters

The database server obtains values for the connection parameters from the connection-information descriptor. The database server initializes the connection-information descriptor with the system-default connection parameters in Figure 7-2 on page 7-8. You can initialize your own connection-information descriptor to override the system-default connection parameters. When you override system-default connection parameters, you enable your application to have connection information that is independent of the client/server environment in which it runs.

To override the system-default connection parameters, follow these steps:

  1. Allocate a connection-information descriptor.
  2. Fill the fields of the connection-information descriptor with the default connection parameters you need.
  3. If you do not set a particular field, the database server uses the system-default value in Figure 7-2 on page 7-8 for the associated connection parameter.

  4. Pass a pointer to this connection-information descriptor to the mi_set_default_connection_info() function.

The user-defined connection parameters provide connection information for all connections made within a client LIBMI application after these functions execute (unless the functions are called again to set new default values).

You can examine existing default connection parameters with the mi_get_default_connection_info() function. This function populates a user-defined connection-information descriptor with the current default connection parameters.

In a C UDR, mi_get_default_connection_info() obtains the same information as mi_get_connection_info(). The mi_set_default_connection_info() function is ignored when it is used in a user-defined routine.

Obtaining Current Connection Parameters

To obtain connection parameters associated with an open connection, use the mi_get_connection_info() function. This function populates a user-defined connection-information descriptor with values from the specified open connection.

The mi_get_connection_info() function is valid when it is used in a C UDR. For more information, see Accessing the Session Environment.

Using Database Parameters

To indicate which database it needs to connect to, the client LIBMI application uses database parameters. The DataBlade API provides a database-information descriptor, MI_DATABASE_INFO, to access database parameters. This data structure identifies the database for a particular session.

Unlike most DataBlade API structures, the database-information descriptor is not an opaque C data structure. To access database information, you must allocate a database-information descriptor and directly access its fields. Figure 7-3 shows the fields in the database-information descriptor.

Figure 7-3
Fields in the Database-Information Descriptor

Field Data Type Description
database_name char * The name of the database.
user_name char * The user account name, as defined by the operating system.
password char * The account password, as defined by the operating system.

The milib.h header file defines the MI_DATABASE_INFO structure.

With the database-information descriptor, you can use the following DataBlade API functions to perform the database-parameter tasks.

Database-Parameter Task DataBlade API Function
Access the default database parameters to determine the database and user for the connection mi_set_default_database_info(), mi_get_default_database_info()
Obtain current database parameters for an open connection mi_get_database_info()

Establishing Default Database Parameters

The default database parameters identify the database and user for the connection. Before you establish a connection, you can determine which of the following database parameters to use:

System-Default Database Parameters

The database server obtains values for the system-default database parameters from the execution environment of the client LIBMI application. When you use system-default database parameters, you enable your application to be portable across client/server environments. However, you must ensure that the client/server environment is correctly initialized to provide the system-default values.

Figure 7-4 shows the system-default database parameters that the database server uses to open a database.

Figure 7-4
System-Default Database Parameters

System-Default Database Parameter System-Default Value
Database name None
User-account name Account name of user that invoked the client LIBMI application
Account password Account password of user that invoked the client LIBMI application

The system-default database parameters provide database information for all connections made within a client LIBMI application unless you explicitly override them within the application.

User-Defined Database Parameters

The database server obtains values for the database parameters from the database-information descriptor. The database server initializes the database-information descriptor with the system-default database parameters in Figure 7-4 on page 7-12. You can initialize your own database-information descriptor to override the default database parameters. When you override system-default database parameters, you enable your application to have database information that is independent of the client/server environment in which it runs.

To override the system-default database parameters, follow these steps:

  1. Allocate a database-information descriptor.
  2. Fill the fields of the database-information descriptor with the default database parameters you need.
  3. If you do not set a particular field, the database server uses the system-default value in Figure 7-4 on page 7-12 for the associated database parameter.

  4. Pass a pointer to this database-information descriptor to the mi_set_default_database_info() function.

The user-defined database parameters provide database information for all connections made within a client LIBMI application after these functions execute (unless the functions are called again to set new default values).

You can examine existing default database parameters with the mi_get_default_database_info() function. This function populates a user-defined database-information descriptor with the current default database parameters.

In a C UDR, mi_get_default_database_info() obtains the same information as mi_get_database_info(). The mi_set_default_database_info() function is ignored.

Obtaining Current Database Parameters

To obtain database parameters associated with an open connection, use the mi_get_database_info() function. This function populates a user-defined database-information descriptor with values from the specified open connection.

The mi_get_database_info() function is valid with a C UDR.

Using Session Parameters

The parameter-information descriptor, MI_PARAMETER_INFO, allows you to set the following session parameters for the client LIBMI application:

Unlike most DataBlade API structures, the parameter-information descriptor is not an opaque C data structure. To access session-parameter information, you must directly access the fields of a parameter-information descriptor that you allocate. Figure 7-5 shows the fields in the MI_PARAMETER_INFO structure.

Figure 7-5
Fields in the Parameter-Information Descriptor

Field Data Type Description
callbacks_enabled mi_integer Indicates whether callbacks are enabled. Valid values are:
  • A value of 1 indicates that callbacks are enabled.
  • A value of 0 indicates that callbacks are disabled.
  • pointer_checks_enabled mi_integer Indicates whether pointers (such as MI_ROW pointers) that the client LIBMI application passes to the database server are checked to ensure that they are within the heap space of the process:
  • A value of 1 indicates that pointers are checked.
  • A value of 0 indicates that pointers are not checked.
  • The milib.h header file defines the MI_PARAMETER_INFO structure.

    Before you establish a connection, you can determine which of the following session parameters to use:

    Using System-Default Session Parameters

    When the database server establishes a connection, it uses the values in Figure 7-6 as the system-default session parameters.

    Figure 7-6
    System-Default Session Parameters

    System-Default Session Parameter System-Default Value
    Callbacks Enabled? Yes
    Pointers Checked? Yes

    The system-default session parameters provide session-parameter information for all connections made within a client LIBMI application unless you explicitly override them within the application.

    Using User-Defined Session Parameters

    The database server obtains values for the session parameters from the parameter-information descriptor. The database server initializes the parameter-information descriptor with the system-default session parameters in page 7-14. To override these system-default values, you can initialize your own parameter-information descriptor to set session parameters.

    The following DataBlade API functions access default session parameters for a client LIBMI application.

    DataBlade API Function Purpose
    mi_set_parameter_info() Sets session parameters for the current session
    mi_get_parameter_info() Obtains session parameters for the current session

    To override the system-default session parameters, follow these steps:

    1. Allocate a parameter-information descriptor.
    2. Fill the fields of the parameter-information descriptor with the default session parameters you need.
    3. If you do not set a particular field, the database server uses the system-default value in Figure 7-6 on page 7-14 for the associated session parameter.

    4. Pass a pointer to this parameter-information descriptor to the mi_set_parameter_info() function.

    You can examine existing session parameters with the mi_get_parameter_info() function. This function populates a user-defined parameter-information descriptor with the current session parameters.

    A Sample Client Connection

    The following example shows one way to set default connection parameters. Assume that the system-default connection parameters are as follows.

    System-Default Parameter Parameter Value
    Default database server joe (INFORMIXSERVER environment variable is set to joe)
    Default server port 200 (SERVERNUM configuration parameter is set to 200)
    Default user tester
    Default user password No password
    Callbacks enabled? Yes (system default)
    Pointers checked? Yes (system default)

    The following code fragment uses DataBlade API functions to change some of the default system values.

    DataBlade API Function Default Parameter Default Value
    mi_set_default_connection_info() Database server name beth
    Server port = 143 143
    mi_set_default_database_info() Database name template1
    User-account name miadmin
    User-account password No password

    Once these new defaults are established, the application calls mi_server_connect() to request a connection to the beth database server. If this request is successful, the application opens the template1 database with mi_login(). For more information on mi_server_connect(), see Connections with mi_server_connect() and mi_login(). For a description of the all_callback() function, see Creating an All-Events Callback.


    Informix DataBlade API Programmer's Manual, Version 9.2
    Copyright © 1999, Informix Software, Inc. All rights reserved