Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make the trigger work in hibernate?

 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I am doing in one transaction.

1. insert/update on table A
2. a trigger on table A inserts/updates table B , obviously no java code for this , done automatically.
3. insert on table C.

My requirement is to make sure the correct order of insert/update in DB as I have mentioned above.
But I believe(and I am not sure) trigger will only be called once the transaction is complete i.e. after step 3 is done. In this case the order will be 1 , 3 , 2 .
How can I make the trigger called before doing the step 3?
Please suggest.
My attempt : I wrote a line

just before the DAO layer of step 3. But it didnt work. Also I saw the order 1,3,2 is also not guaranteed as on some machine I am getting the correct order as 1,2, 3 without any change on one machine. Does it also have anything to do with server or db version?
My requirement is to achieve a guaranteed order of 1,2, 3.
Thanks a lot in advance.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Did you try turning the auto commit on programmatically.
2. Did you try using nested transactions and commiting the inner transaction before you do the third operation?
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Kumarr wrote:1. Did you try turning the auto commit on programmatically.
2. Did you try using nested transactions and commiting the inner transaction before you do the third operation?



How to do the first thing that you said?
Regarding point 2 , it is not possible to commit the inner transaction as the transaction handling is done from different layer. it is like layer 1 handles transaction and I am at layer 2 and layer 3 is the DAO layer.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware if we can do it via code. Just a thought. The problem you have is intermittent flushing during transaction and it is common.
Check these links:

3.8.3. Current Session context management with JTA in here http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional-jndi

and also check this setting, hibernate.transaction.flush_before_completion
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
via code, try this setting of flush modes.
FlushMode
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Kumarr wrote:1. Did you try turning the auto commit on programmatically.
2. Did you try using nested transactions and commiting the inner transaction before you do the third operation?


Personally, I would not use auto commit. It just makes things less obvious.

A nested transaction can't be committed before the parent transaction - that's not how transactions work. However, regardless of whether the transaction has been committed the trigger will still fire and any changes made will be part of the same undo block of the transaction that causes the trigger to fire.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:
A nested transaction can't be committed before the parent transaction - that's not how transactions work.

- Very true. Assuming you don't roll back the outer transaction and commit it, won't it guarantee the order OP mentioned.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic