Link to home
Start Free TrialLog in
Avatar of jdrescher
jdrescher

asked on

ADO BeginTrans inside a try catch block

I have a couple of questions about using BeginTrans() in a try catch block. Below is an example of how I use ADO to modify a record in my Application. This application was originally made to use JET and I am tyring to migrate to MSDE. When I was using JET to maintain consistancy between the records of one connection and the records of another connection in the same app using a different thread I had to flush the cache in Jet ( using a call to pJetEngine->RefreshCache( m_pConnection)) and then call BeginTrans, update the data and call CommitTrans. In JET each ConnectionPtr has a cache on the database that is independent of other open connections. Using this procedure the thread will see and modify the current data in the db and not what was cached for this connection. Do we have to do someting like this with MSDE? Is there a way to flush the cache? Also is it possible that BeginTrans / CommitTrans will throw an exception? If so how do I handle BeginTrans inside a try / catch block? Do I call RollbackTrans in each of my catch routines? Could RollbackTrans() throw an exception?



_RecordsetPtr pRst = NULL;
try
{
  TESTHR(pRst.CreateInstance(__uuidof(Recordset)));
 
  pRst->Open(_bstr_t(strQuery), m_pConnection, adOpenStatic ,
     adLockOptimistic, adCmdText);

  m_pConnection->BeginTrans();

  int count = pRst->GetRecordCount();      
  if ( count == 1 ) {
    // Modify record using PutCollect
  }
 
  pRst->Update();
  pRst->Close();
  pRst=NULL;
  m_pConnection->CommitTrans();
}
catch (_com_error &e)
{
  GenerateError(e.Error(), e.ErrorMessage(), e.Description(),__FILE__,__LINE__);
}
catch( CException* e)
{
  GenerateError(e,__FILE__,__LINE__);
}
catch (...)
{
  GenerateError(__FILE__,__LINE__);  
}
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial