<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Hibernate Sessions in Two Tier Rich Client Applications</title>
	<atom:link href="http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Thu, 02 Sep 2010 08:13:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-411</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Tue, 23 Mar 2010 19:00:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-411</guid>
		<description>Hi Vlad,
Your assessment of validation unique constraints is absolutely correct. And for this reason I strongly recommend the use of database constraints, which do essentially what you describe with pretty good performance and very well tested.

Yet &#039;enforcing&#039; this kind of constraint in the gui, without using the locking catches many cases of constraint violation, which would otherwise only get caught by the database, rendering the session unusable. 

There is only a small gap left, for another user entering the same value at almost the same time and the database constraint takes care of that.

The performance cost of this approach is very limited, since we only use a select on an indexed column and no locking. 

So for us it was a nice compromise between performance, ease of implementation and usability.</description>
		<content:encoded><![CDATA[<p>Hi Vlad,<br />
Your assessment of validation unique constraints is absolutely correct. And for this reason I strongly recommend the use of database constraints, which do essentially what you describe with pretty good performance and very well tested.</p>
<p>Yet &#8216;enforcing&#8217; this kind of constraint in the gui, without using the locking catches many cases of constraint violation, which would otherwise only get caught by the database, rendering the session unusable. </p>
<p>There is only a small gap left, for another user entering the same value at almost the same time and the database constraint takes care of that.</p>
<p>The performance cost of this approach is very limited, since we only use a select on an indexed column and no locking. </p>
<p>So for us it was a nice compromise between performance, ease of implementation and usability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vlad Sadilovskiy</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-410</link>
		<dc:creator>Vlad Sadilovskiy</dc:creator>
		<pubDate>Tue, 23 Mar 2010 18:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-410</guid>
		<description>Hi. 

I wanted to say that validating some type of constraints is not possible even conceptually in the application layer without locking tables that are involved in such validation. Consider unique key validation. The only way that guaranties uniqueness and would not throw a unique constraint violation is to do the following sequence of actions. 1. Lock the table from modifications 2. Query this table for duplicates of an incoming data. 3. If no duplicates found, insert incoming data. 4. Unlock the table or commit transaction. This is workable, but in my opinion cannot be a solution for concurrent multi-user enterprise level application.

Any other algorithm may render the assumptions of querying for duplicates invalid at the time of the insert.</description>
		<content:encoded><![CDATA[<p>Hi. </p>
<p>I wanted to say that validating some type of constraints is not possible even conceptually in the application layer without locking tables that are involved in such validation. Consider unique key validation. The only way that guaranties uniqueness and would not throw a unique constraint violation is to do the following sequence of actions. 1. Lock the table from modifications 2. Query this table for duplicates of an incoming data. 3. If no duplicates found, insert incoming data. 4. Unlock the table or commit transaction. This is workable, but in my opinion cannot be a solution for concurrent multi-user enterprise level application.</p>
<p>Any other algorithm may render the assumptions of querying for duplicates invalid at the time of the insert.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-253</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Wed, 29 Jul 2009 19:10:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-253</guid>
		<description>Hi Milan,
I&#039;d be super carefull with this approach. I would love if it works, but it will always be without support of the hibernate team, so if it failes at some point you will be on your own completely. One case which I would expect problems with is: how does it handle the dirty state of objects that already got flushed successfully? Is it set to &#039;not dirty&#039; as it probably should? What if the sql exception happens on an update that happens in mutiple steps, which might be the case when manipulating references.

If you want to go down this road I&#039;d check the source code, write some tests and then propose a patch for the hibernate team to make it an official feature. This way you know (to some extend) it works, and it will get some support from the hibernate team and the community as well.</description>
		<content:encoded><![CDATA[<p>Hi Milan,<br />
I&#8217;d be super carefull with this approach. I would love if it works, but it will always be without support of the hibernate team, so if it failes at some point you will be on your own completely. One case which I would expect problems with is: how does it handle the dirty state of objects that already got flushed successfully? Is it set to &#8216;not dirty&#8217; as it probably should? What if the sql exception happens on an update that happens in mutiple steps, which might be the case when manipulating references.</p>
<p>If you want to go down this road I&#8217;d check the source code, write some tests and then propose a patch for the hibernate team to make it an official feature. This way you know (to some extend) it works, and it will get some support from the hibernate team and the community as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Milan</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-252</link>
		<dc:creator>Milan</dc:creator>
		<pubDate>Mon, 20 Jul 2009 21:12:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-252</guid>
		<description>Hi Jens,

Regarding the constraints validation in the client, that is definitely the best approach when possible. However there are certain constraints that are more convenient to validate in DB or they perform better there, such as Unique Key, or complex business rules implemented in DB triggers.

I did some experimenting with the error handling and found out the session does not die after a SQL constraint exception (at least I could not make it die). If you handle exception and subsequently fix the data that caused the constraint to fail, everything works properly at the end. You can have multiple DML operations and fail only some of them, commit or rollback at the end (intentionally), etc.

So here is my idea about how to apply this to user interface. Trap every focus/selection change event that causes the underlying data context to change, and flush the session each time. Catch the error, display it (in a dialog) and do not allow changing the selection until the error is corrected (or the changes are rolled back). I successfully implemented a simple prototype that follows this approach. The biggest hurdle for me is to find proper events where to flush the session. (I am using SWT and it is not working as I would expect it.)</description>
		<content:encoded><![CDATA[<p>Hi Jens,</p>
<p>Regarding the constraints validation in the client, that is definitely the best approach when possible. However there are certain constraints that are more convenient to validate in DB or they perform better there, such as Unique Key, or complex business rules implemented in DB triggers.</p>
<p>I did some experimenting with the error handling and found out the session does not die after a SQL constraint exception (at least I could not make it die). If you handle exception and subsequently fix the data that caused the constraint to fail, everything works properly at the end. You can have multiple DML operations and fail only some of them, commit or rollback at the end (intentionally), etc.</p>
<p>So here is my idea about how to apply this to user interface. Trap every focus/selection change event that causes the underlying data context to change, and flush the session each time. Catch the error, display it (in a dialog) and do not allow changing the selection until the error is corrected (or the changes are rolled back). I successfully implemented a simple prototype that follows this approach. The biggest hurdle for me is to find proper events where to flush the session. (I am using SWT and it is not working as I would expect it.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-223</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Wed, 03 Jun 2009 16:55:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-223</guid>
		<description>Hi Milan,
you are absolutely right, this is a problem that needs consideration.

I absolutely recommend to validate all input against the constraints before flushing the session. In the project on which this article is based we basically used a NakedObject appraoch (http://en.wikipedia.org/wiki/Naked_objects) although we didn&#039;t know it at that time. So we actually used the constraints as defined in the hibernate mapping to create checks/special UI control in the UI layer. So for Foreign Keys one only could choose by picking from a list, other values where checked using hibernate validation.

If everything fails and you actually get a sql exception due to a constraint violation the session is dead, which sucks, but is seldom.

An alternative approach is to edit data in special objects and then copy the content of those objects into the session objects. This results in much shorter session live time (which probably is a good thing), and of course you could recover more easily after an exception. But you need to create a seperate set of objects (possibly classes), which makes life even more complecated then it is already. ...

As a German saying goes: You have to die one death</description>
		<content:encoded><![CDATA[<p>Hi Milan,<br />
you are absolutely right, this is a problem that needs consideration.</p>
<p>I absolutely recommend to validate all input against the constraints before flushing the session. In the project on which this article is based we basically used a NakedObject appraoch (<a href="http://en.wikipedia.org/wiki/Naked_objects">http://en.wikipedia.org/wiki/Naked_objects</a>) although we didn&#8217;t know it at that time. So we actually used the constraints as defined in the hibernate mapping to create checks/special UI control in the UI layer. So for Foreign Keys one only could choose by picking from a list, other values where checked using hibernate validation.</p>
<p>If everything fails and you actually get a sql exception due to a constraint violation the session is dead, which sucks, but is seldom.</p>
<p>An alternative approach is to edit data in special objects and then copy the content of those objects into the session objects. This results in much shorter session live time (which probably is a good thing), and of course you could recover more easily after an exception. But you need to create a seperate set of objects (possibly classes), which makes life even more complecated then it is already. &#8230;</p>
<p>As a German saying goes: You have to die one death</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Milan</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-222</link>
		<dc:creator>Milan</dc:creator>
		<pubDate>Wed, 03 Jun 2009 16:44:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-222</guid>
		<description>Excellent article!

Here is my concern: how to handle database errors/constraint violations. The scenario is as follows. User changes several records in a grid or in different parts of one form (frame). When user saves the changes, hibernate flushes all changes to the database. Some of the changes are successful and some of them fail DB constraints. Ideally, you would want to navigate to the UI element that caused the constraint failure, and give the user a chance to fix the error, while preserving the valid changes. 

Here is an excerpt from Hibernate documentation: &quot;If the Session throws an exception (including any SQLException), you should immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block.&quot;

If there are multiple failures, it gets even more complicated.

Your thoughts on the error handling subject would be much appreciated.</description>
		<content:encoded><![CDATA[<p>Excellent article!</p>
<p>Here is my concern: how to handle database errors/constraint violations. The scenario is as follows. User changes several records in a grid or in different parts of one form (frame). When user saves the changes, hibernate flushes all changes to the database. Some of the changes are successful and some of them fail DB constraints. Ideally, you would want to navigate to the UI element that caused the constraint failure, and give the user a chance to fix the error, while preserving the valid changes. </p>
<p>Here is an excerpt from Hibernate documentation: &#8220;If the Session throws an exception (including any SQLException), you should immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block.&#8221;</p>
<p>If there are multiple failures, it gets even more complicated.</p>
<p>Your thoughts on the error handling subject would be much appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sola</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-206</link>
		<dc:creator>sola</dc:creator>
		<pubDate>Wed, 27 May 2009 15:27:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-206</guid>
		<description>Great article!

Of course rich client applications have a lot of other problems like this:
- Database connection needs to get through firewalls
- The database connection can be easily broken over the Internet (we use PostgreSQL and have to implement reconnection functionality because the connection so often get lost on long distance sessions)

I really like rich clients but the ORM frameworks most often simply don&#039;t consider the extra needs of these kinds of apps which makes them hard to develop.

We also opted for the one Session for one Frame approach (or something very similar).</description>
		<content:encoded><![CDATA[<p>Great article!</p>
<p>Of course rich client applications have a lot of other problems like this:<br />
- Database connection needs to get through firewalls<br />
- The database connection can be easily broken over the Internet (we use PostgreSQL and have to implement reconnection functionality because the connection so often get lost on long distance sessions)</p>
<p>I really like rich clients but the ORM frameworks most often simply don&#8217;t consider the extra needs of these kinds of apps which makes them hard to develop.</p>
<p>We also opted for the one Session for one Frame approach (or something very similar).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-154</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Sat, 28 Mar 2009 20:13:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-154</guid>
		<description>Hi Aappddeevv,
well I just added Orchestra (http://myfaces.apache.org/orchestra/) to the many thing I&#039;d like to investigate. It sounds interesting in deed. Yet so far I fail to see how one could specify the scope of a conversation in an easy way. Do you have more detailed sources for that?</description>
		<content:encoded><![CDATA[<p>Hi Aappddeevv,<br />
well I just added Orchestra (<a href="http://myfaces.apache.org/orchestra/">http://myfaces.apache.org/orchestra/</a>) to the many thing I&#8217;d like to investigate. It sounds interesting in deed. Yet so far I fail to see how one could specify the scope of a conversation in an easy way. Do you have more detailed sources for that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aappddeevv</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-153</link>
		<dc:creator>aappddeevv</dc:creator>
		<pubDate>Sat, 28 Mar 2009 16:22:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-153</guid>
		<description>You can have a session stay open within a RCP based on the lifetime of the unit of work which is typically an editor screen of some sort while still having different session patterns on other editors windows in the same thread by using orchestra and spring. By placing DAOs or services into a special scope that intercepts all methods on those DAOs and swaps out the session on the thread and then replaces it after the method complete automatically using orchestra. Orchestra is from a myfaces implementation. This makes conversational control placed into the scope versus using program code. This is essentially what jboss&#039;s seam does. 

In the worse case you can just create your DAO objects with the long-lived session object directly and maintain the session object in your editor state somewhere. Then just ensure that all other DAOs that are called use that same session by initializing them with it as well. This approach is harder but also doable.

The only other way is to do the second approach via code instead of a container which makes configuration more laborious but is what many RCPs have done over the years.

Supposedly the new JSR 299 (or something like that) will address conversational scope and should apply to RCPs.</description>
		<content:encoded><![CDATA[<p>You can have a session stay open within a RCP based on the lifetime of the unit of work which is typically an editor screen of some sort while still having different session patterns on other editors windows in the same thread by using orchestra and spring. By placing DAOs or services into a special scope that intercepts all methods on those DAOs and swaps out the session on the thread and then replaces it after the method complete automatically using orchestra. Orchestra is from a myfaces implementation. This makes conversational control placed into the scope versus using program code. This is essentially what jboss&#8217;s seam does. </p>
<p>In the worse case you can just create your DAO objects with the long-lived session object directly and maintain the session object in your editor state somewhere. Then just ensure that all other DAOs that are called use that same session by initializing them with it as well. This approach is harder but also doable.</p>
<p>The only other way is to do the second approach via code instead of a container which makes configuration more laborious but is what many RCPs have done over the years.</p>
<p>Supposedly the new JSR 299 (or something like that) will address conversational scope and should apply to RCPs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-145</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Thu, 05 Mar 2009 01:43:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-145</guid>
		<description>We are trying to write a rich application using ZK framework http://www.zkoss.org/ where the rich client applications run as a session variable on web  server. So in our case we also have to consider sessions maintained across multiple http request/responses - add yet another layer of complexity.....</description>
		<content:encoded><![CDATA[<p>We are trying to write a rich application using ZK framework <a href="http://www.zkoss.org/">http://www.zkoss.org/</a> where the rich client applications run as a session variable on web  server. So in our case we also have to consider sessions maintained across multiple http request/responses &#8211; add yet another layer of complexity&#8230;..</p>
]]></content:encoded>
	</item>
</channel>
</rss>
