f90SQLConnect (SQLConnect)

 

Conformance

Version Introduced: ODBC 1.0

Standards Compliance: ISO 92

 

Summary

f90SQLConnect establishes connections to a driver and a data source. The connection handle references storage of all information about the connection to the data source, including status, transaction state, and error information.

 

Syntax

f90SQLConnect (ConnectionHandle, ServerName, UserName, Authentication, iRet)

 

integer(SQLHDBC_KIND), intent(in):: ConnectionHandle
character(len=*),intent(in):: ServerName
character(len=*),intent(in):: UserName
character(len=*),intent(in):: Authentication
integer(SQLRETURN_KIND),intent(out):: iRet

 

Arguments

ConnectionHandle [Input]

Connection handle.

ServerName [Input]

Character string with a data source name.

UserName [Input]

Character string with a User identifier.

Authentication [Input]

Character string with Authentication data (typically the password).

iRet [Output]

SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_ERROR, or SQL_INVALID_HANDLE.

Diagnostics

When f90SQLConnect returns SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value can be obtained by calling f90SQLGetDiagRec with a HandleType of SQL_HANDLE_DBC and a Handle of ConnectionHandle. The following table lists the SQLSTATE values commonly returned by f90SQLConnect and explains each one in the context of this function; the notation "(DM)" precedes the descriptions of SQLSTATEs returned by the Driver Manager. The return code associated with each SQLSTATE value is SQL_ERROR, unless noted otherwise.

SQLSTATE Error Description
01000 General warning Driver-specific informational message. (Function returns SQL_SUCCESS_WITH_INFO.)
01S02 Option value changed The driver did not support the specified value of the ValuePtr argument in f90SQLSetConnectAttr and substituted a similar value. (Function returns SQL_SUCCESS_WITH_INFO.)
08001 Client unable to establish connection The driver was unable to establish a connection with the data source.
08002 Connection name in use (DM) The specified ConnectionHandle had already been used to establish a connection with a data source and the connection was still open or the user was browsing for a connection.
08004 Server rejected the connection The data source rejected the establishment of the connection for implementation-defined reasons.
08S01 Communication link failure The communication link between the driver and the data source to which the driver was attempting to connect failed before the function completed processing.
28000 Invalid authorization specification The value specified for the argument UserName or the value specified for the argument Authentication violated restrictions defined by the data source.
HY000 General error An error occurred for which there was no specific SQLSTATE and for which no implementation-specific SQLSTATE was defined. The error message returned by f90SQLGetDiagRec in the MessageText buffer describes the error and its cause.
HY001 Memory allocation error (DM) The Driver Manager was unable to allocate memory required to support execution or completion of the function.The driver was unable to allocate memory required to support execution or completion of the function.
HY013 Memory management error The function call could not be processed because the underlying memory objects could not be accessed, possibly because of low memory conditions.
HY090 Invalid string or buffer length (DM) The value specified for argument ServerNameLength, UserNameLength, or AutheticationLength was less than 0, but not equal to SQL_NTS.

(DM) The value specified for argument ServerNameLength exceeded the maximum length for a data source name.

HYT00 Timeout expired The query timeout period expired before the connection to the data source completed. The timeout period is set through f90SQLSetConnectAttr, SQL_ATTR_LOGIN_TIMEOUT
HYT01 Connection timeout expired The connection timeout period expired before the data source responded to the request. The connection timeout period is set through f90SQLSetConnectAttr, SQL_ATTR_CONNECTION_TIMEOUT.
IM001 Driver does not support this function (DM) The driver specified by the data source name does not support the function.
IM002 Data source not found and no default driver specified (DM) The data source name specified in the argument ServerName was not found in the system information, nor was there a default driver specification.
IM003 Specified driver could not be connected to (DM) The driver listed in the data source specification in system information was not found or could not be connected to for some other reason.
IM004 Driver's f90SQLAllocHandle on SQL_HANDLE_ENV failed (DM) During f90SQLConnect, the Driver Manager called the driver's f90SQLAllocHandle function with a HandleType of SQL_HANDLE_ENV and the driver returned an error.
IM005 Driver's f90SQLAllocHandle on SQL_HANDLE_DBC failed (DM) During f90SQLConnect, the Driver Manager called the driver's f90SQLAllocHandle function with a HandleType of SQL_HANDLE_DBC and the driver returned an error.
IM006 Driver's f90SQLSetConnectAttr failed During f90SQLConnect, the Driver Manager called the driver's f90SQLSetConnectAttr function and the driver returned an error. (Function returns SQL_SUCCESS_WITH_INFO).
IM009 Unable to connect to translation DLL The driver was unable to connect to the translation DLL that was specified for the data source.
IM010 Data source name too long (DM) ServerName was longer than SQL_MAX_DSN_LENGTH characters.

 

Comments

The Driver Manager does not connect to a driver until the application calls a function (f90SQLConnect, f90SQLDriverConnect, or f90SQLBrowseConnect) to connect to the driver. Until that point, the Driver Manager works with its own handles and manages connection information. When the application calls a connection function, the Driver Manager checks whether a driver is currently connected to for the specified ConnectionHandle:

The driver then allocates handles and initializes itself.

When the application calls f90SQLDisconnect, the Driver Manager calls f90SQLDisconnect in the driver. However, it does not disconnect the driver. This keeps the driver in memory for applications that repeatedly connect to and disconnect from a data source. When the application calls f90SQLFreeHandle with a HandleType of SQL_HANDLE_DBC, the Driver Manager calls f90SQLFreeHandle with a HandleType of SQL_HANDLE_DBC and then f90SQLFreeHandle with a HandleType of SQL_HANDLE_ENV in the driver, and then disconnects the driver.

