Alaska Software Inc. - BEGIN TRANSACTION-END TRANSACTION does not work.
Username: Password:
AuthorTopic: BEGIN TRANSACTION-END TRANSACTION does not work.
Itai Ben-ArtziBEGIN TRANSACTION-END TRANSACTION does not work.
on Sat, 14 Dec 2019 23:50:06 -0800
It seems the Begin Transaction – End Transaction does not work.  If a
runtime error occurs between Begin Transaction and End Transaction,
all database operations up to the error point are being executed.
For example:
*******************
BEGIN TRANSACTION
(nArea1)->(DbAppend())
(nArea1)->(FieldPut(1, xVal1)
(nArea1)->(FieldPut(2, xVal21)
(nArea2)->(DbAppend()) 
(nArea2)->(FieldPut(1, xVal1)   Runtime here.
(nArea2)->(FieldPut(2, xVal21)
END TRANSACTION
*******************
All operations throughout the runtime error point are executed.
What am I missing?

Many thanks,

-Itai
Andreas Gehrs-Pahl
Re: BEGIN TRANSACTION-END TRANSACTION does not work.
on Sun, 15 Dec 2019 14:28:19 -0500
Itai,

>What am I missing?

First, Begin/End Transaction (as well as Rollback) only works for Database 
Sessions, and not for directly opened tables. So, unless you use some sort
of database back-end/server, and opened a Session with DacSession(), using 
Begin/End Transaction has no effect at all.

Also, you need to RollBack any Transaction that you don't want to commit:

LOCAL bError := ErrorBlock({|e| Break(e)})
BEGIN SEQUENCE
   BEGIN TRANSACTION
      (nArea1)->(DbAppend())
      (nArea1)->(FieldPut(1, xVal1)
      (nArea1)->(FieldPut(2, xVal21)
      (nArea2)->(DbAppend()) 
      (nArea2)->(FieldPut(1, xVal1)   Runtime here.
      (nArea2)->(FieldPut(2, xVal21)
   END TRANSACTION
RECOVER
   ErrorBlock(bError)
   ROLLBACK
END SEQUENCE
ErrorBlock(bError)

Hope that helps,

Andreas

Andreas Gehrs-Pahl
Absolute Software, LLC

phone: (989) 723-9927
email: Andreas@AbsoluteSoftwareLLC.com
web:   http://www.AbsoluteSoftwareLLC.com
[F]:   https://www.facebook.com/AbsoluteSoftwareLLC
Itai Ben-ArtziRe: BEGIN TRANSACTION-END TRANSACTION does not work.
on Mon, 16 Dec 2019 10:44:15 -0800
Thank you,Andreas!
I [mistakenly] thought that all database operations between BEGIN
TRANSACTION and END TRANSACTION are executed only if/when END
TRANSACTION is reached.

Does ROLLBACK work with direct FieldPut() writing into DBF table?

Again, thank you for clarifying this issue.
Andreas Gehrs-Pahl
Re: BEGIN TRANSACTION-END TRANSACTION does not work.
on Tue, 17 Dec 2019 03:09:01 -0500
Itai,

>I [mistakenly] thought that all database operations between BEGIN
>TRANSACTION and END TRANSACTION are executed only if/when END
>TRANSACTION is reached.

That might be the case, I have never used Transactions in Xbase++ and the 
docs are not very specific, and there aren't any examples. But each Begin 
Transaction should have a corresponding End Transaction or RollBack to be 
closed. The docs don't say what specifically happens in case of an error 
before either End Transaction or RollBack is executed, but I would assume 
that no data is committed.

>Does ROLLBACK work with direct FieldPut() writing into DBF table?

Transactions and Rollback should work with any database access. But as 
those commands are pre-processed to DacSession() methods, they will have 
absolutely no effect, unless you actually use a DacSession().

Also, when using the commands (as opposed to the methods directly), the 
selected DacSession() will be based on the current WorkArea, which might 
not be the default session or the ones your (nArea1) and (nArea2) are 
actually connected with.

So, be sure that you a) use a DacSession() to open your WorkAreas and 
b) make sure that the Current WorkArea is opened by the same DacSession() 
when those commands are executed.

A safer way to code this, might be:

LOCAL bError
LOCAL cConnect := "..."
LOCAL oSession := DacSession():New(cConnect)
[...]
DbUseArea(.t., oSession, cTable1, cAlias1, .f., .f.)
DbUseArea(.t., oSession, cTable2, cAlias2, .f., .f.)
[...]
bError := ErrorBlock({|e| Break(e)})
BEGIN SEQUENCE
   oSession:BeginTransaction()
   (cAlias1)->(DbAppend())
   (cAlias1)->(FieldPut(1, xVal1))
   (cAlias1)->(FieldPut(2, xVal21))
   (cAlias2)->(DbAppend()) 
   (cAlias2)->(FieldPut(1, xVal1))
   (cAlias2)->(FieldPut(2, xVal21))
   oSession:CommitTransaction()
RECOVER
   ErrorBlock(bError)
   oSession:RollBackTransaction()
END SEQUENCE
ErrorBlock(bError)

As your example code didn't do any unlocking, I assume that the tables were 
opened exclusive.

Hope that helps,

Andreas

Andreas Gehrs-Pahl
Absolute Software, LLC

phone: (989) 723-9927
email: Andreas@AbsoluteSoftwareLLC.com
web:   http://www.AbsoluteSoftwareLLC.com
[F]:   https://www.facebook.com/AbsoluteSoftwareLLC
Jorge L. BorlandoRe: BEGIN TRANSACTION-END TRANSACTION does not work.
on Mon, 23 Dec 2019 12:52:30 -0300