Session对象

神通数据库 OLE DB的Session对象标示对数据库的一个单独连接,数据库的事务操作就作用在这个对象上。所有的Command对象都由Session对象产生。Session对象由数据源对象的IDBCreateSession::CreateSession方法创建。只要Session对象没有被释放,它就一直保持对数据的一个连接。

下面的例子展示神通数据库 OLE DB如何连接数据库。

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_OSCAROLEDB, 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 SHENTONG 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 SHENTONG Server.

if (FAILED(pIDBInitialize->QueryInterface(IID_IDBCreateSession,

(void**) &pIDBCreateSession)))

{

// Display error from pIDBInitialize.

goto EXIT;

}

// Releasing this has no effect on the SHENTONG Server connection

// of the data source object because of the reference maintained by

// pIDBCreateSession.

pIDBInitialize->Release();

pIDBInitialize = NULL;

// The session created next receives the SHENTONG 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 SHENTONG Server is

established to support the

// next session object created. On successful completion, the

// application has two active connections on the SHENTONG 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

SHENTONG 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 SHENTONG Server.

pICreateCmd1->Release();

pICreateCmd1 = NULL;

// The next session created gets the SHENTONG Server connection

// of the data source object. The application has two active

// connections to the instance of SHENTONG 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 SHENTONG Server connection

// because pICreateCmd1 has the connection of the data source

// object.

if (pICreateCmd1 != NULL)

pICreateCmd1->Release();

// Releasing the reference on pICreateCmd2 terminates the SHENTONG

// Server connection supporting the session object. The application

// now has only a single active connection on the instance of

SHENTONG Server.

if (pICreateCmd2 != NULL)

pICreateCmd2->Release();

// Even on error, this does not terminate a SHENTONG 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 SHENTONG Server

is broken.

// The example application now has no SHENTONG 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

SHENTONG 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);

}

如果一个应用持续的创建和释放Session对象,将对应用性能带来较大影响。应用可以通过更有效的管理和使用Session对象来降低这种影响。通过保留Session对象的某个接口来保持对该Session对象的引用,应用可以保持对数据库的连接。