<?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/"
	>

<channel>
	<title>jolbox.com</title>
	<atom:link href="http://jolbox.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://jolbox.com/blog</link>
	<description>Home of BoneCP and more</description>
	<lastBuildDate>Wed, 17 Feb 2010 16:18:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Switching datasources dynamically at runtime</title>
		<link>http://jolbox.com/blog/?p=31</link>
		<comments>http://jolbox.com/blog/?p=31#comments</comments>
		<pubDate>Wed, 17 Feb 2010 16:00:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[bonecp]]></category>
		<category><![CDATA[datasource]]></category>
		<category><![CDATA[DelegatingDataSource]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[DynamicDataSourceProxy]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://jolbox.com/blog/?p=31</guid>
		<description><![CDATA[Let&#8217;s say you are running your application on a database that requires a password change (or switching to a different machine or whatever). Normally this would require an application restart to reflect the new changes but there&#8217;s an easier way. Since v0.6.4-rc2, BoneCP has support for lazy termination which means that: Connections not in use [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you are running your application on a database that requires a password change (or switching to a different machine or whatever). Normally this would require an application restart to reflect the new changes but there&#8217;s an easier way.</p>
<p>Since v0.6.4-rc2, <a href="http://jolbox.com">BoneCP</a> has support for lazy termination which means that:</p>
<li>Connections not in use are closed off
<li>Helper threads are shut down
<li>Connections in use are left to work but will be closed off when they are closed by the application.
</li>
<p>This means that so long as we can tell our application to use a different connection pool we can seamlessly migrate to use the new settings by creating a new pool with the new parameters (ip, username, password, etc) and telling the old one to terminate gently.</p>
<p>That&#8217;s the gist of it and thankfully Spring and BoneCP makes this easy. By using a DelegatingDataSource we can bounce off a request to our datasource to some custom logic. This is the approach provided by BoneCP&#8217;s DynamicDataSourceProxy that simply exposes a new method, switchDataSource, that takes the new BoneCPConfig configuration object and does all the required work. </p>
<p>In other words, when DynamicDataSourceProxy&#8217;s switchDataSource method is called:</p>
<li> A new datasource with the new configuration is created (i.e. a new connection pool is created)
<li> A connection is fetched and closed from the new pool to force any lazy initialization structures to kick-start.
<li> The DelegatingDataSource target datasource is switched to the new pool (this forces the application to start using the new pool)
<li> The old pool is asked to terminate lazily.
</li>
<p>Spring&#8217;s context file is amended so that the obtained datasource bounces off the dynamicDataSourceProxy instead:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code3'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p31code3"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dynamicDataSourceProxy&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jolbox.bonecp.spring.DynamicDataSourceProxy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;targetDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;mainDataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
where mainDataSource is your old datasource.</pre></td></tr></table></div>

<p>Now just create a mechanism to trigger the switch method by some means such as polling an LDAP server for new settings or reading a configuration file. Something like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code4'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p314"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p31code4"><pre class="java" style="font-family:monospace;">BoneCPConfig newConfig <span style="color: #339933;">=</span> .. <span style="color: #006633;">create</span> <span style="color: #000000; font-weight: bold;">new</span> config here ...
<span style="color: #006633;">BoneCPConfig</span> oldConfig <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>BoneCPDataSource<span style="color: #009900;">&#41;</span>ds.<span style="color: #006633;">getTargetDataSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newConfig <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>newConfig.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>oldConfig<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	ds.<span style="color: #006633;">switchDataSource</span><span style="color: #009900;">&#40;</span>newConfig<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<!-- Easy AdSense V2.82 -->
<!-- Post[count: 2] -->
<div class="ezAdsense adsense adsense-leadout" style="text-align:center;margin:12px;"><script type="text/javascript"><!--
google_ad_client = "pub-9180114063075078";
/* 300x250, created 11/18/09 */
google_ad_slot = "6508452605";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"

src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script>	</div>]]></content:encoded>
			<wfw:commentRss>http://jolbox.com/blog/?feed=rss2&amp;p=31</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Understanding &#8220;No session bound to thread&#8221; spring/hibernate problem</title>
		<link>http://jolbox.com/blog/?p=27</link>
		<comments>http://jolbox.com/blog/?p=27#comments</comments>
		<pubDate>Mon, 23 Nov 2009 05:30:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://jolbox.com/blog/?p=27</guid>
		<description><![CDATA[Let&#8217;s start off with some sample code. The following sample will throw an error at runtime. Can you identify the problem? ?View Code JAVA1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Autowired SomeClass someClass; &#160; public void doSomething&#40;&#41;&#123; someClass.callFoo&#40;&#41;; &#125; &#160; @Component public class SomeClass&#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s start off with some sample code. The following sample will throw an error at runtime. Can you identify the problem?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code12'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2712"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p27code12"><pre class="java" style="font-family:monospace;">@Autowired 
SomeClass someClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	someClass.<span style="color: #006633;">callFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClass<span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>	
&nbsp;
@Transactional 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">// write to the db here </span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Before we discuss the problem, let&#8217;s go back to the basics on some aspects of Spring for a tiny refresher:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code13'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2713"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p27code13"><pre class="java" style="font-family:monospace;">@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span> … <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> somewhereElse<span style="color: #009900;">&#123;</span> 
	@Autowired
	Foo fooBar<span style="color: #339933;">;</span>     
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The @Autowired annotation in line 5 tells Spring to fill in the variable &#8220;fooBar&#8221; just as if you wrote fooBar = new Foo(). Now the key thing here is this: <em>you&#8217;re asking someone else (Spring) to give you what is required.</em> In other words, you have given the Spring library the chance to do something before giving you what you asked for. </p>
<p>Now back to our initial example. In line 5, reproduced below:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code14'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2714"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code14"><pre class="java" style="font-family:monospace;">	someClass.<span style="color: #006633;">callFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>the variable someClass was obtained via Spring (via the @Autowired annotation); therefore you need to read this line as :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code15'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2715"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code15"><pre class="java" style="font-family:monospace;">	<span style="color: #009900;">&#91;</span>something Spring gave you<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">callFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So far so good. Tracing through the code we now arrive at the writeToDB call:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code16'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2716"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p27code16"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
       <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>this time there is no Spring involved! Even though you initially jumped to the class via the spring handle, at this point you&#8217;re simply jumping from one method to the next just as if you typed <em>this</em>.writeToDB().</p>
<p>Put differently, you are now back to &#8220;normal&#8221; Java land, no Spring library or proxy involved here! Some people think that once you&#8217;ve jumped into the class via Spring, the Spring Framework is still somehow in control. This is not the case! Spring has done it&#8217;s job and is now gone!</p>
<p>With this in mind, it should be obvious that the @Transactional will have no effect whatsoever because there&#8217;s no place where anyone is checking for it, thus leading to a &#8220;no session bound to thread&#8221; error the moment you try to write to the database.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code17'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2717"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p27code17"><pre class="java" style="font-family:monospace;">@Autowired 
SomeClass someClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	someClass.<span style="color: #006633;">callFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #339933;">&lt;--</span> someClass is a proxy, Spring can kick in
<span style="color: #009900;">&#125;</span> 
&nbsp;
@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClass<span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #339933;">&lt;--</span> No spring here. <span style="color: #006633;">You</span>’re back in normal Java land
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>	
&nbsp;
@Transactional                 <span style="color: #339933;">&lt;--</span> Java ignores <span style="color: #000000; font-weight: bold;">this</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  write to the db here         <span style="color: #339933;">&lt;--</span> <span style="color: #0000ff;">&quot;No session bound to thread here&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now let&#8217;s make a little change by moving the @Transactional attribute elsewhere.</p>
<p>Remember the <em>[something Spring gave you]</em>.callFoo() call?  This time, Spring gets a change to see the @Transactional annotation because before jumping to the method callFoo(), it can perform the @Transactional magic.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code18'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2718"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p27code18"><pre class="java" style="font-family:monospace;">@Autowired 
SomeClass someClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	someClass.<span style="color: #006633;">callFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	<span style="color: #339933;">&lt;--</span> someClass is a proxy, Spring can kick in 
<span style="color: #009900;">&#125;</span> 
&nbsp;
@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClass<span style="color: #009900;">&#123;</span> 
&nbsp;
       @Transactional		<span style="color: #339933;">&lt;--</span> Spring sees <span style="color: #000000; font-weight: bold;">this</span>, starts session context
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 		
<span style="color: #009900;">&#125;</span>	
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> writeToDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	  write to the db here   <span style="color: #339933;">&lt;--</span> Now it works
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now for some rules of the game:</p>
<ul>
<li>Decide once and for all who’s going to define the @Transactional. Don’t sprinkle these around! The sensible place where to place these is in the service layer. If you add the annotation on your data layer you will probably be creating transactions all over the place!</li>
<li>new XXX() is a potential source of trouble. @Autowired/getBean() are your friends
</ul>
<p>JolBox is home to <a href="http://jolbox.com">BoneCP &#8211; the fast JDBC Connection Pool</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jolbox.com/blog/?feed=rss2&amp;p=27</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache</title>
		<link>http://jolbox.com/blog/?p=22</link>
		<comments>http://jolbox.com/blog/?p=22#comments</comments>
		<pubDate>Thu, 19 Nov 2009 13:58:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[LazyDataSourceProxy]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://jolbox.com/blog/?p=22</guid>
		<description><![CDATA[You might not have encountered it yet, but the excellent Spring framework has a pretty useful class, the so-called LazyConnectionDataSourceProxy. Where would you use it? Let&#8217;s say you have a servlet or process that will eventually talk to a database. Typically you&#8217;d have something like: Open a database connection Perform a SELECT query Return the [...]]]></description>
			<content:encoded><![CDATA[<p>You might not have encountered it yet, but the excellent Spring framework has a pretty useful class, the so-called <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.html">LazyConnectionDataSourceProxy</a>. Where would you use it?</p>
<p>Let&#8217;s say you have a servlet or process that will eventually talk to a database. Typically you&#8217;d have something like:</p>
<ul>
<li>Open a database connection</li>
<li>Perform a SELECT query</li>
<li>Return the result and close the connection</li>
</ul>
<p>This works fine for small loads but eventually you&#8217;ll want to hit a cache to avoid a database hit if necessary. </p>
<p>Under spring/hibernate control this is typically coded as:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code21'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2221"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p22code21"><pre class="java" style="font-family:monospace;">@Transactional
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   dataLayer.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT ....&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now hibernate does support caching to avoid the database hit, but by the time you&#8217;re at the dataLayer call you&#8217;ve already gone past the @Transaction. This means that <em>before</em> you even hit the cache, you&#8217;ve already talked to the database! What&#8217;s there to talk about? Not much &#8212; but at the very least somewhere you&#8217;d be sending &#8220;BEGIN TRANSACTION&#8221;. This is database chatter that you can do without.</p>
<p>Enter LazyDataSourceProxy which, as the name implies, makes the datasource behave &#8220;lazily&#8221;. This is actually a pretty simple bean &#8212; instead of talking to the database as soon as Spring sees @Transactional, the bean will add some more logic to your connection handles. In essence, the bean will simply delay the BEGIN TRANSACTION request (or whatever) until you do something useful with the connection &#8212; in this case, the SQL query. Therefore for the code snippet pasted above this would result in:</p>
<p>Line 1-2: Do nothing<br />
Line 3: If Hibernate doesn&#8217;t hit the cache and somewhere tries to use the connection to actually send the query: Do the @Transactional bits to open a connection etc and proceed as normal; otherwise do nothing.</p>
<p>The best thing about the bean is that it is completely non-invasive &#8211; you just bounce off your datasource off the bean and that&#8217;s it. </p>
<p>Enough talk. Here&#8217;s an example on how to configure it using <a href="http://jolbox.com">BoneCP</a> as a connection pool (the same technique applies for any connection pool or datasource provider)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code22'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2222"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p22code22"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;...&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Tell hibernate to use our given datasource --&gt;</span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateProperties&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        ...
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #808080; font-style: italic;">&lt;!-- Spring bean configuration. Tell Spring to bounce off BoneCP --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;targetDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;mainDataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #808080; font-style: italic;">&lt;!-- BoneCP configuration --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainDataSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jolbox.bonecp.BoneCPDataSource&quot;</span> <span style="color: #000066;">destroy-method</span>=<span style="color: #ff0000;">&quot;close&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;driverClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jdbcUrl&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jdbc:mysql://127.0.0.1/yourdb&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;root&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;abcdefgh&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                ...
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jolbox.com/blog/?feed=rss2&amp;p=22</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find my resource!</title>
		<link>http://jolbox.com/blog/?p=17</link>
		<comments>http://jolbox.com/blog/?p=17#comments</comments>
		<pubDate>Wed, 18 Nov 2009 18:18:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[FindClass]]></category>
		<category><![CDATA[Util]]></category>

		<guid isPermaLink="false">http://jolbox.com/blog/?p=17</guid>
		<description><![CDATA[Hands up if you&#8217;ve seen this: You deploy your application. Crash. Time to switch on more logging by editing log4j.properties. You change it and nothing happens. Was there another log4j.properties somewhere? Which one is being used? or You made your change and the build is still picking up old code. Where&#8217;s that being loaded? Wouldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Hands up if you&#8217;ve seen this:</p>
<p>You deploy your application. Crash. Time to switch on more logging by editing log4j.properties. You change it and nothing happens. Was there another log4j.properties somewhere? Which one is being used?</p>
<p>or</p>
<p>You made your change and the build is still picking up old code. Where&#8217;s that being loaded?</p>
<p>Wouldn&#8217;t it be nice to get some more info from the classloader? I hereby introduce FindClass which, despite the name, can find any resource really, whether nested in a sub folder or buried inside a JAR file.</p>
<p>Here&#8217;s a quick example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p17code27'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1727"><td class="code" id="p17code27"><pre class="java" style="font-family:monospace;">FindClass findClass <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FindClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Long class loaded from : &quot;</span><span style="color: #339933;">+</span>findClass.<span style="color: #006633;">getLoadOrigin</span><span style="color: #009900;">&#40;</span>1L<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FindClass class has been loaded from : &quot;</span> <span style="color: #339933;">+</span> findClass.<span style="color: #006633;">getLoadOrigin</span><span style="color: #009900;">&#40;</span>FindClass.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print out:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p17code28'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1728"><td class="code" id="p17code28"><pre class="java" style="font-family:monospace;"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> <span style="color: #000000; font-weight: bold;">class</span> has been loaded from <span style="color: #339933;">:</span> D<span style="color: #339933;">:</span>\Java\jdk1.6.0_02\jre\lib\rt.<span style="color: #006633;">jar</span>
FindClass <span style="color: #000000; font-weight: bold;">class</span> has been loaded from <span style="color: #339933;">:</span> D<span style="color: #339933;">:</span>\Workspace\\findclass\target\classes\com\jolbox\utils\FindClass.<span style="color: #000000; font-weight: bold;">class</span></pre></td></tr></table></div>

<p>Just for kicks, I&#8217;ve added helper methods on the same theme so that you can trace an existing object, a given class or a resource:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p17code29'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1729"><td class="code" id="p17code29"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getLoadOriginClass<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> fullyQualifiedClassName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getLoadOrigin<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getLoadOrigin<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> findLoadOrigin<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is nice but sometimes you need to find stuff without having to embed this inside your application. Using basically the same methods, the class itself can be made to search directly from the command line:</p>
<p>java -cp . com.jolbox.utils.FindClass startdir match extension</p>
<p>For example:</p>
<pre code="shell" colla="+">
FindClass d:\\workspace FooBar .class
</pre>
<p>matches *FooClass*.class (case is not important)</p>
<p>What if you&#8217;re application is already running? Not to fear! So long as your application is Spring enabled (and you&#8217;ve scanned the class in), the more important methods are already JMX enabled.</p>
<p>Going back to our original question: Want to know all the locations of log4j.properties?  Just call:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p17code30'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1730"><td class="code" id="p17code30"><pre class="java" style="font-family:monospace;">&nbsp;
searchInClassPath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log4j&quot;</span>, <span style="color: #0000ff;">&quot;properties&quot;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>at the beginning of your application to get a list of locations containing that file. </p>
<p>There are some other helper methods available but I&#8217;ll leave those for you to discover. Do remember however that these methods are slow and are therefore only to be used in a debugging context.</p>
<p>FAQ: Can you dump all the locations of all the classes in use?<br />
Answer: No&#8230; and yes. The method is available but remember that the classloader will only load the classes you actually use so it&#8217;s really a dump of all the locations of the classes that have been used at least once.</p>
<p>Disclaimer: Not all the code in the class was written by me; some routines are a collection of snippets off the web.</p>
<p>Download code from <a href="http://jolbox.com/blog-downloads/FindClass.java">here</a></p>
<p>JolBox hosts <a href="http://jolbox.com">BoneCP &#8211; The fast Java JDBC Connection pool.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jolbox.com/blog/?feed=rss2&amp;p=17</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

