Is it recommended to flush everytime when insert, update or delete operation is performed?
In my web application, I currently use one transaction per user request, where all insert/update/delete/query are done before comitting the transaction and redirect to jsp. However, in one of my unique scenarios, I noticed the newly inserted row doesn't have an ID value assigned to it yet (assigned as "native" since my primary key uses DB identity) after I queried all rows.
Where have you read that it is recommended to flush after every CRUD operation? That will cause more network and database traffic, which slows performance a little bit.
If user A inserts a new record without flushing it, and user B queries the record in split second, wouldn't user B acquire that record with null ID assigned to it?
Originally posted by Choon-Chern Lim: If user A inserts a new record without flushing it, and user B queries the record in split second, wouldn't user B acquire that record with null ID assigned to it?
How would user B manage to query for a record that doesn't yet exist?
However, in one of my unique scenarios, I noticed the newly inserted row doesn't have an ID value assigned to it yet (assigned as "native" since my primary key uses DB identity) after I queried all rows.
You have an entity defined that allows a null primary key? How did your RDBMS allow you to get away with that? I ask since this violates one of the rules of that defines a PK, namely that it is unique, unchanging and not null. [ August 09, 2006: Message edited by: Paul Sturrock ]
One of the downsides of allowing the database to generate your id is you will have to commit the transaction in order to allow it to do this. So in the context of your application, yes you will have to flush after every insert. You won't have to flush after every update though, since an update will never change the PK (if it does, its really an insert). Nor should you need to flush after a delete, since the PK is meaninless after a delete.
Choon-Chern Lim
Ranch Hand
Joined: Aug 29, 2005
Posts: 74
posted
0
Thanks for the explanation, Paul.... it makes more sense to me now.