We help IT Professionals succeed at work.

ADO BeginTrans inside a try catch block

jdrescher
jdrescher asked
on
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__);  
}
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
MSDE is like MSSQL server, so you are safe regarding the "cache"...

now, RollbackTrans can raise an error, if there is no BEGIN transaction for example... Also if meanwhile the database server has crashed (oooops)
And yes, you should have a RollbackTrans in all you catch{} blocks...

Cheers

Explore More ContentExplore courses, solutions, and other research materials related to this topic.