<?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>Molindo Techblog &#187; Spring</title>
	<atom:link href="http://techblog.molindo.at/category/java/spring/feed" rel="self" type="application/rss+xml" />
	<link>http://techblog.molindo.at</link>
	<description>Molindo Techblog – formerly known as talk-on-tech.blogspot.com</description>
	<lastBuildDate>Tue, 20 Dec 2011 10:22:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Custom reuseable LoadableDetachableModel for Spring/Hibernate</title>
		<link>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-custom-reuseable-loadabledetachablemodel-for-springhibernate</link>
		<comments>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html#comments</comments>
		<pubDate>Wed, 21 May 2008 16:28:00 +0000</pubDate>
		<dc:creator>Michael Sparer</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html</guid>
		<description><![CDATA[the annoyance org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closedorg.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed the problem Generally, detached objects in Hibernate are a bad thing when it comes to reusing objects between web-requests. Detached objects are objects that have been persistent but are not attached [...]]]></description>
			<content:encoded><![CDATA[<h2>the annoyance</h2>
<p><code><br />org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closed<br />org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed<br /></code><br />
<h2>the problem</h2>
<p>Generally, detached objects in Hibernate are a bad thing when it comes to reusing objects between web-requests. Detached objects are objects that have been persistent but are not attached to a Hibernate session. They&#8217;re easily produced but can be a mess if it comes to Lazy-Loading or updating them. Just consider the following case</p>
<p><code><br />Foo foo = _myDAO.getFoo(); // attached object</p>
<p>add(new Link("fooLink", new Model(foo)) {</p>
<p>&nbsp;&nbsp;protected void onClick() {<br />&nbsp;&nbsp;&nbsp;&nbsp;Foo foo = (Foo) getModelObject(); // now it's detached as this is called in the next request<br />&nbsp;&nbsp;&nbsp;&nbsp;Bar bar = foo.getBar(); // possible lazy-loading exception if bar gets loaded lazily  </p>
<p>&nbsp;&nbsp;}<br />}<br /></code></p>
<h2>the solution</h2>
<p>First be sure to use the OpenSessionInViewFilter sothat a new hibernate session is opened when the request gets attached and closed when it gets detached.</p>
<p>Then the solution of course is a to use a LoadableDetachableModel that gets a fresh instance from the persistence-layer on each request. Could look like that:</p>
<p><code><br />IModel fooModel = new LoadableDetachableModel() {</p>
<p>&nbsp;&nbsp;protected Object load() {<br />&nbsp;&nbsp;&nbsp;&nbsp;return _myDAO.getFoo();<br />&nbsp;&nbsp;}<br />}</p>
<p>add(new Link("fooLink", fooModel) {</p>
<p>&nbsp;&nbsp;protected void onClick() {<br />&nbsp;&nbsp;&nbsp;&nbsp;Foo foo = (Foo) getModelObject(); // attached as LDM has loaded a fresh instance<br />&nbsp;&nbsp;&nbsp;&nbsp;Bar bar = foo.getBar(); // no lazy loading exception as still attached  </p>
<p>&nbsp;&nbsp;}<br />}<br /></code></p>
<h2>the improvement</h2>
<p>
<p>LoadableDetachableModels are a nice thing but it&#8217;s quite a lot of work to inject your DAO (or service) and then create a new LDM for each of your objects in each and every one of your panels. so we came up with a more generic version of the LoadableDetachableModel. First, let your model-objects implement an Interface, say IPersistentObject with the single method</p>
<p><code><br />&nbsp;&nbsp;Serializable getId();<br /></code></p>
<p>As your object are likely to have a primary key named id it&#8217;ll be no problem anyway. Then create the following class:</p>
<p><code><br />public class PersistentObjectModel extends LoadableDetachableModel {</p>
<p>&nbsp;&nbsp;private static final long serialVersionUID = 1L;</p>
<p>&nbsp;&nbsp;private final Class _clazz;</p>
<p>&nbsp;&nbsp;private final Serializable _id;</p>
<p>&nbsp;&nbsp;@SpringBean(name = "myDao")<br />&nbsp;&nbsp;private IMyDao _myDao;</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />&nbsp;&nbsp;public PersistentObjectModel(final T object) {<br />&nbsp;&nbsp;&nbsp;&nbsp;super(object);</p>
<p>           // object may be a hibernate proxy, so get the actual class<br />&nbsp;&nbsp;&nbsp;&nbsp;if (object instanceof HibernateProxy) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_clazz = (Class) ((HibernateProxy) object).getHibernateLazyInitializer()<br />                                   .getImplementation().getClass();<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_clazz = (Class) object.getClass();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;_id = object.getId();<br />&nbsp;&nbsp;&nbsp;&nbsp;// inject the bean<br />&nbsp;&nbsp;&nbsp;&nbsp;InjectorHolder.getInjector().inject(this);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public PersistentObjectModel(final Class clazz, final Serializable id) {<br />&nbsp;&nbsp;&nbsp;&nbsp;clazz = clazz;<br />&nbsp;&nbsp;&nbsp;&nbsp;_id = id;<br />&nbsp;&nbsp;&nbsp;&nbsp;// inject the bean<br />&nbsp;&nbsp;&nbsp;&nbsp;InjectorHolder.getInjector().inject(this);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;protected T load() {<br />&nbsp;&nbsp;&nbsp;&nbsp;// the DAO then simply passes the params to hibernate (or spring's hibernatetemplate<br />&nbsp;&nbsp;&nbsp;&nbsp;return _myDao.getById(_clazz, _id);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;public T getObject() {<br />&nbsp;&nbsp;&nbsp;&nbsp;return (T) super.getObject();<br />&nbsp;&nbsp;}</p>
<p>}<br /></code></p>
<p>This makes working with hibernate objects really easy. There are two options:</p>
<p></p>
<ol>
<li>Use the object e.g.
<p>setModel(new PersistentObjectModel(foo));</p>
<p></li>
<p>
<li>Use only class and id.
<p>This might happen e.g. if you pass your id using PageParameters (but better encrypt it <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )<br />setModel(new PersistentObjectModel(Foo.class, 1));</p>
<p></li>
<p></ol>
<p>
<p>Hope that saved you some time writing boilerplate code <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  have fun</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Capacity planning</title>
		<link>http://techblog.molindo.at/2007/12/capacity-planning.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=capacity-planning</link>
		<comments>http://techblog.molindo.at/2007/12/capacity-planning.html#comments</comments>
		<pubDate>Tue, 04 Dec 2007 12:48:00 +0000</pubDate>
		<dc:creator>Stefan Fußenegger</dc:creator>
				<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2007/12/capacity-planning.html</guid>
		<description><![CDATA[Today, I discovered a nice little gotcha in the Spring source, namely in org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor, while I was implementing a priority task queue. The mentioned class uses a template method to create the backing queue for pending tasks: protected BlockingQueue createQueue(int queueCapacity) {...} I&#8217;ve overridden this method like so: protected BlockingQueue createQueue(int queueCapacity) {return new PriorityBlockingQueue(queueCapacity, [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I discovered a nice little gotcha in the Spring source, namely in <a href="http://www.koders.com/java/fid9BF985B758BA3A8B0CF26D967F726B90D67FC1A3.aspx?s=org.springframework.scheduling.concurrent">org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor</a>, while I was implementing a priority task queue. The mentioned class uses a <a href="http://en.wikipedia.org/wiki/Template_method_pattern">template method</a> to create the backing queue for pending tasks:</p>
<p><code>protected BlockingQueue createQueue(int queueCapacity) {...}</code></p>
<p>I&#8217;ve overridden this method like so:</p>
<p><code>protected BlockingQueue createQueue(int queueCapacity) {<br />return new PriorityBlockingQueue(queueCapacity, new Comparator() {...});<br />}</code></p>
<p>However, this didn&#8217;t work as I got:</p>
<p><code>java.lang.OutOfMemoryError: Requested array size exceeds VM limit</code></p>
<p>Why? By default, the ThreadPoolTaskExecutor uses</p>
<p><code>new LinkedBlockingQueue(queueCapacity);</code></p>
<p>With a default <code>queueCapacity</code> of <code>Integer.MAX_VALUE</code> &#8230; This is ok, as the capacity of a LinkedBlockingQueue is its <span style="font-weight: bold">maximum capacity</span> while the capacity of the PriorityBlockingQueue is its <span style="font-weight: bold">initial capacity</span> (and used to create a array of the given size).  Unfortunately, it isnt&#8217;t even possible to limit the size of PriorityBlockingQueue at all. Therefore, the parameter should be ignored for PriorityBlockingQueues.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2007/12/capacity-planning.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tapestry/Spring Integration</title>
		<link>http://techblog.molindo.at/2007/08/tapestryspring-integration.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tapestryspring-integration</link>
		<comments>http://techblog.molindo.at/2007/08/tapestryspring-integration.html#comments</comments>
		<pubDate>Wed, 08 Aug 2007 15:11:00 +0000</pubDate>
		<dc:creator>Michael Sparer</dc:creator>
				<category><![CDATA[Maven]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Tapestry]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2007/08/tapestryspring-integration.html</guid>
		<description><![CDATA[In the last weeks I started to have a look at different web frameworks and came across Tapestry5. Having watched the screencasts on the website, I began to like it straight away. Although still in development (the release is planned in fall 2007), it offers some nice features. The most impressive one is that you [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:100%"><span style="font-family:arial">In the last weeks I started to have a look at different web frameworks and came across <a href="http://tapestry.apache.org/tapestry5/">Tapestry5</a>. Having watched the <a href="http://tapestry.apache.org/tapestry5/screencast.html">screencasts</a> on the website, I began to like it straight away. Although still in development (the release is planned in fall 2007), it offers some nice features. The most impressive one is that you do not have to restart the servlet-container to apply changes in java files. But for me personally, the features I appreciate the most are that you don&#8217;t have to bother with jsp-specific stuff (such as  &#8211; Ugh!) and it lets you get rid of the servlet mapping stuff in the web.xml. That&#8217;s nice isn&#8217;t it? I also read some posts claiming that <a href="http://wicket.apache.org/features.html">Apache Wicket</a> already provides such features. I started reading the Wicket docs, but stopped when I saw that there&#8217;s a need to inherit from a Wicket base-class <span style="font-weight: bold">and</span> to define servlet-mappings in the web.xml &#8211; as stated above, I don&#8217;t like too many manual servlet-mappings and I also prefer dealing with <a href="http://en.wikipedia.org/wiki/Plain_Old_Java_Object">POJOs</a>.</p>
<p></span><span style="font-family:arial">After playing around with Tapestry by implementing the screencasts and the tutorial, I tried to inject <a href="http://www.springframework.org/">Spring2 </a>managed-beans into a Tapestry5 app. I found a load of examples/tutorials online, but they either dealt with tapestry4 and/or spring1.2.x. There also is a <a href="http://tapestry.apache.org/tapestry5/tapestry-spring/index.html">tutorial</a> on the Tapestry site from its creator <a href="http://tapestryjava.blogspot.com/">Howard Lewis Ship</a>. Unfortunately, he missed pointing out that it is vital to add another dependency, the tapestry-spring library. </span><span style="font-family:arial">Worth mentioning is, that the tapestry-spring dependency differs from the tapestry-spring.jar available on the <a href="http://howardlewisship.com/tapestry-javaforge/tapestry-spring/">project page</a> which I used at first. It took me quite a while to find out what&#8217;s wrong until I found a hint in the tapestry-mailing-list. </span><span style="font-family:arial">A user there suggested to add the tapestry-spring dependency to your maven2 pom.xml. After including the library, a simple @Inject was enough to inject a spring-bean into the tapestry-class. The dependency should look as follows:</span>
<pre>&lt;dependency&gt; &lt;groupid&gt;org.apache.tapestry&lt;/groupid&gt; &lt;artifactid&gt;tapestry-spring&lt;/artifactid&gt; &lt;version&gt;${tapestry-release-version}&lt;/version&gt;&lt;/dependency&gt;</pre>
<p><span style="font-family:arial">The hacks I did with Tapestry5 made me to get in touch with <a href="http://maven.apache.org/">Maven2</a> for the first time. Together with the <a href="http://m2eclipse.codehaus.org/">eclipse-plugin</a>, it was very easy to add dependencies to the project and to build and pack the application. That&#8217;s of course only the tip of the iceberg, and I&#8217;ll definitely have to get a bit more into maven &#8211; as it really looks like it offers some nice features. And also I have to prove if <a href="http://jelmer.jteam.nl/?p=21">Jelmer Kupurus&#8217;s statement</a> is right or not <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></p>
<p><span style="font-family:arial">Enough for now, I&#8217;d love to receive some comments with your experience with Tapestry5 and Spring integration!</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2007/08/tapestryspring-integration.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

