0

Desktop init & cleanup

asked 2011-08-25 07:44:42 +0800

vandoor gravatar image vandoor
9 1

updated 2011-08-25 07:46:31 +0800

Hello,

I have a DesktopCleanup-Init class in my application (Zk 5.0.7.1).

public class MyInitCleanup implements DesktopCleanup, DesktopInit {

    public void init(Desktop desktop, Object request) throws Exception {
    // ...
    }

    public void cleanup(Desktop desktop) throws Exception {
    // ...
    }
}

When i press F5 in IE9, current desktop's cleanup is invoked before the new desktop's initialisation but is not the same in FF and Chrome.

I must do some actions in clean up before the creation of the new desktop.
Can you help me please?

Thanks,
vandoor

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2011-08-25 09:53:25 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

@Vandoor: so in FF and Chrome new desktop init is invoked before the current desktop cleanup. The "cleanup" is triggered by the Javascript "onunload" event, which sends "rmdesktop" message to the server. I have observed the communication between FF browser and server (with Firebug) and the "rmdesktop" message is sent after the new URL request. So I am afraid the ZK cannot do anything with this. It is a browser behaviour.

I think you should not rely on cleanup and init order, what about if the user has more tabs open with the same URL, i.e. more desktops open? Maybe better try to redesign your application.

link publish delete flag offensive edit

answered 2011-08-26 02:08:09 +0800

vandoor gravatar image vandoor
9 1

@xmedeko Thanks for observations!

Is it possible to stop F5 event propagation?
Is it possible to trap F5 event?

Redesign application is not possible.
The problem : I have to lock some entities. A the beginning, i worked with the session but i found bugs when user uses multiple browser tabs. The same entity was editable in 2 tabs --> Integrity problem!

Working with the desktop was the solution. Application works correctly except if the user press F5 or history button.
Any idea?

Thanks,
vandoor

link publish delete flag offensive edit

answered 2011-08-26 04:19:15 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

The database locking is definitely not the good way for the web application. Try optimistic locking instead of database lock - then the user can open edit form in more tabs without corrupting data integrity. Sometimes you have to do more data integrity checks manually on the form submit if the domain model is complicated.

link publish delete flag offensive edit

answered 2011-08-26 05:15:25 +0800

Bobzk gravatar image Bobzk
444 1 8

The sensible way to update or edit data in a multi-user, multi-tab, web access environment is the following -

1. Your data needs some unique column. A counter or a time-stamp for example.
2. Read your data (but DO NOT lock it).
3. Save the unique column value.
4. Display the data to your user.

wait for user to do changes and click the "update" button.

5. re-read your data (with locking)
6. compare unique column (timestamps or count) from 3 above with the current value.
7. If they are NOT the same, inform the user that the data has already changed and he/she needs to try again, unlock the data and start again at 2 above.
8. Only if the unique column value IS THE SAME, update the data AND you must update the unique column.
9. Unlock the data.

The important things to note are that between 4 and 5 above there is the possibility of a long wait for user input (or in fact the user goes away and never inputs). But as we don't lock the data it caused no problem.

Between 5 and 9 above there are only a few milliseconds (with no wait for user interaction) thus ensuring data is locked for as short a duration as possible.

The above is the general way of doing it. Many frameworks attempt to do this automatically for you. If you are doing all the work yourself without a framework then this really is the way to go. More work but a safe, secure, consistent and reliable application. And once you get into it, it becomes the natural way of doing it.

link publish delete flag offensive edit

answered 2011-08-26 07:15:48 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

@Bobzk: This technique is called the optimistic locking. JPA/Hibernate has a default support for this. You just have to create a @Version column (the optimistic lock) in a table. It there is a conflict during the entity save, the JPA/Hibernate throws "OptimisticLockingException". You do not need DB locking in 5. - 9., it's simpler to do it in one DB transaction. And that's exactly what JPA/Hibernate does.

On my last project, I have created a handler for the OptimisticLockingException which has showed a warning to a user and asked her if she want to reload the data. Something like "The form cannot be saved. Data has changed while you have edited the form. Would you like to reload the form with the fresh DB data?" So i had a simple general code to handle OptimisticLockingException in all forms.

link publish delete flag offensive edit

answered 2011-08-26 07:48:46 +0800

Bobzk gravatar image Bobzk
444 1 8

xmedeko,

Yes, as I said various frameworks do (or try to do) it for you. I was simply trying to explaining what is in effect the logic behind all of these frameworks - what happens in the background.. You obviously understand the concept and correctly design/code/handle it. In fact if you understand the concept it becomes second nature to design this way.

I have spent many hours explaining the concept to (even some quite experienced) developers who just can't see why they need to do it. Why they can't just update and/or lock records at will.

If I had a job vacancy available, you would be the sort of person i would employ [(:-)

link publish delete flag offensive edit

answered 2011-08-29 04:08:32 +0800

vandoor gravatar image vandoor
9 1

Hello,

@xmedeko : My lock system is design to prevent any LockingException. I don't want that user works, refreshes his screen and works again! There is a risk that another LockException will be throw.

My problem was url in the browser. I simply remove parameters in the url, the browser refresh the screen on F5 and redirects on the main page instead of the form page (which lock the datas). That's the solution i found!

+++
vandoor

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-25 07:44:42 +0800

Seen: 363 times

Last updated: Aug 29 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More