While the answer above is the standard answer given to the question, I do not agree (just as you do).
1. It is a mistake to assume that your session is in a consistent state, in the meaning of 'all selected data reflects its status at the beginning of the transaction'.
a) This kind of read consistency is normally not guaranteed by a database. Instead you get the data in the status of the time of the select, which might vary for data coming from different tables/selects
b) if configured to that effect, hibernate will release jdbc connections after a select as long as no dml is issued. That will break the 'consistent' status for sure
2. Ending up with the whole database in memory is a potential problem, but has nothing to do with the problem at hand. If one hits such an exception, the developer won't go "Oh no, I can't have that object, my session is closed, I guess I have to use random data". No she will either keep the original session open or open a new one to manually load the required data. The Complete Database in Session Problem is one of partioning your object graph and evicting objects from your first level cache aka session. Normaly by closing the session.
As I see it the whole problem is one of perspective:
Hibernate is largely used in web applications. There the session handling is easy, and Exceptions as the one discussed occur when your Object was attached to a session from a previouse request. Your sure don't want to revive that session. So throwing an exception is a good thing.
In a rich client (two tier) scenario this is way different. The way sessions are handled/implemented is a serious problem, although solutions exist non will make you really happy. Automatic reattaching would be a great help and really not that much of a problem (at least no one showed me one).
Jens
_________________ Please rate useful posts.
Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"
|