Friday, March 9, 2012

recover adodb::_ConnectionPtr

Most of the the time my connection is created and closed fine.

However, sometimes I get Connection failures (these happen often because I am connecting to the server using a VPN connection). Once I get the connection failure, sometimes I cannot successfully recreate the connection until I kill the application and restart it. Below is my open and release code. I call release anytime the connection fails or a stored procedure fails. If you see anything wrong with it the code below that would prevent the connection from recovering please let me know.

Thanks in advance.

Code Snippet

adodb::_ConnectionPtr m_spConnection;

HRESULT InitializeConnections()

{

HRESULT hr = E_FAIL;

if (m_spConnection && m_spConnection->GetState() == adodb::adStateOpen)

{

hr = S_OK;

}

else

{

if (m_spOperationEvents)

{

const _bstr_t c_bstrEmpty(_T(""));

try

{

CComBSTR bstrInit;

hr = get_DbInitializationString(&bstrInit);

if (!m_spConnection)

{

hr = m_spConnection.CreateInstance(adodb::CLSID_Connection);

}

if(SUCCEEDED(hr))

{

m_spConnection->ConnectionTimeout=30;

hr = m_spConnection->Open((LPCWSTR)bstrInit, c_bstrEmpty, c_bstrEmpty, -1);

}

}

catch (_com_error ce)

{

hr = E_FAIL;

CString strArgs;

strArgs.Format(_T("x%x\t%s\t"), ce.Error(), (LPCWSTR)ce.Description());

m_strLastError = GetTranslatedString(eMsgConnectionError, strArgs);

}

catch(HRESULT hrException)

{

hr = hrException;

}

catch(...)

{

hr = E_FAIL;

m_strLastError = GetTranslatedString(eMsgUnknownConnectionError, NULL);

}

}

}

if (!SUCCEEDED(hr))

{

ReleaseConnections();

}

return hr;

}

// Queued thread job to release thread resources

HRESULT CSptAggregation::ReleaseConnections()

{

HRESULT hr = E_FAIL;

try

{

if (m_spConnection)

{

m_spConnection->Cancel();

if (m_spConnection->GetState()== adodb::adStateOpen)

m_spConnection->Close();

}

m_spConnection = NULL;

}

catch (_com_error ce) { hr = E_FAIL; }

catch(...) { hr = E_FAIL; }

return S_OK;

}

To see whether this might be related to OLEDB session pooling you can try disabling it by adding
"OLE DB Services=-2" to your connection string. Depending on what the result is we could discuss further steps.

No comments:

Post a Comment