hi
i created an explicite transaction which contains 2 update statements
both of these two update statements act on a single table with the name of
"TABLE1".
these table has two column "name varchar(20)" & "account (int)".
this table also has a check constraint on the "account" column which resric
ts
the value of this column to be greater than 100.
here is the table before execting the transaction:
<<
NAME ACCOUNT
' ali ' 140
' nima ' 200
here is the code of my transaction:
<<
begin transaction
update table1 set account=account-50 where name='ali'
update table1 set account=account+50 where name='esi'
commit transaction
and this is table1 after executing the transaction:
<<
NAME ACCOUNT
' ali ' 140
' nima ' 250
if the value of the "account" column be for example " 140" before executing
the
transaction , with executing the transaction a check constraint violation
happens
and the first update statement does not affect on the table but the second
update statement affect permanently on the table.
alltransactions are "ATOMIC" .Either all the satements inside them should
perform or none of them. with executing this transaction the "CONSISTANCY"
feature off transactions is ignored too.(140+200 before execution) &
(140+250 after execution).
wwwwhhhhhhhhhhhhhhyyyyyyyyyyyyyyyy'''The problem is that you don't have any error handling. A constraint
violation does not terminate a batch or a transaction, it only terminates
the offending statement and sets @.@.ERROR. You must always, Always, ALWAYS
check @.@.ERROR after every DML statement (INSERT, UPDATE, DELETE) and every
stored procedure call. It is also possible for a transaction to be rolled
back when an error occurs, but for the batch to continue executing. That
means that statements following an error are no longer protected by a
transaction! Failing to include error handling defeats the purpose of
transactions.
There is a statement, SET XACT_ABORT, which is supposed to cause every
transaction to be rolled back and terminated whenever an error occurs, but I
don't rely on it. I use @.@.ERROR and @.@.ROWCOUNT because that gives me more
control over the error recovery process.
"pooyan_pdm" <pooyan_pdm@.discussions.microsoft.com> wrote in message
news:3C02A013-830C-42C7-AB75-C53348528FDA@.microsoft.com...
> hi
> i created an explicite transaction which contains 2 update statements
> both of these two update statements act on a single table with the name of
> "TABLE1".
> these table has two column "name varchar(20)" & "account (int)".
> this table also has a check constraint on the "account" column which
resricts
> the value of this column to be greater than 100.
> here is the table before execting the transaction:
> <<
> NAME ACCOUNT
>
> ' ali ' 140
> ' nima ' 200
>
>
>
> here is the code of my transaction:
> <<
> begin transaction
> update table1 set account=account-50 where name='ali'
> update table1 set account=account+50 where name='esi'
> commit transaction
>
>
> and this is table1 after executing the transaction:
> <<
> NAME ACCOUNT
>
> ' ali ' 140
> ' nima ' 250
>
>
>
> if the value of the "account" column be for example " 140" before
executing
> the
> transaction , with executing the transaction a check constraint
violation
> happens
> and the first update statement does not affect on the table but the
second
> update statement affect permanently on the table.
> alltransactions are "ATOMIC" .Either all the satements inside them should
> perform or none of them. with executing this transaction the
"CONSISTANCY"
> feature off transactions is ignored too.(140+200 before execution) &
> (140+250 after execution).
>
> wwwwhhhhhhhhhhhhhhyyyyyyyyyyyyyyyy'''
>|||Brian
But there are some kind of error that terminate batches and @.@.ERROR does not
catch them
"Brian Selzer" <brian@.selzer-software.com> wrote in message
news:%23wF7tWdkFHA.2916@.TK2MSFTNGP14.phx.gbl...
> The problem is that you don't have any error handling. A constraint
> violation does not terminate a batch or a transaction, it only terminates
> the offending statement and sets @.@.ERROR. You must always, Always, ALWAYS
> check @.@.ERROR after every DML statement (INSERT, UPDATE, DELETE) and every
> stored procedure call. It is also possible for a transaction to be rolled
> back when an error occurs, but for the batch to continue executing. That
> means that statements following an error are no longer protected by a
> transaction! Failing to include error handling defeats the purpose of
> transactions.
> There is a statement, SET XACT_ABORT, which is supposed to cause every
> transaction to be rolled back and terminated whenever an error occurs, but
> I
> don't rely on it. I use @.@.ERROR and @.@.ROWCOUNT because that gives me more
> control over the error recovery process.
> "pooyan_pdm" <pooyan_pdm@.discussions.microsoft.com> wrote in message
> news:3C02A013-830C-42C7-AB75-C53348528FDA@.microsoft.com...
> resricts
> executing
> violation
> second
> "CONSISTANCY"
>|||You're absolutely right. It's important to keep that in mind. It's also
important to understand that the nature of error and transaction management
change within triggers. However, neither of these supplant the requirement
to manage errors that do not terminate a batch.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uylJrjekFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Brian
> But there are some kind of error that terminate batches and @.@.ERROR does
not
> catch them
>
>
> "Brian Selzer" <brian@.selzer-software.com> wrote in message
> news:%23wF7tWdkFHA.2916@.TK2MSFTNGP14.phx.gbl...
terminates
ALWAYS
every
rolled
That
but
more
should
>
Sunday, February 19, 2012
a problem with transactions
Labels:
act,
contains,
created,
database,
explicite,
hii,
microsoft,
mysql,
oracle,
server,
single,
sql,
statements,
statementsboth,
table,
transaction,
transactions,
update
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment