To Persist or Not Persist. That is the question. When is the appropriate time to persist to a database?
I'm working 开发者_C百科on a new web application to file county tax returns. So far, I'm developing the logic so that the data is collected in several forms, where it stays persisted in memory until the user gets to the final step where they click save.
At that point, the data is saved to the database, all at once. My reasoning for this method is to allow the data to be loaded in a single transaction. Plus, I only need to use insert statements, not Insert if/else update
Which, if that transaction fails, I don't have orphaned and partial data to clean up.
But, now I'm starting to think I should save as soon as possible. Then add data to the DB as needed.
I am hoping this is not a Ford vs Chevy debate. I'm really asking an objective question, is there truly a correct approach to apply in most situations?
I'd go for your new approach also - save as often as possible - reasons for this:
- what happens when application goes off? (the user has to enter ALL the data again)
- what happens when the user starts filling the form, leaves for 2-3-4 hours and days and comes back? expired and deleted sessions and session variables - the user has to enter all the data again
- you have to check the validity of the data both ways - when you save to memory first and after to DB
- what happens when the user completes 5 forms, submits and you have an error in the first one? - you will put him to complete the form anyway.
- you have to check the data integrity in both cases.
I don't see any reasonable reason for NOT saving as often as possible
精彩评论