An ODBC application can establish more than one connection.

Connection Pooling

Connection pooling allows an application to reuse a connection that has already been created. When connection pooling is enabled and f90SQLConnect is called, the Driver Manager attempts to make the connection using a connection that is part of a pool of connections in an environment that has been designated for connection pooling. This environment is a shared environment that is used by all applications that use the connections in the pool.

Connection pooling is enabled before the environment is allocated by calling f90SQLSetEnvAttr to set SQL_ATTR_CONNECTION_POOLING to SQL_CP_ONE_PER_DRIVER (which specifies a maximum of one pool per driver) or SQL_CP_ONE_PER_HENV (which specifies a maximum of one pool per environment). f90SQLSetEnvAttr in this case is called with EnvironmentHandle set to null, which makes the attribute a process-level attribute. If SQL_ATTR_CONNECTION_POOLING is set to SQL_CP_OFF, connection pooling is disabled.

Once connection pooling has been enabled, f90SQLAllocHandle with a HandleType of SQL_HANDLE_ENV is called to allocate an environment. The environment allocated by this call is a shared environment because connection pooling has been enabled. The environment to be used is not determined, however, until f90SQLAllocHandle with a HandleType of SQL_HANDLE_DBC is called.

f90SQLAllocHandle with a HandleType of SQL_HANDLE_DBC is called to allocate a connection. The Driver Manager attempts to find an existing shared environment that matches the environment attributes set by the application. If no such environment exists, one is created as an implicit shared environment. If a matching shared environment is found, the environment handle is returned to the application, and its reference count is incremented.

The connection to be used is not determined, however, until f90SQLConnect is called. At that point, the Driver Manager attempts to find an existing connection in the connection pool that matches the criteria requested by the application. These criteria include the connection options requested in the call to f90SQLConnect (the values of the ServerName, UserName, and Authentication keywords) and any connection attributes set since f90SQLAllocHandle with a HandleType of SQL_HANDLE_DBC was called. The Driver Manager checks this criteria against the corresponding connection keywords and attributes in connections in the pool. If a match is found, the connection in the pool is used. If no match is found, a new connection is created.

If the SQL_ATTR_CP_MATCH environment attribute is set to SQL_CP_STRICT_MATCH, the match must be exact for a connection in the pool to be used. If the SQL_ATTR_CP_MATCH environment attribute is set to SQL_CP_RELAXED_MATCH, the connection options in the call to f90SQLConnect must match, but not all of the connection attributes must match.

The following rules are applied when a connection attribute, as set by the application before f90SQLConnect is called, does not match the connection attribute of the connection in the pool:

When the application calls f90SQLDisconnect to disconnect, the connection is returned to the connection pool, and is available for reuse.

 

Code Example

In the following example, an application allocates environment and connection handles. It then connects to the SalesOrders data source with the user ID JohnS and the password Sesame and processes data. When it has finished processing data, it disconnects from the data source and frees the handles.

program Example101
 
!demonstrates use of f90SQLConnect
!load f90SQL modules
 
use f90SQLConstants
use f90SQL
 
implicit none
integer(SQLHENV_KIND)::henv
integer(SQLHDBC_KIND)::hdbc
integer(SQLHSTMT_KIND)::hstmt
integer(SQLRETURN_KIND)::retcode
 
!Allocate environment handle
call f90SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, henv,retcode)
 
if (retcode.eq.SQL_SUCCESS.or.retcode.eq.SQL_SUCCESS_WITH_INFO) then
!Set the ODBC version environment attribute
call f90SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, retcode)
if (retcode.eq.SQL_SUCCESS.or.retcode.eq.SQL_SUCCESS_WITH_INFO) then
!Allocate connection handle
call f90SQLAllocHandle(SQL_HANDLE_DBC, henv, hdbc, retcode)
if (retcode.eq.SQL_SUCCESS .or. retcode.eq.SQL_SUCCESS_WITH_INFO) then
!Set login timeout to 5 seconds.
call f90SQLSetConnectAttr(hdbc,SQL_ATTR_LOGIN_TIMEOUT, int(5, SQLUINTEGER_KIND), retcode)
!Connect to data source
call f90SQLConnect(hdbc, 'Sales', 'JohnS', 'Sesame', retcode)
if (retcode.eq.SQL_SUCCESS.or.retcode.eq.SQL_SUCCESS_WITH_INFO) then
!Allocate statement handle
call f90SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt, retcode)
if (retcode.eq.SQL_SUCCESS.or. retcode.eq.SQL_SUCCESS_WITH_INFO) then
!Process data
...
...
...
call f90SQLFreeHandle(SQL_HANDLE_STMT, hstmt, retcode)
endif
call f90SQLDisconnect(hdbc, retcode)
endif
call f90SQLFreeHandle(SQL_HANDLE_DBC, hdbc, retcode)
endif
endif
call f90SQLFreeHandle(SQL_HANDLE_ENV, henv, retcode)
endif
stop
end

 

Related Subroutines

For information about See
Allocating a handle f90SQLAllocHandle
Discovering and enumerating values required to connect to a data source f90SQLBrowseConnect
Disconnecting from a data source f90SQLDisconnect
Connecting to a data source using a connection string or dialog box f90SQLDriverConnect
Returning the setting of a connection attribute f90SQLGetConnectAttr
Setting a connection attribute f90SQLSetConnectAttr