Click to See Complete Forum and Search --> : URGENT!! ::SQLDriverConnect seems hang...


NoAntz
February 24th, 2005, 05:18 AM
Hello!

I have troubles with ODBC connection to a MSSQL server when server is down. My MFC application seems to be hang when it tries to open an ODBC connection (exactly into ::SQLDriverConnect).

This is my code....


bool CMyRecordsetClass::InsertInfo(...)
{
bool ok = false;
try
{
// Comprueba la base de datos
if (!m_Database.IsOpen())
{
CString szConnectString;
szConnectString.Format("ODBC;DSN=%s;UID=%s;PWD=%s", m_szDSN, m_szDBUsername, m_szDBPassword);
m_Database.Open(szConnectString, FALSE, FALSE, szConnectString, FALSE);
}


// Insert into some registers....
ok = DoInsert(...);
}
catch(CDBException e)
{
// Ooops DB error
}
catch(CMemoryException e)
{
// Ooops Memory error
}

if (ok == true)
{
// Info saved into DB!!!
}
else
{
// Info not saved into DB!!!
}

return ok;
}


bool CMyRecordsetClass::DoInsert(...)
{
bool ok = false;
BOOL bConnected = TRUE;
CString szConnectionString;
CString szDSN, szUsername, szPassword;
VERIFY(GetDatabaseCfgParams(szDSN, szUsername, szPassword) == TRUE);
szConnectionString.Format("DSN=%s;UID=%s;PWD=%s;", szDSN, szUsername, szPassword);

try
{
if (!m_pDatabase) // No hay conexión, la crea
{
m_pDatabase = new CDatabase();
}

// Abre la transacción con la base de datos
szConnectionString += GetDefaultConnect();
if (!m_pDatabase->IsOpen()) // Abre la base de datos en caso de estar cerrada
bConnected = m_pDatabase->OpenEx(szConnectionString, CDatabase::noOdbcDialog);

if (bConnected) // Esta conectado
{
BOOL bTransOk = m_pDatabase->BeginTrans();
if(bTransOk == FALSE)
{
TRACE(" BeginTrans Failed");
}

InsertInfo();

bTransOk = m_pDatabase->CommitTrans();
if(bTransOk == FALSE)
{
TRACE("CommitTrans Failed");
}

ok = true;
}
}
catch (CException *e)
{
if (e->IsKindOf(RUNTIME_CLASS(CDBException))) // Problemas en la base de datos
{
if (((CDBException *) e)->m_strStateNativeOrigin.Find("State:08S01") != -1) // Se ha caido la conexión
{
if (IsOpen())
Close();

m_pDatabase->Close();
#ifdef _DEBUG
m_pDatabase->m_bTransactionPending = FALSE;
#endif
}
}

e->Delete();
}

if (m_pDatabase != NULL
&& m_pDatabase->m_hdbc != SQL_NULL_HDBC)
{
#ifdef _DEBUG
if (m_pDatabase->m_bTransactionPending) // Transacción pendiente
{
BOOL bTransOk = m_pDatabase->Rollback();
if(bTransOk == FALSE)
{
TRACE("Rollback Failed");
}
}
#else
m_pDatabase->Rollback();
#endif
}

return ok;
}



What's the problem. All works fine, but when I stop MSSQL and I try to insert data into database, I receive a communication failure exception. All it's correct, I catch exception and process it. But when I try to insert again a register (server still is stopped), the application seems to be hang when it tries to open database.



bool CMyRecordsetClass::InsertInfo(...)
{
bool ok = false;
try
{
// Comprueba la base de datos
if (!m_Database.IsOpen())
{
CString szConnectString;
szConnectString.Format("ODBC;DSN=%s;UID=%s;PWD=%s", m_szDSN, m_szDBUsername, m_szDBPassword);
m_Database.Open(szConnectString, FALSE, FALSE, szConnectString, FALSE);
}

// Insert into some registers....
ok = DoInsert(...);
}
catch(...)
{

}
}


Exactly, execution never exits from SQLDriverConnection and I don't receive any expection. Application seems to be hang :confused:


BOOL CDatabase::Connect(DWORD dwOptions)
{
// ....
AFX_SQL_SYNC(::SQLDriverConnect(m_hdbc, hWnd,
(UCHAR*)T2A((LPTSTR)(LPCTSTR)m_strConnect), SQL_NTS,
szConnectOutput, _countof(szConnectOutput),
&nResult, wConnectOption));
// ....
}


I am disturbed!!! :(

Do someone have any ideas?

Thanks you in advance!!

NoAntz
February 24th, 2005, 09:29 AM
Stupid Bug!!

My application is a windows service and when application tries to connect (when SQL server is down) ODBC dialog appears in desktop window. But my application is a service and it hasn't a visible desktop window, but the ODBC connection modal dialog appears blocking my application.

In order to solve the problem, I have changed the Open method by the OpenEx method and I choosed CDatabase::noOdbcDialog option. Now, when I try to connect to database and server is closed, an exception raises and I can catch it.


bool CMyRecordsetClass::InsertInfo(...)
{
bool ok = false;
try
{
// Comprueba la base de datos
if (!m_Database.IsOpen())
{
CString szConnectString;
szConnectString.Format("DSN=%s;UID=%s;PWD=%s", m_szDSN, m_szDBUsername, m_szDBPassword);
m_Database.OpenEx(szConnectString, CDatabase::noOdbcDialog);
}
....
}


Thnaks!