Examples seem to show different ways and different orders of doing things re: transactions. I'm hoping to do things the proper way, any comments of this code snippet would be appreciated :)
//pseudocode:
string sConnString = "myconnectionstring";
FbTransaction objTrans = null;
FbConnection objConn = new FbConnection(sConnString);
public bool DoSomeDatabaseStuff(objTrans, objConn, ref string sErrMsg)
{
objConn.Open();
objTrans = objConn.BeginTransaction();
FbCommand objCmd1 = new FbCommand("insert query one", objConn);
FbCommand objCmd2 = new FbCommand("insert query two", objConn);
//SOME MICROSOFT NOTES say I have to do this:
//objCmd1.Transaction = obj.Trans;
//objCmd2.Transaction = obj.Trans;
try
{
//ANY OF THESE THREE could throw an exception, correct?
//-should I arrange separate try/catch for each?
objCmd1.ExecuteNonQuery();
objCmd2.ExecuteNonQuery();
objTrans.Commit();
}
catch (Exception)
{
//DO I NEED A TRY/CATCH here for rollback? connection might be closed?
//-what could I do about it?
objTrans.Rollback();
}
finally
{
objConn.Close();
}
}