These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Triggers and Transaction context
PostPosted: Thu Oct 27, 2005 1:11 pm 
Hi, I'm running into a situation:

Code:
trx = session.BeginTransaction()
acc = session.Load(GetType(FundAcc), acc_id)
acc.LastUpdate = DateTime.Now
sess.Update(acc)
sess.Flush() 'acc Locked

request = session.Load(GetType(FundRequest), request_id)
request.LastUpdate = DateTime.Now
sess.Update(request)
sess.Flush() 'request locked

STEP 1: check and validate acc has sufficient funds
If (acc.Balance >= request.Amount) Then

  STEP 2: update request status
  request.Status = Status.APPROVED
  sess.Update(request)
  sess.Flush()

  STEP 3: approve request - update acc balance
  acc.Balance = acc.Balance - request.Amount
  sess.Update(acc)
  sess.Flush()
 
End If
trx.Commit()


This works fine. But one of our DBA insisted on STEP 3 being done by UPDATE trigger - and the code became:

Code:
trx = session.BeginTransaction()
acc = session.Load(GetType(FundAcc), acc_id)
acc.LastUpdate = DateTime.Now
sess.Update(acc)
sess.Flush() 'acc Locked

request = session.Load(GetType(FundRequest), request_id)
request.LastUpdate = DateTime.Now
sess.Update(request)
sess.Flush() 'request locked

STEP 1: check and validate acc has sufficient funds
If (acc.Balance >= request.Amount) Then

  STEP 2: update request status
  request.Status = Status.APPROVED
  sess.Update(request)
  sess.Flush()
End If
trx.Commit()


The problem is, the trigger is trying to update "acc", which is locked during the duration of the transaction. Is there anyway to get around this? Or is UPDATE TRIGGER a bad idea (TRIGGER isn't executed in the same transaction context)? What I found is that the UPDATE TRIGGER was just never fired.

I assume it was never fired because it's outside the transaction and was therefore blocked - correct me if I'm wrong. Under what context is TRIGGER being executed? However, I'm seeing conflicting information:

A trigger is part of the transaction started by the statement that fired it. Thus, if the trigger fails the firing statement fails and is rolled back.

If this is true, why the hell my UPDATE TRIGGER never updated "acc"??
Reference: http://www.dotnetjohn.com/articles.aspx?articleid=133

which lead me to thinking:
1. are triggers fired on Flush? Or Commit?
2. When Flushed (but not yet committed), updates propagated to database, yet?

Thanks in advance.


Top
  
 
 Post subject: ... UPDATE TRIGGER not fired ...
PostPosted: Thu Oct 27, 2005 1:14 pm 
could it be that UPDATE TRIGGER not fired because changes have been Flushed but NOT yet commmitted?


Top
  
 
 Post subject: Trigger fired BEFORE TRAN COMMITTED
PostPosted: Thu Oct 27, 2005 2:03 pm 
TRIGGERS fired before transaction committed:
Code:
-- SETUP SCRIPT
CREATE TRIGGER trg_update_authors
ON authors
FOR
UPDATE
AS
PRINT 'Firing trigger'
UPDATE authors
   SET authors.au_lname='UPDATE FROM TRIGGER'
   WHERE authors.au_id IN (SELECT INS.au_id FROM inserted INS)


GO

-- SIMPLE TEST
Code:
BEGIN TRAN
UPDATE authors
  SET au_lname='White'
  WHERE au_id='172-32-1176'
PRINT 'Updated'
COMMIT TRAN
PRINT 'TRAN committed'



Result:
Quote:
Firing trigger
(1 row(s) affected)
Updated
TRAN committed


Conclusion: Trigger fired BEFORE transaction was committed.

So, can I say that Trigger should fired at "Flush" under the same transaction context?


Top
  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 2:05 pm 
Quote:
So, can I say that Trigger should fired at (AFTER) "Flush" under the same transaction context?


If so, how come UPDATE TRIGGER was *** apparently *** "blocked"?


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.