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 方法,這是命令或數據列集範圍中唯一的方法,可讓物件判斷其建立所在的會話。 因此,命令物件和只有命令物件,可讓應用程式擷取數據源對象指標,以便從中建立其他會話。