I am calling a statefull session bean from a servlet. The transaction setting of the method is "required".
The problem I am having is that a user can click on a link multiple times, causing the same method on the same statefull bean to be called multiple times. I sometimes get the message "a bean cannot be used in one transaction and then in another" because the methods are called simultaneous from different threads.
Can anyone explain how to prevent this or what transaction mechanisms I should use.
You should probably synchronize on the HttpSession object within your servlet. This will prevent it happening.
However, you need to know how the HttpSession is handled if your server is in a cluster. There are two possibilities...
1) It lives on one server and all your requests are routed to that server (server affine approach)
2) It is persisted into a back end store and therefore could be on multiple servers.
If it's (2) then this won't help.
On the other hand, if you only have one server then this will work (Also, you could synchronize around the bean remote interface just as well.)