次の方法で共有


セッション

SQL Server Native Client OLE DB プロバイダー セッションは、SQL Server のインスタンスへの単一の接続を表します。

SQL Server Native Client OLE DB プロバイダーでは、セッションがデータ ソースのトランザクション領域を区切る必要があります。 特定のセッション オブジェクトから作成されたすべてのコマンド オブジェクトは、セッション オブジェクトのローカル トランザクションまたは分散トランザクションに参加します。

初期化されたデータ ソースで作成された最初のセッション オブジェクトは、初期化時に確立された SQL Server 接続を受け取ります。 セッション オブジェクトのインターフェイス上のすべての参照が解放されると、SQL Server のインスタンスへの接続が、データ ソースで作成された別のセッション オブジェクトで使用できるようになります。

データ ソースで作成された追加のセッション オブジェクトは、データ ソースで指定された SQL Server インスタンスへの独自の接続を確立します。 SQL Server のインスタンスへの接続は、アプリケーションがそのセッションを作成したオブジェクトへのすべての参照を解放すると削除されます。

次の例では、SQL Server Native Client OLE DB プロバイダーを使用して SQL Server データベースに接続する方法を示します。

int main()  
{  
    // Interfaces used in the example.  
    IDBInitialize*      pIDBInitialize      = NULL;  
    IDBCreateSession*   pIDBCreateSession   = NULL;  
    IDBCreateCommand*   pICreateCmd1        = NULL;  
    IDBCreateCommand*   pICreateCmd2        = NULL;  
    IDBCreateCommand*   pICreateCmd3        = NULL;  
  
    // Initialize COM.  
    if (FAILED(CoInitialize(NULL)))  
    {  
        // Display error from CoInitialize.  
        return (-1);  
    }  
  
    // Get the memory allocator for this task.  
    if (FAILED(CoGetMalloc(MEMCTX_TASK, &g_pIMalloc)))  
    {  
        // Display error from CoGetMalloc.  
        goto EXIT;  
    }  
  
    // Create an instance of the data source object.  
    if (FAILED(CoCreateInstance(CLSID_SQLNCLI10, NULL,  
        CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**)  
        &pIDBInitialize)))  
    {  
        // Display error from CoCreateInstance.  
        goto EXIT;  
    }  
  
    // The InitFromPersistedDS function   
    // performs IDBInitialize->Initialize() establishing  
    // the first application connection to the instance of SQL Server.  
    if (FAILED(InitFromPersistedDS(pIDBInitialize, L"MyDataSource",  
        NULL, NULL)))  
    {  
        goto EXIT;  
    }  
  
    // The IDBCreateSession interface is implemented on the data source  
    // object. Maintaining the reference received maintains the   
    // connection of the data source to the instance of SQL Server.  
    if (FAILED(pIDBInitialize->QueryInterface(IID_IDBCreateSession,  
        (void**) &pIDBCreateSession)))  
    {  
        // Display error from pIDBInitialize.  
        goto EXIT;  
    }  
  
    // Releasing this has no effect on the SQL Server connection  
    // of the data source object because of the reference maintained by  
    // pIDBCreateSession.  
    pIDBInitialize->Release();  
    pIDBInitialize = NULL;  
  
    // The session created next receives the SQL Server connection of  
    // the data source object. No new connection is established.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd1)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
    // A new connection to the instance of SQL Server is established to support the  
    // next session object created. On successful completion, the  
    // application has two active connections on the SQL Server.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd2)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
    // pICreateCmd1 has the data source connection. Because the  
    // reference on the IDBCreateSession interface of the data source  
    // has not been released, releasing the reference on the session  
    // object does not terminate a connection to the instance of SQL Server.  
    // However, the connection of the data source object is now   
    // available to another session object. After a successful call to   
    // Release, the application still has two active connections to the   
    // instance of SQL Server.  
    pICreateCmd1->Release();  
    pICreateCmd1 = NULL;  
  
    // The next session created gets the SQL Server connection  
    // of the data source object. The application has two active  
    // connections to the instance of SQL Server.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd3)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
EXIT:  
    // Even on error, this does not terminate a SQL Server connection   
    // because pICreateCmd1 has the connection of the data source   
    // object.  
    if (pICreateCmd1 != NULL)  
        pICreateCmd1->Release();  
  
    // Releasing the reference on pICreateCmd2 terminates the SQL  
    // Server connection supporting the session object. The application  
    // now has only a single active connection on the instance of SQL Server.  
    if (pICreateCmd2 != NULL)  
        pICreateCmd2->Release();  
  
    // Even on error, this does not terminate a SQL Server connection   
    // because pICreateCmd3 has the connection of the   
    // data source object.  
    if (pICreateCmd3 != NULL)  
        pICreateCmd3->Release();  
  
    // On release of the last reference on a data source interface, the  
    // connection of the data source object to the instance of SQL Server is broken.  
    // The example application now has no SQL Server connections active.  
    if (pIDBCreateSession != NULL)  
        pIDBCreateSession->Release();  
  
    // Called only if an error occurred while attempting to get a   
    // reference on the IDBCreateSession interface of the data source.  
    // If so, the call to IDBInitialize::Uninitialize terminates the   
    // connection of the data source object to the instance of SQL Server.  
    if (pIDBInitialize != NULL)  
    {  
        if (FAILED(pIDBInitialize->Uninitialize()))  
        {  
            // Uninitialize is not required, but it fails if an  
            // interface has not been released. Use it for  
            // debugging.  
        }  
        pIDBInitialize->Release();  
    }  
  
    if (g_pIMalloc != NULL)  
        g_pIMalloc->Release();  
  
    CoUninitialize();  
  
    return (0);  
}  

SQL Server Native Client OLE DB プロバイダー セッション オブジェクトを SQL Server のインスタンスに接続すると、セッション オブジェクトを継続的に作成および解放するアプリケーションに対して大きなオーバーヘッドが発生する可能性があります。 SQL Server Native Client OLE DB プロバイダー セッション オブジェクトを効率的に管理することで、オーバーヘッドを最小限に抑えることができます。 SQL Server Native Client OLE DB プロバイダー アプリケーションは、オブジェクトの少なくとも 1 つのインターフェイスで参照を維持することで、セッション オブジェクトの SQL Server 接続をアクティブな状態に保つことができます。

たとえば、コマンド作成オブジェクト参照のプールを維持すると、プール内のセッション オブジェクトのアクティブな接続が維持されます。 セッション オブジェクトが必要な場合、プールのメンテナンス コードは、セッションを必要とするアプリケーション メソッドに有効な IDBCreateCommand インターフェイス ポインターを渡します。 アプリケーション メソッドでセッションが不要になった場合、メソッドは、コマンド作成オブジェクトへのアプリケーションの参照を解放するのではなく、インターフェイス ポインターをプールメンテナンス コードに戻します。

前の例では、 IDBCreateCommand インターフェイスが使用されています。これは、 ICommand インターフェイスが GetDBSession メソッドを実装しているためです。これは、オブジェクトが作成されたセッションを判断できるようにするコマンドまたは行セット スコープ内の唯一のメソッドです。 そのため、コマンド オブジェクトとコマンド オブジェクトのみを使用すると、アプリケーションは、追加のセッションを作成できるデータ ソース オブジェクト ポインターを取得できます。

こちらもご覧ください

データ ソース オブジェクト (OLE DB)