1) How does the container decide either to "passivate" or "to time out and kill a bean" when a stateful bean is in method ready state?
'Timeout/killing a bean' is related to inactivity on part of the bean, only a time factor is involved in this. 'Passivation' is primarily a memory related thing though there is a time factor involved in this too. Consider a server that is running an
EJB application with a bunch of beans. As more and more stateful beans get created, the server's memory gets used up. At some point if the server finds itself short of memory to handle requests for new beans, it will look through the existing beans and pick the ones that have been most inactive and passivate them to free up memory. Different servers provide different options in configuring the passivation criteria.
Besides this the server will also be monitoring the beans to see which ones have been inactive for a period greater than the configured time-out period(say 20 minutes). Clients of beans that have been idle for more than this period are considred to be no longer using the application and so such beans will be killed. The server can kill beans that are already passivated too.
2) I understand it is container's sole decision to remove/kill a stateless bean. So When dealing with a "stateless session bean" should "remove()" be called by the client? I dont see remove() being called in the HFEJB examples. What exactly (and always) happens when the client calls remove() while dealing with a session beans?
Stateless session beans do not get a remove call even though the client may actually call the remove method. For statefull session beans remove is invoked when the client calls it and any code written here is executed.
3) Should the database connections etc be closed before ejbpassivate completes
ejbPassivate is called before the server passivates the bean, and the bean should be brought to a passivable state before it can be passivated. You would write code in the ejbPassivate to take care of non passivable data, such as database connections.