세션(Native Client OLE DB 공급자)

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics AnalyticsPlatform System(PDW)

Important

SQL Server Native Client(약칭 SNAC)는 SQL Server 2022(16.x) 및 SSMS(SQL Server Management Studio) 19에서 제거되었습니다. SQL Server Native Client OLE DB 공급자(SQLNCLI 또는 SQLNCLI11)와 레거시 Microsoft OLE DB Provider for SQL Server(SQLOLEDB)는 모두 새로운 개발에 권장되지 않습니다. 앞으로 SQL Server용 새 Microsoft OLE DB 드라이버(MSOLEDBSQL)로 전환합니다.

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 공급자 애플리케이션은 하나 이상의 개체 인터페이스에서 참조를 유지하여 세션 개체의 SQL Server 연결을 활성 상태로 유지할 수 있습니다.

예를 들어 명령 만들기 개체 참조 풀을 유지 관리하면 풀의 해당 세션 개체에 대한 활성 연결이 유지됩니다. 세션 개체가 필요하므로 풀 유지 관리 코드는 세션이 필요한 애플리케이션 메서드에 유효한 IDBCreateCommand 인터페이스 포인터를 전달합니다. 애플리케이션 메서드에 더 이상 세션이 필요하지 않은 경우 메서드는 명령 생성 개체에 대한 애플리케이션의 참조를 해제하지 않고 다시 풀 유지 관리 코드로 인터페이스 포인터를 반환합니다.

참고 항목

앞의 예제에서 IDBCreateCommand 인터페이스는 ICommand 인터페이스가 GetDBSession 메서드를 구현하기 때문에 사용됩니다. 이 메서드는 개체가 만들어진 세션을 확인할 수 있도록 하는 명령 또는 행 집합 범위의 유일한 메서드입니다. 따라서 명령 개체와 명령 개체만 사용하면 애플리케이션이 추가 세션을 만들 수 있는 데이터 원본 개체 포인터를 검색할 수 있습니다.

참고 항목

데이터 원본 개체(OLE DB)