<?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>Eddie Hsiung&#039;s Blog</title>
	<atom:link href="https://eddii.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://eddii.wordpress.com</link>
	<description>My sandbox of ideas</description>
	<lastBuildDate>Wed, 09 Jul 2008 00:16:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='eddii.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s0.wp.com/i/buttonw-com.png</url>
		<title>Eddie Hsiung&#039;s Blog</title>
		<link>https://eddii.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://eddii.wordpress.com/osd.xml" title="Eddie Hsiung&#039;s Blog" />
	<atom:link rel='hub' href='https://eddii.wordpress.com/?pushpress=hub'/>
	<item>
		<title>Hibernate &#8216;on-delete=cascade&#8217; Performance Tuning</title>
		<link>https://eddii.wordpress.com/2006/11/16/hibernate-on-deletecascade-performance/</link>
					<comments>https://eddii.wordpress.com/2006/11/16/hibernate-on-deletecascade-performance/#comments</comments>
		
		<dc:creator><![CDATA[eddie]]></dc:creator>
		<pubDate>Wed, 15 Nov 2006 17:13:14 +0000</pubDate>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance tuning]]></category>
		<guid isPermaLink="false">http://eddii.wordpress.com/2006/11/16/hibernate-on-deletecascade-performance/</guid>

					<description><![CDATA[Be careful when you use Hibernate&#8217;s support for database ON DELETE CASCADE constraint. If not configured properly, your application might be more performance-costly than you think. Let me use the example from Gavin King&#8217;s email introducing the new setting of on-delete=&#8221;cascade&#8221;: For a parent object associated with N child objects (cascade=&#8221;all&#8221;), the setting on-delete=&#8221;cascade&#8221; avoids [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Be careful when you use Hibernate&#8217;s support for database <em>ON DELETE CASCADE</em> constraint. If not configured properly, your application might be more performance-costly than you think.</p>
<p>Let me use the example from <a href="http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg03801.html" target="blank">Gavin King&#8217;s email</a> introducing the new setting of <em>on-delete=&#8221;cascade&#8221;</em>:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;set name=&quot;children&quot; inverse=&quot;true&quot; cascade=&quot;all&quot;&gt;
  &lt;key name=&quot;PARENT_ID&quot; on-delete=&quot;cascade&quot;&gt;
  &lt;one-to-many class=&quot;Child&quot;&gt;
&lt;set&gt;
</pre>
<p>For a parent object associated with N child objects (cascade=&#8221;all&#8221;), the setting <em>on-delete=&#8221;cascade&#8221;</em> avoids issuing N deletes to the database if the parent were to be deleted.</p>
<p><span id="more-5"></span>Gavin also <a href="http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg03803.html" target="blank">tells us</a> that this setting only optimizes away the delete statements while all semantics are preserved. Well, what exactly does he mean?</p>
<p>I will roughly explain the procedure Hibernate performs to cascade delete in the simplest scenario:</p>
<ol>
<li>mark entity for deletion</li>
<li>iterate through all of entity&#8217;s associations having cascade delete (one or many)</li>
<li>if the association is single, perform delete on this association (by starting step 1 for this association)</li>
<li>if the association is many, perform delete on this collection by:
<ol>
<li>iterate all the collection elements, loading them from the database if necessary</li>
<li>for each collection element, perform delete on the element (by starting step1 for this element)</li>
</ol>
</li>
<li>if not <em>on-delete=&#8221;cascade&#8221;</em>, issue delete statement on this entity</li>
</ol>
<p>When you have a to-many association with <em>cascade=&#8221;all&#8221;</em> as described in our example, Hibernate iterates the associated collection having size N, loading them from database if necessary, and marks each collection element for deletion. Before actually issuing delete statements for these elements, Hibernate searches for any other cascades configured on them and perform these found cascading actions accordingly.</p>
<p>Even with the setting <em>on-delete=&#8221;cascade&#8221;</em> on a specific lazy collection, <em>cascade=&#8221;all&#8221;</em> causes Hibernate to initialize and iterate the collection (because it wants to check if the elements in this collection also cascade delete to other associations). Therefore, when you are sure there are no other cascading actions configured on your child entity (the element in the <em>on-delete=&#8221;cascade&#8221;</em> collection), use the setting <em>cascade=&#8221;save-update&#8221;</em> instead of <em>cascade=&#8221;all&#8221;</em> to prevent Hibernate from performing the delete cascade checks and consequently avoid loading your lazy to-many association into memory.</p>
<p>If you have multiple layers of delete cascade, from parent to children and from child to grand children, consider the size of your collections and decide if you want to set up multiple layers of <em>ON DELETE CASCADE</em> constraints in database accordingly or preserve the configuration of <em>cascade=&#8221;all&#8221;</em> and <em>on-delete=&#8221;cascade&#8221;</em> to let Hibernate handle the delete cascade on the grand children.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://eddii.wordpress.com/2006/11/16/hibernate-on-deletecascade-performance/feed/</wfw:commentRss>
			<slash:comments>10</slash:comments>
		
		
		
		<media:content url="https://2.gravatar.com/avatar/e9e66af7a8b6b12dffddb1a71f12de07f91f093e1acfe76a60301f61b22236a7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">eddie</media:title>
		</media:content>
	</item>
		<item>
		<title>Detecting Infinite Loop</title>
		<link>https://eddii.wordpress.com/2006/11/15/detecting-infinite-loop/</link>
					<comments>https://eddii.wordpress.com/2006/11/15/detecting-infinite-loop/#comments</comments>
		
		<dc:creator><![CDATA[eddie]]></dc:creator>
		<pubDate>Wed, 15 Nov 2006 08:02:41 +0000</pubDate>
				<category><![CDATA[algorithm]]></category>
		<category><![CDATA[java]]></category>
		<guid isPermaLink="false">http://eddii.wordpress.com/2006/11/15/detecting-infinite-loop/</guid>

					<description><![CDATA[Just yesterday I have come across a problem at work. The problem is the possible occurrence of infinite loop inside a tree structure. To solve it, I need to come up with an algorithm to detect infinite loop occurrence. This little puzzle brings back the memory of a similar and simpler puzzle my high school [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Just yesterday I have come across a problem at work. The problem is the possible occurrence of infinite loop inside a tree structure. To solve it, I need to come up with an algorithm to detect infinite loop occurrence. This little puzzle brings back the memory of a similar and simpler puzzle my high school computer science instructor asked the class.</p>
<p>The problem was: <strong>Given an uni-directional linked list, detect infinite loop without using any extra data structure</strong>.</p>
<p><span id="more-4"></span>The linked list data structure is really simple:<br />
<strong>Node.java</strong></p>
<pre class="brush: java; title: ; notranslate">
class Node {
  Node next;
}
</pre>
<p>My solution to the problem was something like:<br />
<strong>Function isInInfiniteLoop</strong></p>
<pre class="brush: java; title: ; notranslate">
boolean isInInfiniteLoop(Node node) {
  if (node == null) {
    return false;
  }
  Node turtle = node; // slower moving node
  Node rabbit = node.next; // faster moving node
  while (rabbit != null) {
    if (rabbit.equals(turtle)) {
      // the faster moving node has caught up with the slower moving node
      return true;
    } else if (rabbit.next == null) {
      // reached the end of list
      return false;
    } else {
      turtle = turtle.next;
      rabbit = rabbit.next.next;
    }
  }
  // rabbit reached the end
  return false;
}
</pre>
<p>Simple, isn&#8217;t it! At the first glance, almost everyone wondered if this short piece of code is really up to the task. The logic behind it is analogous to having two runners, one (<em>rabbit</em>) running faster than the other (<em>turtle</em>) and, if the course is circular, the faster runner eventually overlapping the slower runner.</p>
<p>Note that it is important for the slower runner to keep moving instead of remaining at the same node because we don&#8217;t know where the course will merge into circle. If the merge point is further down the course, the non-moving runner will never enter the loop which the faster runner is lapping.</p>
<p>This algorithm is not only simplistic, it is also efficient. It is just a O(N) or linear function.</p>
<p>Now, getting back to my work related problem, the data structure I am facing is a tree instead of a sequential linked list. The structure is something like:<br />
<strong>TreeNode.java</strong></p>
<pre class="brush: java; title: ; notranslate">
class TreeNode {
  TreeNode parent;
  List children;
}
</pre>
<p>I have had a hard time figuring out how to apply my little <em>rabbit and turtle</em> algorithm into this context. When it comes to tree structure, I always think recursion. However, I cannot imagine transforming my algorithm into a recursive algorithm. It just appears overwhelming.</p>
<p>Then, it suddenly hits me, since <em>TreeNode</em> can traverse to its parent, luckily, I could do it backwards. The traversal from child to parent is just another sequential linked list.</p>
<p>It simply feels awesome to see that this little algorithm I thought of years ago is still useful today.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://eddii.wordpress.com/2006/11/15/detecting-infinite-loop/feed/</wfw:commentRss>
			<slash:comments>24</slash:comments>
		
		
		
		<media:content url="https://2.gravatar.com/avatar/e9e66af7a8b6b12dffddb1a71f12de07f91f093e1acfe76a60301f61b22236a7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">eddie</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring Bean Substitution</title>
		<link>https://eddii.wordpress.com/2006/06/24/spring-bean-substitution/</link>
					<comments>https://eddii.wordpress.com/2006/06/24/spring-bean-substitution/#comments</comments>
		
		<dc:creator><![CDATA[eddie]]></dc:creator>
		<pubDate>Sat, 24 Jun 2006 12:41:31 +0000</pubDate>
				<category><![CDATA[java]]></category>
		<category><![CDATA[springframework]]></category>
		<guid isPermaLink="false">http://eddii.wordpress.com/2006/06/24/spring-bean-substitution/</guid>

					<description><![CDATA[If we ever try to import a spring configuration from a packaged jar and need to customize some of the bean definitions, we would find ourselves having to do these things: extract the packaged configuration to somewhere we can actually edit; change the bean definition according to our needs; import the new configuration and exclude [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>If we ever try to import a spring configuration from a packaged jar and need to customize some of the bean definitions, we would find ourselves having to do these things:</p>
<ol>
<li>extract the packaged configuration to somewhere we can actually edit;</li>
<li>change the bean definition according to our needs;</li>
<li>import the new configuration and exclude the original one.</li>
</ol>
<p>Even though this works just fine, we will have to maintain the configuration copy and synchronize it with any updates made to the original one. This feels like forking our own branch from the spring configuration.</p>
<p>I think spring configuration can be a lot more flexible and powerful if we can substitute beans instead of having to redefine the whole bean dependency tree. Currently spring provides no such mechanism, so I tried a little hack utilizing the <em>BeanFactoryPostProcessor</em> and the <em>BeanPostProcessor</em>.</p>
<p><span id="more-3"></span>The idea comprises three parts:</p>
<ul>
<li><strong>BeanSubstitutionMapping</strong>
<ul>
<li>Users define which bean to be substituted and which bean is substituting.</li>
</ul>
</li>
<li><strong>BeanSubstitutionConfigurer</strong> implements <em>BeanFactoryPostProcessor</em>
<ul>
<li>When spring application context is created, it reads the substitution mappings and store them.</li>
</ul>
</li>
<li><strong>BeanSubstitutionProcessor</strong> implements <em>BeanPostProcessor</em>
<ul>
<li>Everytime a bean is requested, it reads the substitution mappings, checks if the bean to be loaded has been defined for substitution.</li>
<li>If the bean to be loaded has been mapped for substitution, it returns the substituting bean defined in the mapping instead.</li>
</ul>
</li>
</ul>
<p>Here is the difference when we use bean substitution:</p>
<ol>
<li>don&#8217;t need to extract the original packaged configuration;</li>
<li>don&#8217;t need to exclude the original configuration;</li>
<li>define our substituting bean;</li>
<li>define the substitution mapping;</li>
<li>no longer need to maintain and synchronize with jar updates, only need to maintain our own configuration.</li>
</ol>
<p>I ran a simple test case described below. I was trying to substitute the &#8220;helloWorld&#8221; bean with &#8220;helloWorld2&#8221; bean.</p>
<p>In real world scenario, the bean definition for &#8220;helloWorld&#8221;, java class for <em>HelloWorld</em>, and <em>HelloTaipei</em> would be packaged in jar where we can only read but not modify. The bean definitions for &#8220;helloWorld2&#8221;, &#8220;config&#8221;, &#8220;mapping&#8221;, and java class <em>HelloIrvine</em> would be defined in our project where we are able to modify.</p>
<p><strong>HelloWorld.java</strong></p>
<pre class="brush: java; title: ; notranslate">
public interface HelloWorld {
  public String print();
}
</pre>
<p><strong>HelloTaipei.java</strong></p>
<pre class="brush: java; title: ; notranslate">
public class HelloTaipei implements HelloWorld {
  public String print() {
    return &quot;Hello Taipei!&quot;;
  }
}
</pre>
<p><strong>HelloIrvine.java</strong></p>
<pre class="brush: java; title: ; notranslate">
public class HelloIrvine implements HelloWorld {
  public String print() {
    return &quot;Hello Irvine!&quot;;
  }
}
</pre>
<p><strong>applicationContext.xml</strong></p>
<pre class="brush: xml; title: ; notranslate">
&lt;beans&gt;
  &lt;bean id=&quot;config&quot; class=&quot;my.sandbox.beans.factory.config.BeanSubstitutionConfigurer&quot;/&gt;
  &lt;bean id=&quot;helloWorld&quot; class=&quot;my.sandbox.substitution.HelloTaipei&quot;/&gt;
  &lt;bean id=&quot;helloWorld2&quot; class=&quot;my.sandbox.substitution.HelloIrvine&quot;/&gt;
  &lt;bean id=&quot;mapping&quot; class=&quot;my.sandbox.beans.factory.config.BeanSubstitutionMapping&quot;&gt;
&lt;property name=&quot;targetBean&quot; value=&quot;helloWorld&quot;/&gt;
&lt;property name=&quot;sourceBean&quot; value=&quot;helloWorld2&quot;/&gt;
  &lt;/bean&gt;
&lt;/beans&gt;
</pre>
<p><strong>BeanSubstitutionTest.java</strong></p>
<pre class="brush: java; title: ; notranslate">
public class BeanSubstitutionTest extends
AbstractDependencyInjectionSpringContextTests {
  private HelloWorld helloWorld;
  public BeanSubstitutionTest() {
    setAutowireMode(AUTOWIRE_BY_NAME);
  }
  @Override
  protected String[] getConfigLocations() {
    return new String[] {
      &quot;classpath*:META-INF/applicationContext.xml&quot;
    };
  }
  public void testHelloWorld() {
    System.out.print(helloWorld.print());
  }
  ...
}
</pre>
<p>The <em>BeanSubstitutionTest</em> ran successfully and printed out &#8220;Hello Irvine!&#8221; instead of &#8220;Hello Taipei!&#8221;.</p>
<p>Note that even though <em>BeanSubstitutionProcessor</em> returns the substitution source bean instead of the substitution target bean, the target bean is already created by the application context when <em>BeanSubstitutionProcessor</em> kicks in. This means that we have an unused instance of the target bean in the application context.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://eddii.wordpress.com/2006/06/24/spring-bean-substitution/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
		
		<media:content url="https://2.gravatar.com/avatar/e9e66af7a8b6b12dffddb1a71f12de07f91f093e1acfe76a60301f61b22236a7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">eddie</media:title>
		</media:content>
	</item>
	</channel>
</rss>
