Page 1 of 1

New to Transactions

Posted: Tue 08 Aug 2006 13:29
by Tigger
Hi there,

I've been using MyDAC for Borland Builder 6 for a while now, but I've just started a new project which uses elements I've never used before in any form - namely transactions.

I'd like a little help to get me started please.

The following is some basic test code:

Code: Select all

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
    if (!MyConnection1->InTransaction)
    {
        MyConnection1->StartTransaction();

        MyQuery1->SQL->Text = "SELECT * FROM cvbs c;";
        MyQuery1->Execute();

        MyConnection1->Commit();

     }
}
I don't understand why this code fires even if I comment the Commit line out.

The following is from the Borland help files:

Code: Select all

void __fastcall TForm1::TransferButtonClick(TObject *Sender)

{
  if (!SQLConnection1->InTransaction)
  {

    TTransactionDesc TD;

    TD.TransactionID = 1;
    TD.IsolationLevel = xilREADCOMMITTED;
    SQLConnection1->StartTransaction(TD);
    try
    {
      int Amt = StrToInt(AmtEdit->Text);
      Debit->Params->ParamValues["Amount"] = Amt;
      Credit->Params->ParamValues["Amount"] = Amt;
      SQLConnection1->Commit(TD); // on success, commit the changes;
    }
    catch (...)
    {
      SQLConnection1->Rollback(TD); // on failure, undo the changes

      ShowMessage("Transfer failed")
    }
  }
}
I've tried emulating the first part:

Code: Select all

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
    if (!MyConnection1->InTransaction)
    {
        TTransactionDesc TD;

        TD.TransactionID = 1;
        TD.IsolationLevel = xilREADCOMMITTED;
        MyConnection1->StartTransaction(TD);

        MyQuery1->SQL->Text = "SELECT * FROM cvbs c;";
        MyQuery1->Execute(); // this line required?

        MyConnection1->Commit(TD);

     }
}
but the compiler throws it out as it doesn't know what a TTransactionDesc is. I originally thought I must be missing an include file, but can't seem to find any useful information anywhere on that.

If anyone can point me in the direction of a Borland based tutorial for DB transaction handlers I would very much appreciate it.

Many thanks in advance.

Posted: Wed 09 Aug 2006 09:08
by Jackson
TCustomDAConnection.StartTransaction does not have TTransactionDesc in its parameters.
Your first example is almost correct.
Executing SELECT statement doesn't cause any modifications in database.
Try to use updates, insertions and deletions that take place after a call to StartTransaction.
You will see that they are held by the server until the application calls Commit to save the changes or Rollback to cancel them.

Posted: Wed 09 Aug 2006 15:15
by Tigger
Many, many thanks.

I've been checking back here avidly hoping for a response :D