<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dumb Programmer</title>
	<atom:link href="http://sysin.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sysin.wordpress.com</link>
	<description>Trying to be smart :-)</description>
	<lastBuildDate>Wed, 11 May 2011 02:59:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sysin.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Dumb Programmer</title>
		<link>http://sysin.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sysin.wordpress.com/osd.xml" title="Dumb Programmer" />
	<atom:link rel='hub' href='http://sysin.wordpress.com/?pushpress=hub'/>
		<item>
		<title>2010 in review</title>
		<link>http://sysin.wordpress.com/2011/01/03/2010-in-review/</link>
		<comments>http://sysin.wordpress.com/2011/01/03/2010-in-review/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 08:21:55 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=133</guid>
		<description><![CDATA[The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here&#8217;s a high level summary of its overall blog health: The Blog-Health-o-Meter&#8482; reads Fresher than ever. Crunchy numbers A helper monkey made this abstract painting, inspired by your stats. The average container ship can carry about 4,500 containers. This blog [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=133&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[
<p>The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here&#8217;s a high level summary of its overall blog health:</p>
<p align="center"><img style="border:1px solid #ddd;background:#f5f5f5;padding:20px;" src="http://s0.wp.com/i/annual-recap/meter-healthy3.gif" width="250" height="183" alt="Healthy blog!"></p>
<p align="center">The <em>Blog-Health-o-Meter&trade;</em> reads Fresher than ever.</p>
<h2>Crunchy numbers</h2>
<div style="width:288px;float:right;border:1px solid #ddd;background:#fff;margin:0 0 1em 1em;padding:6px;">
<p>				<img src="http://s0.wp.com/i/annual-recap/abstract-stats-3.png" alt="Featured image" /><br />
				<br /><em>A helper monkey made this abstract painting, inspired by your stats.</em></p></div>
<p>The average container ship can carry about 4,500 containers.  This blog was viewed about <strong>15,000</strong> times in 2010.  If each view were a shipping container, your blog would have filled about 3 fully loaded ships.</p>
<p>
<p>In 2010, there were <strong>3</strong> new posts, growing the total archive of this blog to 14 posts.</p>
<p>The busiest day of the year was March 10th with <strong>108</strong> views. The most popular post that day was <a style="color:#08c;" href="http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/">Hibernate, Spring and multiple datasources (Part 1)</a>.</p>
<p></p>
<h2>Where did they come from?</h2>
<p>The top referring sites in 2010 were <strong>stackoverflow.com</strong>, <strong>en.wordpress.com</strong>, <strong>google.co.in</strong>, <strong>swik.net</strong>, and <strong>blog.smart-java.nl</strong>.</p>
<p>Some visitors came searching, mostly for <strong>hibernate multiple data sources</strong>, <strong>spring multiple datasource</strong>, <strong>spring hibernate multiple data sources</strong>, <strong>spring hibernate multiple datasources</strong>, and <strong>opensessioninviewfilter</strong>.</p>
<div style="clear:both;"></div>
<h2>Attractions in 2010</h2>
<p>These are the posts and pages that got the most views in 2010.</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">1</div>
<p>					<a style="margin-right:10px;" href="http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/">Hibernate, Spring and multiple datasources (Part 1)</a> <span style="color:#999;font-size:8pt;">March 2009</span><br />9 comments											</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">2</div>
<p>					<a style="margin-right:10px;" href="http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2/">Hibernate, Spring and multiple datasources (Part 2)</a> <span style="color:#999;font-size:8pt;">March 2009</span><br />4 comments											</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">3</div>
<p>					<a style="margin-right:10px;" href="http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3/">Hibernate, Spring and multiple datasources (Part 3)</a> <span style="color:#999;font-size:8pt;">March 2009</span><br />6 comments											</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">4</div>
<p>					<a style="margin-right:10px;" href="http://sysin.wordpress.com/2009/02/27/hibernate-inheritance-classcastexception-part-1/">Hibernate inheritance ClassCastException (Part 1)</a> <span style="color:#999;font-size:8pt;">February 2009</span><br />1 comment											</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">5</div>
<p>					<a style="margin-right:10px;" href="http://sysin.wordpress.com/2009/02/28/hibernate-inheritance-classcastexception-part-2/">Hibernate inheritance ClassCastException (Part 2)</a> <span style="color:#999;font-size:8pt;">February 2009</span>											</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/133/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=133&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2011/01/03/2010-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://s0.wp.com/i/annual-recap/meter-healthy3.gif" medium="image">
			<media:title type="html">Healthy blog!</media:title>
		</media:content>

		<media:content url="http://s0.wp.com/i/annual-recap/abstract-stats-3.png" medium="image">
			<media:title type="html">Featured image</media:title>
		</media:content>
	</item>
		<item>
		<title>Introduction to Domain Driven Design</title>
		<link>http://sysin.wordpress.com/2010/12/22/introduction-to-domain-driven-design/</link>
		<comments>http://sysin.wordpress.com/2010/12/22/introduction-to-domain-driven-design/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 11:25:25 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[aggregates]]></category>
		<category><![CDATA[domain driven design]]></category>
		<category><![CDATA[entities]]></category>
		<category><![CDATA[introduction to ddd]]></category>
		<category><![CDATA[introduction to domain driven design]]></category>
		<category><![CDATA[repositories]]></category>
		<category><![CDATA[repository pattern]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=129</guid>
		<description><![CDATA[A presentation that I prepared for a technical talk at my office. A very high level introduction to some of the concepts that DDD talk about http://sysin.files.wordpress.com/2011/05/introducetion-to-ddd.pptx<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=129&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A presentation that I prepared for a technical talk at my office. A very high level introduction to some of the concepts that DDD talk about</p>
<p><a href="http://sysin.files.wordpress.com/2011/05/introducetion-to-ddd.pptx">http://sysin.files.wordpress.com/2011/05/introducetion-to-ddd.pptx</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/129/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=129&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2010/12/22/introduction-to-domain-driven-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>DQGen &#8211; Dynamic Query Generation framework</title>
		<link>http://sysin.wordpress.com/2010/12/03/dqgen-dynamic-query-generation-framework/</link>
		<comments>http://sysin.wordpress.com/2010/12/03/dqgen-dynamic-query-generation-framework/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 14:49:00 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[adhoc report builder]]></category>
		<category><![CDATA[dynamic queries]]></category>
		<category><![CDATA[dynamic query generation]]></category>
		<category><![CDATA[dynamic query generation framework]]></category>
		<category><![CDATA[dynamic sql generation]]></category>
		<category><![CDATA[dynamic sql generation framework]]></category>
		<category><![CDATA[report builder]]></category>
		<category><![CDATA[report builder framework]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=125</guid>
		<description><![CDATA[I have been working on a new idea that would make dynamic query generation quite simpler and would be of much use in the reporting arena. The initial beta version (DQGen1.0) is just out. Checkout http://dynamicquerygen.sourceforge.net/ for more details<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=125&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been working on a new idea that would make dynamic query generation quite simpler and would be of much use in the reporting arena. The initial beta version (DQGen1.0) is just out.</p>
<p>Checkout <a href="http://dynamicquerygen.sourceforge.net/">http://dynamicquerygen.sourceforge.net/</a> for more details</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/125/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=125&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2010/12/03/dqgen-dynamic-query-generation-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Horizontally Partitioned Datasources</title>
		<link>http://sysin.wordpress.com/2010/08/09/horizontally-partitioned-datasources/</link>
		<comments>http://sysin.wordpress.com/2010/08/09/horizontally-partitioned-datasources/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 03:32:26 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[hibernate horizontally partitioned datasources performance memory]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=119</guid>
		<description><![CDATA[I have been receiving quite a few requests on using hibernate for horizontally partitioned databases and the performance issues it would bring in. FYI, I have successfully implemented a component on top of core for handling horizontally partitioned data sources. It&#8217;s been well tested and already running in our production servers without any issues. I&#8217;m [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=119&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been receiving quite a few requests on using hibernate for horizontally partitioned databases and the performance issues it would bring in. FYI, I have successfully implemented a component on top of core for handling horizontally partitioned data sources. It&#8217;s been well tested and already running in our production servers without any issues. I&#8217;m planning to make it open source and will post further details in my next post</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=119&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2010/08/09/horizontally-partitioned-datasources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Why Hibernate with Spring?</title>
		<link>http://sysin.wordpress.com/2009/03/07/why-hibernate-with-spring/</link>
		<comments>http://sysin.wordpress.com/2009/03/07/why-hibernate-with-spring/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 13:00:21 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[domain driven design]]></category>
		<category><![CDATA[Hibernate transaction manager]]></category>
		<category><![CDATA[HibernateTemplate]]></category>
		<category><![CDATA[transaction management]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=112</guid>
		<description><![CDATA[Spring has become an inevitable part of the middle tier these days. With its wide range of features, Spring is making the code development look easy and faster. I&#8217;m not here to discuss why use Spring in the middle tier but rather why should one integrate Hibernate with Spring. Of course if your application is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=112&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Spring has become an inevitable part of the middle tier these days. With its wide range of features, Spring is making the code development look easy and faster. I&#8217;m not here to discuss why use Spring in the middle tier but rather why should one integrate Hibernate with Spring. Of course if your application is smaller and is a non-transactional one, you can go with plain Hibernate and integrating with Spring in such applications is just an additional layer(overhead). </p>
<p>Spring comes with an inbuilt support for the some of the popular frameworks in the ORM world including Hibernate, JDO, TopLink, Ibatis and JPA. Spring provides a template for each of these frameworks and also a base Data Access Object which your DAO layer can extend. The Hibernate template can be used either directly in the application layer or in conjunction with the DAO. This template acts like a wrapper over the <em>Session</em> and abstracts all the session handling code from us. So how about transaction management? Spring provides different flavors of transaction management, you can use TransactionTemplate and define the transaction scope or use the HibernateTransactionManager to define the scope or annotations or AOP to define the scope. This means that the underlying orm is completely abstracted from the application layer and you can at any point switch to some other framework.</p>
<p>I don&#8217;t wanna repeat how to use these features with Hibernate (trying to follow DRY <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ) as there are hell lot of articles in the net. There&#8217;s nothing much in this post as the aim of this post is to just kick start a discussion on the much talked about Domain-Driven-Design (DDD). I&#8217;m in the process of coming up with an example for DDD using Hibernate and Spring in the middle tier. Keep reading my posts for updates. Thanks !!</p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/07/why-hibernate-with-springwhy-hibernate-with-spring/&amp;title=Why Hibernate with Spring?"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/07/why-hibernate-with-springwhy-hibernate-with-spring/&amp;title=Why Hibernate with Spring?&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/07/why-hibernate-with-springwhy-hibernate-with-spring/&amp;title=Why Hibernate with Spring?"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=112&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/07/why-hibernate-with-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
		<item>
		<title>Hibernate and Composite key associations</title>
		<link>http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/</link>
		<comments>http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 03:45:26 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[associations]]></category>
		<category><![CDATA[composite key]]></category>
		<category><![CDATA[composite primary key]]></category>
		<category><![CDATA[composite-id]]></category>
		<category><![CDATA[foreign key order]]></category>
		<category><![CDATA[key order]]></category>
		<category><![CDATA[legacy]]></category>
		<category><![CDATA[many-to-one]]></category>
		<category><![CDATA[one-to-many]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=105</guid>
		<description><![CDATA[Hibernate will fetch you good results when you follow the standards it insists. But there are cases where you can&#8217;t do that like migrating a legacy application to Hibernate. Your db model could be completely different from what hibernate expects (though hibernate does support legacy migration) and changing that may not be an easy task. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=105&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hibernate will fetch you good results when you follow the standards it insists. But there are cases where you can&#8217;t do that like migrating a legacy application to Hibernate. Your db model could be completely different from what hibernate expects (though hibernate does support legacy migration) and changing that may not be an easy task. For instance, the legacy tables might be having a composite primary key but Hibernate insists in having single column primary key.</p>
<p>One typical bug (user induced and not hibernate) that a beginner would face is with composite key associations. Say that you have a couple of tables linked by a foreign key, and the foreign key is the primary key of the second table. Now, while defining your many-to-one or one-to-many associations in the mapping file, you gotta be careful with the key ordering. The keys should be in the same order as that of what is defined in the composite-id of the second table&#8217;s mapping file. What hibernate does is, applies the values to the keys to the prepared statement in the order the keys are defined in the associations as long as the type is same.</p>
<p>Take a look at the below association mapping,</p>
<pre class="brush: xml;">
&lt;class name=&quot;Entity1&quot;&gt;
	&lt;id name=&quot;id1&quot;/&gt;
	&lt;property name=&quot;property1&quot; column=&quot;property1&quot; type=&quot;long&quot;/&gt;
	&lt;property name=&quot;property2&quot; column=&quot;property2&quot; type=&quot;long&quot;/&gt;
	....
	....
	&lt;many-to-one name=&quot;entity2&quot; class=&quot;Entity2&quot;&gt;
		&lt;column name=&quot;property1&quot;/&gt;
		&lt;column name=&quot;property2&quot;/&gt;
	&lt;/many-to-one&gt;
&lt;/class&gt;

&lt;class name=&quot;Entity2&quot;&gt;
	&lt;composite-id name=&quot;id1&quot;&gt;
		&lt;key-property name=&quot;property1&quot; column=&quot;property1&quot; type=&quot;long&quot;/&gt;
		&lt;key-property name=&quot;property2&quot; column=&quot;property2&quot; type=&quot;long&quot;/&gt;
	&lt;/composite-id&gt;
&lt;/class&gt;
</pre>
<p>This would work perfectly fine as the key ordering in the many-to-one is exactly same as the second entity. But when you change the order say, property2 as the first and property1 as second in the association, Hibernate subsitutes the value of property2 to the property1 and vice versa. The outcome is not an exception but rather an empty result set and its very very tough to debug this issue. Believe me I spent nearly a couple of days without getting any clue on whats happenning and had to debug line by line of Hibernate code to check what it&#8217;s doing internally. </p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/&amp;title=Hibernate and Composite key associations"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/&amp;title=Hibernate and Composite key associations&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/&amp;title=Hibernate and Composite key associations"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=105&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/06/hibernate-and-composite-key-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
		<item>
		<title>Hibernate, Spring and multiple datasources (Part 3)</title>
		<link>http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3/</link>
		<comments>http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 03:54:51 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[catalog]]></category>
		<category><![CDATA[CustomConnectionProvider]]></category>
		<category><![CDATA[dynamic datasource routing]]></category>
		<category><![CDATA[Hibernate template]]></category>
		<category><![CDATA[Hibernate transaction manager]]></category>
		<category><![CDATA[horizontal partitioning]]></category>
		<category><![CDATA[JTA]]></category>
		<category><![CDATA[LocalSessionFactoryBean]]></category>
		<category><![CDATA[multiple datasources]]></category>
		<category><![CDATA[multiple session factories]]></category>
		<category><![CDATA[shards]]></category>
		<category><![CDATA[threadlocal]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=96</guid>
		<description><![CDATA[So, how to use the same connection for all the partitions by switching the catalog? And before that why should one go for that? To answer the later, there are lots of advantages in going for that, You can reduce the number of connection pools The number of session factories can be limited as the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=96&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So, how to use the same connection for all the partitions by switching the catalog? And before that why should one go for that? To answer the later, there are lots of advantages in going for that,</p>
<ul>
<li>You can reduce the number of connection pools</li>
<li>The number of session factories can be limited as the pools have been reduced.</li>
</ul>
<p>The first thing we have to do is to make the DataSourceIdentifier return the catalog,</p>
<pre class="brush: java;">
public abstract class DataSourceIdentifier {

	protected String dataSourceId;
	protected String dataSourceCatalog;

	public String getDataSourceId() {
		if(dataSourceId == null) {
			resolve();
		}
		return dataSourceId;
	}

	public String getDataSourceCatalog() {
		if(dataSourceCatalog == null) {
			resolve();
		}
		return dataSourceCatalog;
	}

	// Add the partitioning logic here
	public abstract void resolve();
}
</pre>
<p>Then we have create one session factory (assuming that all partitions reside in the same db instance otherwise create one per db instance). We have to create a custom session factory bean to make sure the correct catalog is set before the connection is used by the session.</p>
<pre class="brush: java;">
public class CustomLocalSessionFactoryBean extends LocalSessionFactoryBean {

	@Override
	protected void postProcessConfiguration(Configuration config) throws HibernateException {
		String connectionProvider = config.getProperty(Environment.CONNECTION_PROVIDER);
		if (connectionProvider != null) {
			if (connectionProvider.equals(TransactionAwareDataSourceConnectionProvider.class.getName())) {
				connectionProvider = CustomTransactionAwareDataSourceConnectionProvider.class.getName();
			} else if (connectionProvider.equals(LocalJtaDataSourceConnectionProvider.class.getName())) {
				connectionProvider = CustomLocalJtaDataSourceConnectionProvider.class.getName();
			} else if (connectionProvider.equals(LocalDataSourceConnectionProvider.class.getName())) {
				connectionProvider = CustomLocalDataSourceConnectionProvider.class.getName();
			}
			// Set Spring-provided DataSource as Hibernate ConnectionProvider.
			config.setProperty(Environment.CONNECTION_PROVIDER, connectionProvider);
		}
		super.postProcessConfiguration(config);
	}

	@Override
	protected void afterSessionFactoryCreation() throws Exception {
		SessionFactoryImplementor implementor = (SessionFactoryImplementor) getSessionFactory();
		ConnectionProvider provider = implementor.getConnectionProvider();
		if(provider instanceof CustomConnectionProvider) {
			((CustomConnectionProvider)provider).setFactory(implementor);
		}
		super.afterSessionFactoryCreation();
	}

	public interface CustomConnectionProvider {

		public void setFactory(SessionFactory factory);
	}

	public static class CustomLocalDataSourceConnectionProvider extends LocalDataSourceConnectionProvider implements CustomConnectionProvider {

		private SessionFactoryImplementor factory;

		@Override
		public void setFactory(SessionFactory factory) {
			this.factory = (SessionFactoryImplementor) factory;
		}

		@Override
		public Connection getConnection() throws SQLException {
			Connection connection = super.getConnection();
			DataSourceIdentifier dataSourceId = ContextHolder.getDataSourceId(this.factory);
			if(dataSourceId != null &amp;&amp; dataSourceId.getDataSourceCatalog() != null) {
				connection.setCatalog(dataSourceId.getDataSourceCatalog());
			}
			connection.setAutoCommit(false);
			return connection;
		}

	}

	// Repeat the same for CustomTransactionAwareDataSourceConnectionProvider and CustomLocalJtaDataSourceConnectionProvider as well

}
</pre>
<p>The session factory configuration will look like this,</p>
<pre class="brush: xml;">
&lt;bean id=&quot;db0&quot; class=&quot;CustomLocalSessionFactoryBean&quot;&gt;
	&lt;property name=&quot;hibernateProperties&quot; ref=&quot;hibernateProperties&quot; /&gt;
	&lt;property name=&quot;mappingResources&quot; ref=&quot;dbPartitionMappings&quot;/&gt;
	&lt;property name=&quot;dataSource&quot; ref=&quot;db0DataSource&quot; /&gt;
&lt;/bean&gt;
</pre>
<p>And that is it. We are ready to test this out. It has been working pretty good in my laptop <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I would love to get back from you guys on optimizing the design or the issues that you find with this logic.</p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3/&amp;title=Hibernate, Spring and multiple datasources"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3&amp;title=Hibernate, Spring and multiple datasources&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3&amp;title=Hibernate, Spring and multiple datasources&amp;"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/96/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=96&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/05/hibernate-spring-and-multiple-datasources-part-3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
		<item>
		<title>Hibernate, Spring and multiple datasources (Part 2)</title>
		<link>http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2/</link>
		<comments>http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 04:28:59 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[catalog]]></category>
		<category><![CDATA[dynamic datasource routing]]></category>
		<category><![CDATA[Hibernate template]]></category>
		<category><![CDATA[Hibernate transaction manager]]></category>
		<category><![CDATA[horizontal partitioning]]></category>
		<category><![CDATA[JTA]]></category>
		<category><![CDATA[multiple datasources]]></category>
		<category><![CDATA[multiple session factories]]></category>
		<category><![CDATA[shards]]></category>
		<category><![CDATA[threadlocal]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=73</guid>
		<description><![CDATA[In the last post I was discussing about using Hibernate for multiple datasources with no partitioning. The solution is quite simple with no big design changes. But in the case of partitioned databases you might need to do some changes to your DAL design. The idea here is to use a datasource identifier to decide [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=73&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the last post I was discussing about using Hibernate for multiple datasources with no partitioning. The solution is quite simple with no big design changes. But in the case of partitioned databases you might need to do some changes to your DAL design.</p>
<p>The idea here is to use a datasource identifier to decide which partition to use.</p>
<pre class="brush: java;">
public abstract class DataSourceIdentifier {

	protected String dataSourceId;

	public String getDataSourceId() {
		if(dataSourceId == null) {
			resolve();
		}
		return dataSourceId;
	}

	// Add the partitioning logic here
	public abstract void resolve();
}
</pre>
<p>I just borrowed the idea of ThreadLocal from other posts to identify the current working datasource. But unlike the other solution (routing-data-source) referred in the last post, this would allow us to work with multiple datasources simultaneously.</p>
<pre class="brush: java;">
public final class ContextHolder {

	private ContextHolder holder;
	private ThreadLocal context;

	// Private, to make it singleton
	private ContextHolder() {
		context =  = new ThreadLocal();
	}

	public ContextHolder getInstance() {
		if(holder == null) {
			holder = new ContextHolder();
		}
		return holder;
	}

	public DataSourceIdentifier getLastUsedDataSourceIdentifier() {
		DataSourceIdentifier dataSourceId = (DataSourceIdentifier)context.get();
		return dataSourceId;
	}

	public void use(DataSourceIdetifier dataSourceId) {
		context.set(dataSourceId);
	}
}
</pre>
<p>Now, we gotta implement our custom Spring HibernateTemplate class to make things work.</p>
<pre class="brush: java;">
public class CustomHibernateTemplate extends HibernateTemplate {

	public void setContext(DataSourceIdentifier dataSourceId) {
		ContextHolder.getInstance().use(dataSourceId);
	}

	public  T load(Class entityClass, Serializable id, DataSourceIdentifier dataSourceId) throws DataAccessException {
		setContext(dataSourceId);
		return super.load(entityClass, id);
	}

	// Override the super class methods and throw an exception as we need DatasourceIndetifier to work with a datasource.
	public  T load(Class entityClass, Serializable id) throws DataAccessException {
		throw new IllegalArgumentException();
	}

	// Likewise you can do for all the super class methods

	public SessionFactory getSessionFactory() {
		DataSourceIdentifier dataSourceIdentifier = ContextHolder.getInstance().getLastUsedDataSourceIdentifier();
		if(dataSourceIdentifier == null) {
			// TODO get the default datasource (partition)
			dataSourceIdentifier = getDefaultDataSourceIdentifier();
		}
		return getSessionFactory(dataSourceIdentifier);
	}

	protected SessionFactory getSessionFactory(DataSourceIdentifier dataSourceIdentifier) {
		// TODO get from application context or do an dependency injection or whatever you like/know of
	}
}
</pre>
<p>Thats it, we are almost done. We gotta use this template in the DAO class.</p>
<pre class="brush: java;">
public class HibernateDAO extends HibernateDaoSupport {

	public void delete(T entity, DataSourceIdentifier dataSourceId) throws DAException {
		try {
			getTemplate().delete(entity, dataSourceId);
		} catch (DataAccessException e) {
			throw new DAException(e);
		}
	}

	public void refresh(T entity, DataSourceIdentifier dataSourceId) throws DAException {
		try {
			getTemplate().refresh(entity, dataSourceId);
		} catch (DataAccessException e) {
			throw new DAException(e);
		}
	}

	@Override
	protected HibernateTemplate createHibernateTemplate(SessionFactory factory) {
		return new CustomHibernateTemplate(sessionFactory);
	}

	public CustomHibernateTemplate getTemplate() {
		return (CustomHibernateTemplate) getHibernateTemplate();
	}
}
</pre>
<p>Make sure all your subclasses of HibernateDAO calls getTemplate() instead of getHibernateTemplate() as the later will return an HibernateTemplate and you would have to cast it to CustomHibernateTemplate. So better to use getTemplate() in the DAO.</p>
<p>If you go with container managed transactions, you can sit back and relax, nothing to worry about. But for those poor guys like me who don&#8217;t use JTA, we need to customize the transaction manager as well.</p>
<pre class="brush: java;">
public class CustomHibernateTransactionManager extends HibernateTransactionManager {

	public void setContext(DataSourceIdentifier dataSourceId) {
		ContextHolder.getInstance().use(dataSourceId);
	}

	public void getTransaction(TransactionDefinition definition, DataSourceIdentifier dataSourceId) {
		setContext(dataSourceId);
		super.getTransaction(definition);
	}

	// Unfortunately, we can't override the super class method getTransaction(definition) as its final ;-(.
	// But there's a workaround for that as well around. I'm not going to confuse you explaining it here.

	public SessionFactory getSessionFactory() {
		DataSourceIdentifier dataSourceIdentifier = ContextHolder.getInstance().getLastUsedDataSourceIdentifier();
		if(dataSourceIdentifier == null) {
			// TODO get the default datasource (partition)
			dataSourceIdentifier = getDefaultDataSourceIdentifier();
		}
		return getSessionFactory(dataSourceIdentifier);
	}

	protected SessionFactory getSessionFactory(DataSourceIdentifier dataSourceIdentifier) {
		// TODO get from application context or do an dependency injection or whatever you like/know of
	}
}
</pre>
<p>Yep.. We are done with all the fixes (Hacks ???). Even though we save the dataSourceId in the threadLocal, we should still be able to work with more than one partitions within the same thread provided you use Spring&#8217;s HibernateTemplate in your code. </p>
<p>What if you want to use the same connection to connect to all the partitions?. Meaning, all the partitions are there in the same database instance and you want switch the partitions by changing the catalog. I shall discuss this in my next post.</p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2/&amp;title=Hibernate, Spring and multiple datasources"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2&amp;title=Hibernate, Spring and multiple datasources&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2&amp;title=Hibernate, Spring and multiple datasources&amp;"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=73&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/04/hibernate-spring-and-multiple-datasources-part-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
		<item>
		<title>Hibernate, Spring and multiple datasources (Part 1)</title>
		<link>http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/</link>
		<comments>http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 03:53:27 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[dynamic datasource routing]]></category>
		<category><![CDATA[horizontal partitioning]]></category>
		<category><![CDATA[multiple datasources]]></category>
		<category><![CDATA[multiple session factories]]></category>
		<category><![CDATA[shards]]></category>
		<category><![CDATA[threadlocal]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=69</guid>
		<description><![CDATA[Working with multiple databases always seem to be concern for many hibernate users (myself on the top of the list ). When I started using hibernate, it felt really simpler not until I tried to use it in my application with multiple data sources. I searched the whole net if I&#8217;m the only one looking [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=69&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Working with multiple databases always seem to be concern for many hibernate users (myself on the top of the list <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ). When I started using hibernate, it felt really simpler not until I tried to use it in my application with multiple data sources. I searched the whole net if I&#8217;m the only one looking for that. But thank god, there were (are) thousands of people scratching their heads for a solution. I did manage to find a solution that used ThreadLocal to solve this issue. Take a look at this original post <a href="http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/">&#8216;dynamic-datasource-routing&#8217;</a>.</p>
<p>That&#8217;s a pretty good solution but comes with its own limitation,</p>
<ul>
<li>Works good if the application receives request for a single customer. But what if I have process a request for multiple customers, say I have a cron job that fires a request to the servlet to do something on these customer accounts. The thread local will be constantly overwritten that could lead bizarre effects.</li>
<li>The above holds good for transactions as well. If you are using JTA, you will be safe but if you let your application manage the transaction, this solution is not for you.</li>
</ul>
<p>If you closely take a look at the example in that post, you will notice that the database being partitioned based on the customer (horizontal partitioning). So why not go for Hibernate Shards in this case? I will leave that to you to decide on that. Hibernate Shards is a powerful library when you want to pull out collective data from multiple partitions. But in this case, the partitioning is done based on the customer. So we will be interested only on one partition at a time.</p>
<p>If you are not interested in Shards, then you might be interested in the below solutions. Multiple datasources doesn&#8217;t mean that database is partitioned, you can still have multiple databases one per an independent module. So the solution would differ for both of them.</p>
<p><strong>Multiple datasources &#8211; no partitioning</strong><br />
If you are using JTA, the solution is straight forward. You have to create session factories for all the datasources and attach them to your DAO&#8217;s (Yep, this solution is for those who use Hibernate in conjunction with Spring). And a JTA transaction manager to manage the transactions. That&#8217;s it, you need not bother about the datasource as any table will have in no more than one data source; remember you have not partitioned your database. Since you go with the JTA, its the container that manages the transaction. So the transaction is going to be across all the connections created within that transactional scope. </p>
<p>But when you let the application handle the transactions, you might have to create one transaction manager per session factory. When you create the transaction, choose the transaction manager that you want to use based on the session factory (you can get them from the DAO, no thread local is required here). I don&#8217;t have any example to demonstrate this and I feel that my solution descriptive enough to understand. If you feel that you need to an example, please do ping me.</p>
<p>I shall discuss the multiple data sources with partitioning in my next post..</p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/&amp;title=Hibernate, Spring and multiple datasources"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1&amp;title=Hibernate, Spring and multiple datasources&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1&amp;title=Hibernate, Spring and multiple datasources&amp;"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=69&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/03/hibernate-spring-and-multiple-datasources-part-1/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
		<item>
		<title>Hibernate Horizontal Partitioning</title>
		<link>http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/</link>
		<comments>http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 03:36:42 +0000</pubDate>
		<dc:creator>sgane</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[catalog]]></category>
		<category><![CDATA[horizontal partitioning]]></category>
		<category><![CDATA[multiple session factories]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[shardedsessionfactory]]></category>
		<category><![CDATA[shards]]></category>

		<guid isPermaLink="false">http://sysin.wordpress.com/?p=66</guid>
		<description><![CDATA[I happen to work with multiple partitions (horizontal partitioning) in my application. I was wondering, if there is an out of box way to achieve this and came across Hibernate Shards. Though its still in beta, I dared to go over documentation to check if it would fit my application. Of course it did satisfy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=66&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I happen to work with multiple partitions (horizontal partitioning) in my application. I was wondering, if there is an out of box way to achieve this and came across <a href="http://www.hibernate.org/414.html">Hibernate Shards</a>. Though its still in beta, I dared to go over documentation to check if it would fit my application. Of course it did satisfy many of my requirements like,</p>
<ul>
<li>Selecting a shard for an object at runtime</li>
<li>Support for other hibernate features like Criteria, query etc&#8230;</li>
<li>Clean implementation in the application and DAO layer</li>
</ul>
<p>Although the shards documentation has listed out of a lots of limitations like, lack of support for HQL and other hibernate features, I wasn&#8217;t concerned much as anyways I&#8217;m not using HQL in my application. But all my database partitions are there in the same MySQL instance. So we thought of using the same connection for all the partitions and decide which partition to switch to at runtime by setting the catalog over the connection.<br />
<code><br />
connection.setCatalog(partitionName);<br />
</code><br />
This piece of code will execute.<br />
<code><br />
use partitionName;<br />
</code><br />
using the MySQL connection and there by switch the catalog to the new partition. The idea here is to minimize the total number of connections used by all the partitions thereby allocate more connections to other applications using the same database.</p>
<p>But unfortunately I don&#8217;t find such a feature in shards. Shards always insists in creating one session factory per partition (and group them together using ShardedSessionFactory). Moreover there&#8217;s no active development going on in this project lately and what I have heard from the developer is this project has been <del datetime="2009-03-03T02:53:55+00:00">shelved</del> stalled.</p>
<p>So I thought of coming up with my own version of shards (though not a very good design like shards but certainly satisfies my needs). I will try to post that design sometime later in another post.</p>
<p><a href="http://www.reddit.com/submit?url=http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/&amp;title=Hibernate Horizontal Partitioning"><img src="http://www.reddit.com/static/spreddit8.gif" alt="submit to reddit" border="0" /> </a> <a href="http://digg.com/submit?url=http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/&amp;title=Hibernate Horizontal Partitioning&amp;topic=design"><img src="http://digg.com/img/badges/32x32-digg-guy.png" width="32" height="32" alt="Digg!" /></a><a href="http://www.stumbleupon.com/submit?url=http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/&amp;title=Hibernate Horizontal Partitioning"> <img border="0" src="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" alt=""></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sysin.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sysin.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sysin.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sysin.wordpress.com&amp;blog=6022074&amp;post=66&amp;subd=sysin&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sysin.wordpress.com/2009/03/02/hibernate-horizontal-partitioning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8c07792bd434636c9beed25d1558cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>

		<media:content url="http://www.reddit.com/static/spreddit8.gif" medium="image">
			<media:title type="html">submit to reddit</media:title>
		</media:content>

		<media:content url="http://digg.com/img/badges/32x32-digg-guy.png" medium="image">
			<media:title type="html">Digg!</media:title>
		</media:content>

		<media:content url="http://cdn.stumble-upon.com/images/160x30_su_blue.gif" medium="image" />
	</item>
	</channel>
</rss>
