Warning, /sdk/codevis/thirdparty/soci/docs/connections.md is written in an unsupported language. File is not indexed.

0001 # Connections
0002 
0003 The `session` class encapsulates the database connection and other backend-related details, which are common to all the statements that will be later executed. It has a couple of overloaded constructors.
0004 
0005 ## Using backend factory
0006 
0007 The most basic one expects two parameters: the requested backend factory object and the generic connection string,
0008 which meaning is backend-dependent.
0009 
0010 Example:
0011 
0012 ```cpp
0013 session sql(oracle, "service=orcl user=scott password=tiger");
0014 ```
0015 
0016 Another example might be:
0017 
0018 ```cpp
0019 session sql(postgresql, "dbname=mydb");
0020 ```
0021 
0022 Above, the `sql` object is a local (automatic) object that encapsulates the connection.
0023 
0024 This `session` constructor either connects successfully, or throws an exception.
0025 
0026 ### Portability note
0027 
0028 In case of SOCI linked against DLLs on Windows, the factory objects are not exported from the DLLs.
0029 In order to avoid linker errors, access factory objects via dedicated backend functions
0030 provided (eg. `factory_postgresql()`).
0031 
0032 ## Using loadable backends
0033 
0034 Dynamically loadable backends are compiled as shared libraries and allow to select backends at run-time by name.
0035 
0036 The usage is similar to the above, but instead of providing the factory object, the backend name is expected:
0037 
0038 ```cpp
0039 session sql("postgresql", "dbname=mydb");
0040 ```
0041 
0042 For convenience, the URL-like form that combines both the backend name with connection parameters is supported as well:
0043 
0044 ```cpp
0045 session sql("postgresql://dbname=mydb");
0046 ```
0047 
0048 The last two constructors described above try to locate the shared library with the name `libsoci_ABC.so` (or `libsoci_ABC.dll` on Windows), where ABC is the backend name.
0049 In the above examples, the expected library name will be `libsoci_postgresql.so` for Unix-like systems.
0050 
0051 The most general form of the constructor takes a single object of `connection_parameters` type which contains a pointer to the backend to use, the connection string and also any connection options.
0052 Using this constructor is the only way to pass any non-default options to the backend.
0053 
0054 For example, to suppress any interactive prompts when using ODBC backend you could do:
0055 
0056 ```cpp
0057 connection_parameters parameters("odbc", "DSN=mydb");
0058 parameters.set_option(odbc_option_driver_complete, "0" /* SQL_DRIVER_NOPROMPT */);
0059 session sql(parameters);
0060 ```
0061 
0062 Notice that you need to `#include<soci-odbc.h>` to obtain the option name declaration.
0063 The existing options are described in the backend-specific part of the documentation.
0064 
0065 IBM DB2 driver for ODBC and CLI also support the driver completion requests.
0066 So, the DB2 backend provides similar option `db2_option_driver_complete` with `#include <soci-db2.h>` required to obtain the option name.
0067 
0068 ### Environment configuration
0069 
0070 The `SOCI_BACKENDS_PATH` environment variable defines the set of paths where the shared libraries will be searched for.
0071 There can be many paths, separated by colons, and they are used from left to right until the library with the appropriate name is found. If this variable is not set or is empty, the current directory is used as a default path for dynamically loaded backends.
0072 
0073 ## Using registered backends
0074 
0075 The run-time selection of backends is also supported with libraries linked statically.
0076 
0077 Each backend provides a separate function of the form `register_factory_*name*`, where `*name*` is a backend name. Thus:
0078 
0079 ```cpp
0080 extern "C" void register_factory_postgresql();
0081 // ...
0082 register_factory_postgresql();
0083 session sql("postgresql://dbname=mydb");
0084 ```
0085 
0086 The above example registers the backend for PostgreSQL and later creates the session object for that backend.
0087 This form is provided for those projects that prefer static linking but still wish to benefit from run-time backend selection.
0088 
0089 An alternative way to set up the session is to create it in the disconnected state and connect later:
0090 
0091 ```cpp
0092 session sql;
0093 
0094 // some time later:
0095 sql.open(postgresql, "dbname=mydb");
0096 
0097 // or:
0098 sql.open("postgresql://dbname=mydb");
0099 
0100 // or also:
0101 connection_parameters parameters("postgresql", "dbname=mydb");
0102 sql.open(parameters);
0103 ```
0104 
0105 The rules for backend naming are the same as with the constructors described above.
0106 
0107 The session can be also explicitly `close`d and `reconnect`ed, which can help with basic session error recovery, e.g. the application could check if `is_connected` still returns true after getting an error and attempt to call `reconnect` if it doesn't.
0108 The `reconnect` function has no parameters and attempts to use the same values as those provided with earlier constructor or `open` calls.
0109 
0110 See also the page devoted to [multithreading](multithreading.md) for a detailed description of connection pools.
0111 
0112 It is possible to have many active `session`s at the same time, even using different backends.
0113 
0114 ### Portability note
0115 
0116 The following backend factories are currently (as of 3.1.0 release) available:
0117 
0118 * [mysql](backends/mysql.md) (requires `#include "soci-mysql.h"`)
0119 * [oracle](backends/oracle.md) (requires `#include "soci-oracle.h"`)
0120 * [postgresql](backends/postgresql.md) (requires `#include "soci-postgresql.h"`)
0121 
0122 The following backends are also available, with various levels of completeness:
0123 
0124 * [sqlite3](backends/sqlite3.md) (requires `#include "soci-sqlite3.h"`)
0125 * [odbc](backends/odbc.md) (requires `#include "soci-odbc.h"`)
0126 * [firebird](backends/firebird.md) (requires `#include "soci-firebird.h"`)
0127 * [db2](backends/db2.md) (requires `#include "soci-db2.h"`)
0128 
0129 ## Connection failover
0130 
0131 The `failover_callback` interface can be used as a callback channel for notifications of events that are automatically processed when the session is forcibly closed due to connectivity problems. The user can override the following methods:
0132 
0133 ```cpp
0134 // Called when the failover operation has started,
0135 // after discovering connectivity problems.
0136 virtual void started();
0137 
0138 // Called after successful failover and creating a new connection;
0139 // the sql parameter denotes the new connection and allows the user
0140 // to replay any initial sequence of commands (like session configuration).
0141 virtual void finished(session & sql);
0142 
0143 // Called when the attempt to reconnect failed,
0144 // if the user code sets the retry parameter to true,
0145 // then new connection will be attempted;
0146 // the newTarget connection string is a hint that can be ignored
0147 // by external means.
0148 virtual void failed(bool & retry, std::string & newTarget);
0149 
0150 // Called when there was a failure that prevents further failover attempts.
0151 virtual void aborted();
0152 ```
0153 
0154 The user-provided callback implementation can be installed (or reset) with:
0155 
0156 ```cpp
0157 sql.set_failover_callback(myCallback);
0158 ```
0159 
0160 ### Portability note
0161 
0162 The `failover_callback` functionality is currently supported only by PostgreSQL and Oracle backends (in the latter case the failover mechanism is governed by the Oracle-specific cluster configuration settings).
0163 Other backends allow the callback object to be installed, but will ignore it and will not generate notification calls.