<?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>Pramatr Blog &#187; Examples</title>
	<atom:link href="http://www.pramatr.com/blog/tag/examples/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pramatr.com/blog</link>
	<description>A collection of articles from pramatr.com on technology, security, software and anything we find interesting</description>
	<lastBuildDate>Mon, 29 Mar 2010 19:48:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spring Recipes: A Problem-Solution Approach</title>
		<link>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/</link>
		<comments>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 14:04:13 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Resources]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=421</guid>
		<description><![CDATA[I recently had the pleasure of reading Spring Recipes: A Problem-Solution Approach. I first began reading the sample chapters released on the authors website, so when the book was finally finished I was really looking forward to getting hold of a copy. When it arrived I was amazed to see that it weighed in at [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.apress.com/book/view/1590599799"><img class="alignleft size-medium wp-image-535" title="springrecipes" src="http://pramatr.files.wordpress.com/2009/02/springrecipes.jpg?w=228" alt="springrecipes" width="228" height="300" /></a>I recently had the pleasure of reading <a href="http://apress.com/book/view/1590599799">Spring Recipes: A Problem-Solution Approach</a>. I first began reading the sample chapters released on the authors website, so when the book was finally finished I was really looking forward to getting hold of a copy. When it arrived I was amazed to see that it weighed in at a hefty 752 pages, but that really shouldn&#8217;t put you off.</p>
<p>Spring Recipes covers Spring 2.5 (and earlier versions) taking it from the simplest concepts right through to the most advanced level. The book is divided into nineteen chapters organised into three parts; Core, Fundamentals and Advanced. Each part builds on the previous ones to grow your understanding of all aspects of the Spring framework, for this reason the book is best read cover to cover.</p>
<p>This book is in no way filler and is packed full of content covering not only the <a href="http://www.springsource.org/about">Spring framework</a> and but also several common Spring <a href="http://www.springsource.org/projects">projects</a> including; <a href="http://static.springframework.org/spring-security/site/index.html">Spring Security</a>, <a href="http://www.springsource.org/webflow">Spring Web Flow</a> and <a href="http://static.springframework.org/spring-ws/sites/1.5/">Spring Web Services</a>. For anyone that knows Spring (and it&#8217;s associated projects), this is a huge amount of information to cover in just one book, but this book really does deliver!</p>
<p>As the author puts it; <em>“Reading Spring Recipes will help you master the most important 80 percent of Spring&#8217;s features with just 20 percent of the effort you would need to learn them all.”</em> If you are already using Spring, this book will serve as an invaluable reference and companion guide for future development. If you are a newcomer to Spring, this book will serve as an excellent launch pad to get you up, running and productive with Spring very quickly.</p>
<h2><strong>Structure</strong></h2>
<p>Each of the recipes covered in the book follows; a problem, solution and then how it works approach. This approach lays out many of the common FAQ style questions that come up on <a href="http://forum.springframework.org/">Spring Forum</a> in an accessible way; clearly outlining the problem and mapping the path to the solution. This book also acts an excellent companion to the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/">Spring Reference manual</a>, which is sometimes difficult to read in places and lacks the array of examples this book provides.</p>
<h2><strong>Audience</strong></h2>
<p>The intended audience for Spring Recipes, is any Java programmer hoping to learn more about Spring. No prior knowledge of Spring is required as this book covers it all right from core concepts such as inversion of control (or Ioc). This book is accessible to anyone who has heard the hype about Spring and simply wants to learn more about it and how they can leverage it&#8217;s power. This book gives Java developers hands-on experience using the Spring framework with real-world examples, this experience can be transferred straight into your Spring project.</p>
<h2><strong>Chapters</strong></h2>
<h3><strong>Part 1: Core</strong></h3>
<p>This part focuses on the core concepts and mechanisms of the Spring framework. The chapters in this part aim to familiarize you with Spring&#8217;s core. As a newcomer to Spring <strong>Chapter 1</strong> introduces the concept of inversion of control the core foundation of Spring. It explains how and IoC container works, but also why it is useful and beneficial to use one. This chapter serves as an excellent introduction to IoC and is invaluable to anyone who isn&#8217;t quite sure why they should use a container based approach. This chapter is a <strong>must</strong> for anyone new to Spring or anyone trying to deliver it to a new audience.</p>
<p><strong>Chapters 2-4</strong> build on the IoC introduction, explaining how to setup and configure a new Spring project and apply the basics of Spring configuration. These first four chapters are the foundation for the rest of the book and are a must read for anyone who has never used Spring before and even advanced users may find useful hints and tips within. <strong>Chapters 5-6</strong> cover the extremely powerful approach of <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>, introducing <a href="http://en.wikipedia.org/wiki/Cross-cutting_concern">crosscutting concerns</a> and how these can be configured with Spring. This technique can be extremely useful to provide trace logging, caching and other crosscutting concerns such as security. This is a <strong>must</strong> read for anyone using Spring, even if you are not directly leveraging <a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html">Spring AOP</a> or <a href="http://www.eclipse.org/aspectj/">AspectJ</a>. Spring AOP lays the foundation for introducing how Spring provides features such as transparent transaction support and method level authorization checking.</p>
<h3><strong>Part 2 : Fundamentals</strong></h3>
<p>This part has a split focus, it introduces Spring&#8217;s Data Access support, Web integration and also Testing support. <strong>Chapter 7</strong> introduces one of the most important aspects of Spring&#8217;s <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring-middle-tier.html">data access support</a> in the form of <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html">JdbcTemplate</a>. This shows the reader how they can use JdbcTemplate to prevent the use of direct JDBC and instead let Spring handle all of the boiler plate code. As JdbcTemplate removes the need for the common try/catch/finally/try/catch code seen with JDBC, the code is much cleaner and much more readable. Along with this support, the Spring <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/dao/DataAccessException.html">DataAccessException</a> hierarchy is introduced which provides a rich set of common exceptions to provide a more granular handling approach.</p>
<p><strong>Chapter 8</strong> introduces Spring&#8217;s excellent <a href="http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html">transaction management</a> support and explains in great detail all of the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative-txadvice-settings">various</a> propagation behaviours and isolation levels. This chapter is one of my favourites as it not only introduces the concept of Spring transaction support but transactions in general. It&#8217;s accessible to everyone and extremely useful when mentoring new developers.</p>
<p><strong>Chapter 9</strong> discusses how to integrate Spring with existing ORM frameworks, making their usage within the application seamless. This provides multiple approaches to integration allowing you to build on existing Spring classes, or completely decouple your classes from Spring removing the need for any Spring imports within your code. <strong>Chapters 10-11</strong> discuss Spring&#8217;s popular MVC framework and also discuss how many other leading web frameworks can be integrated with a Spring application. As many of the projects I have worked on in the past have used <a href="http://struts.apache.org/">Struts</a>, Chapter 11 was particularly useful to outline all of the options available to use Spring and Struts together.</p>
<p>This part finishes with the introduction of <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html">Spring Testing Support</a>, teaching you not only what features Spring has to offer but also how it integrates with popular testing frameworks such as <a href="http://www.junit.org/">JUnit</a> and <a href="http://testng.org">TestNG</a>. As the Spring Testing support has been overhauled, it was great that this book covered the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html#testcontext-framework">TestContext</a>framework, but also legacy test classes to cover older versions of JUnit and TestNG. This chapter helps you leverage not only IoC for your tests but also transaction support, mock testing and also MVC integration testing. This approach enables you to easily decouple your code from your tests, providing first class support for all your testing needs.</p>
<h3><strong>Part 3 : Advanced</strong></h3>
<p>For anyone that has mastered the first twelve chapters, <strong>Chapter 13</strong> is where things start to get really interesting. This chapter is dedicated to one of the most scary Spring projects, namely <a href="http://static.springsource.org/spring-security/site/index.html">Spring Security</a> (formerly known as Acegi). Spring Security provides a full range of authentication, authorization and access control options which allow it to secure a standalone Java application or a web-based application. This book focuses on version 2.x onwards which <a href="http://blog.springsource.com/2008/04/17/spring-security-20-final-release-no-more-dead-fairies/">dramatically</a> reduced the size of the required configuration and also its complexity. Spring Security can be a difficult subject to tackle as it&#8217;s often hard for developers to understand it&#8217;s concepts, but this chapter builds security into the example application in piecemeal steps. This demystifies many of the problematic areas of Spring Security whilst addressing the common problems a new developer might face.</p>
<p><strong>Chapter 14</strong> covers the portlet application development using the Spring Portlet MVC framework, highlighting the portlet-specific features which differ from the standard Spring MVC framework. If you don&#8217;t require the portlet features currently within your application, this chapter can safely be skipped as the next one is another gem. <strong>Chapter 15</strong> covers the <a href="http://www.springsource.org/webflow">Spring Web Flow</a> framework which addresses the three major pain–points facing web application developers: user interface navigation control, state management, and modularity. Spring Web Flow offers a <a href="http://static.springframework.org/spring-webflow/docs/2.0.x/reference/htmlsingle/spring-webflow-reference.html#flow-overview">flow</a> definition language which encapsulates a reusable sequence of steps that can execute in different contexts. This framework is extremely flexible and powerful, but this chapter provides a great introduction into what is a very large subject.</p>
<p><strong>Chapter 16</strong> introduces the remoting integration that Spring provides for various technologies such as RMI, Hessian, Burlap, HTTP Invoker, and Web Services. Having already been familiar with many of these technologies, this chapter provides an excellent insight into how these can be integrated with Spring and how quickly this can be achieved. This chapter concludes with a look at Spring Web Services including how to develop contract-first web services. <strong>Chapter 17</strong> discusses how to develop EJB 2.x and 3.0 components with Spring&#8217;s EJB support, and how to use Spring&#8217;s JMS support to simplify sending, receiving, and listening to JMS messages. This introduces a powerful concept of <a href="http://blog.springsource.com/2006/08/11/message-driven-pojos/">message-driven POJOs</a>for those who like the concept of message-driven beans (MDBs) but don&#8217;t want the restrictions of an EJB container.</p>
<p><strong>Chapter 18</strong> demonstrates how easily Spring beans can be exported as JMX MBeans with relative ease in comparison to the typical approach. It continues to extend this by providing remote access, auto-detection and notification. This chapter continues by introducing Spring&#8217;s email support which removes the reliance on <a href="http://java.sun.com/products/javamail/">JavaMail</a> specific mail sessions and exceptions. This provides an abstraction which is independent making it easy to switch to another e-mail API. This chapter finishes with a look at the options available for scheduling tasks within the Spring framework, this integrates with the JDK Timer and also <a href="http://www.opensymphony.com/quartz/">Quartz</a> scheduler.</p>
<p><strong>Chapter 19</strong>, this part and the book concludes with a look at the various scripting languages that can used with Spring applications including; JRuby, Groovy and BeanShell. One of the most powerful features of using scripts is that any changes made to these can be detected by Spring and thus the beans can be <a href="http://static.springframework.org/spring/docs/2.5.x/reference/dynamic-language.html#dynamic-language-refreshable-beans">refreshed</a> automatically. This is of great benefit when you wish to avoid the long compile, deploy and test cycles or the application cannot be taken down regularly for maintenance. This chapter demonstrates how scripted beans can be added to your application quickly and simply using Spring.</p>
<h2><strong>Summary</strong></h2>
<p>This book has undertaken a mammoth task, trying to squeeze Spring and associated projects all into one book. This book is very clearly written, with well-structured, concise examples that get to the heart of the problem without complicating it with unnecessary detail. The examples provide solutions to real world problems instead of taking the form of the simplistic examples found in many other technology books. My only real minor criticism of this book is it&#8217;s scope and size. Given the huge undertaking, there are obviously going to be some areas which only provide a minimal amount of information. This should be expected given the vast array of information covered, but there are a range of <a href="http://www.apress.com">Apress</a> <a href="http://www.apress.com/book/view/1430216247">books</a> which can build on this foundation. I would still give this book five out of five and rate it as one of the best framework specific books I&#8217;ve read.</p>
<p>This book should accompany anyone starting their journey with the Spring framework and even seasoned developers. It can take you from knowing nothing about the Spring framework to knowing pretty much everything you need to create a fully functional web application with all the bells and whistles you&#8217;d expect. It provides information and examples on numerous integration points with other popular open source frameworks and languages allowing you to truly become a Spring expert. Read it, read it again and keep it on your bookshelf as a solid reference for the Spring framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SQL n + 1 Selects Explained</title>
		<link>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/</link>
		<comments>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 06:00:47 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Improvement]]></category>
		<category><![CDATA[Jdbc]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Smells]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=426</guid>
		<description><![CDATA[The SQL n + 1 selects problem is extremely common but I have often found that many people have either never heard of it or simply don&#8217;t understand it. It is actually very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Problems like this [...]]]></description>
			<content:encoded><![CDATA[<p>The SQL n + 1 selects problem is extremely common but I have often found that many people have either never heard of it or simply don&#8217;t understand it. It is actually very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Problems like this are best explained with an example; so imagine we have a table called users and another called user_roles. These tables are setup with a one-to-many relationship, meaning that one user (e.g. jsmith) can have many roles (e.g. Administrator, Auditor, Developer). Many people might implement something like this;</p>
<blockquote>
<pre><span style="font-size:small;"><strong>public</strong> <span style="color:#2040a0;">Iterable</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">allUsers</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUsers</span> <span style="color:#4444ff;">=</span> <span style="color:#008000;">"select users.username, users.email, " +</span>
        <span style="color:#008000;">"users.last_password_change from users"</span><span style="color:#4444ff;">;</span>
    <strong>return</strong> <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUsers</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span><span style="color:#4444ff;"><strong>}</strong></span>,
                <strong>new</strong> <span style="color:#2040a0;">ParameterizedRowMapper</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">mapRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span>, <strong>int</strong> <span style="color:#2040a0;">rowNumber</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">username</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"username"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">email</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"email"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">Date</span> <span style="color:#2040a0;">lastPasswordChange</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getDate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"last_password_change"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">DefaultUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">email</span>, <span style="color:#2040a0;">lastPasswordChange</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">addRolesToUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>return</strong> <span style="color:#2040a0;">user</span><span style="color:#4444ff;">;</span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span>

<strong>private</strong> <strong>void</strong> <span style="color:#2040a0;">addRolesToUser</span><span style="color:#4444ff;"><strong>(</strong></span><strong>final</strong> <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUserRoles</span> <span style="color:#4444ff;">=</span> <span style="color:#008000;">"select role_name from user_roles where username = ?"</span><span style="color:#4444ff;">;</span>
    <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUserRoles</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span> <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">getPrincipalName</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>}</strong></span>,
                <strong>new</strong> <span style="color:#2040a0;">RowCallbackHandler</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <strong>void</strong> <span style="color:#2040a0;">processRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">rolename</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"role_name"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">addRole</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span></span></pre>
</blockquote>
<p>Reviewing the code we can see one query is executed to retrieve the users, the problem here is for each user another SQL statement needs to be executed to retrieve the roles. If the first query retrieved one user, this would require one additional query to retrieve the roles. If the first query retrieved a hundred users, this would require one hundred additional queries to retrieve the roles. The pattern will always be the same, one query for the users and n queries dependent on the number of users found, thus n + 1. Although this solution is functional, it does result in many unnecessary SQL statements being executed.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>select</strong> users.username, users.email, users.last_password_change <strong>from</strong> users;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
<strong>select</strong> role_name <strong>from</strong> user_roles <strong>where</strong> username = ?;
...</span></pre>
</blockquote>
<p>Shared resources are typically the bottleneck in most applications, so expensive or unnecessary SQL should be avoided if possible. As the application attempts to scale, this bottleneck can become extremely problematic and severely inhibit application performance. Fortunately this is a simple solutions to this problem; introducing a join into the query.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>public</strong> <span style="color:#2040a0;">Iterable</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">allUsers</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
    <strong>final</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">selectUsers</span> <span style="color:#4444ff;">=</span>
        <span style="color:#008000;">"select users.username, users.email, users.last_password_change, user_roles.role_name "</span>
            <span style="color:#4444ff;">+</span> <span style="color:#008000;">"from users left join user_roles on (users.username = user_roles.username)"</span><span style="color:#4444ff;">;</span>
    <strong>final</strong> <span style="color:#2040a0;">Map</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">String</span>, <span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span> <span style="color:#2040a0;">users</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">HashMap</span><span style="color:#4444ff;">&lt;</span><span style="color:#2040a0;">String</span>, <span style="color:#2040a0;">User</span><span style="color:#4444ff;">&gt;</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
    <span style="color:#2040a0;">getJdbcTemplate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span>.<span style="color:#2040a0;">query</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">selectUsers</span>, <strong>new</strong> <span style="color:#2040a0;">Object</span><span style="color:#4444ff;"><strong>[</strong></span><span style="color:#4444ff;"><strong>]</strong></span> <span style="color:#4444ff;"><strong>{</strong></span><span style="color:#4444ff;"><strong>}</strong></span>, <strong>new</strong> <span style="color:#2040a0;">RowCallbackHandler</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
        <strong>public</strong> <strong>void</strong> <span style="color:#2040a0;">processRow</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">ResultSet</span> <span style="color:#2040a0;">resultSet</span><span style="color:#4444ff;"><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">SQLException</span> <span style="color:#4444ff;"><strong>{</strong></span>
            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">username</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"username"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>if</strong> <span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;">!</span><span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">containsKey</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
                <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">email</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"email"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">Date</span> <span style="color:#2040a0;">lastPasswordChange</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getDate</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"last_password_change"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <strong>new</strong> <span style="color:#2040a0;">DefaultUser</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">email</span>, <span style="color:#2040a0;">lastPasswordChange</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">put</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span>, <span style="color:#2040a0;">user</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#4444ff;"><strong>}</strong></span>

            <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">rolename</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">resultSet</span>.<span style="color:#2040a0;">getString</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"role_name"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <strong>if</strong> <span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;">!</span><span style="color:#2040a0;">StringUtil</span>.<span style="color:#2040a0;">isNull</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span> <span style="color:#4444ff;"><strong>{</strong></span>
                <span style="color:#2040a0;">User</span> <span style="color:#2040a0;">user</span> <span style="color:#4444ff;">=</span> <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">get</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">username</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
                <span style="color:#2040a0;">user</span>.<span style="color:#2040a0;">addRole</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">rolename</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
            <span style="color:#4444ff;"><strong>}</strong></span>
        <span style="color:#4444ff;"><strong>}</strong></span>
    <span style="color:#4444ff;"><strong>}</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
    <strong>return</strong> <span style="color:#2040a0;">users</span>.<span style="color:#2040a0;">values</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>
<span style="color:#4444ff;"><strong>}</strong></span></span></pre>
</blockquote>
<p>Although the code and SQL statement are slightly more complex that the original example, it results in much fewer SQL statements being executed. Instead of the n + 1 statements executed in the first example, one statement is executed that fetches all the required data. This typically results in much improved performance and as the numbers scale the improvement in performance can become much more apparent.</p>
<blockquote>
<pre><span style="font-size:small;"><strong>select</strong> users.username, users.email, users.last_password_change, user_roles.role_name
    <strong>from</strong> users left join user_roles <strong>on</strong> <strong>(</strong>users.username = user_roles.username);</span></pre>
</blockquote>
<p>As with all performance optimizations the most important thing is to measure the effect of the improvement. Performance optimizations aren&#8217;t always predictable so by taking measurements before and after the change, you can accurately know if you have actually improved the performance (or made it worse). A SQL join may be the most appropriate way of solving a problem such as this, but there are other alternatives such as <a href="http://ehcache.sourceforge.net/">caching</a> the data instead. Although the SQL n + 1 selects is an extremely common problem, is not always well understood and is often still found within code. It is very easy to introduce a problem like this into your code, but it&#8217;s also very easy to resolve as well. Next time you are viewing your debug output, see if you can spot SQL n + 1 selects.</p>
<p><strong>References</strong></p>
<p>Database access using <a href="http://pramatr.com/2008/08/19/spring-jdbctemplate-the-phantom-performance-problem/">Spring JdbcTemplate</a><br />
Preventing the n + 1 select problem when using <a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/performance.html#performance-fetching-custom">Hibernate</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/05/sql-n-1-selects-explained/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Spring Framework: Where Can I Find Examples And Resources?</title>
		<link>http://www.pramatr.com/blog/2008/09/03/spring-framework-where-can-i-find-examples-and-resources/</link>
		<comments>http://www.pramatr.com/blog/2008/09/03/spring-framework-where-can-i-find-examples-and-resources/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 21:00:00 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Resources]]></category>

		<guid isPermaLink="false">http://pramatr.com/2007/09/03/spring-framework-where-can-i-find-examples-and-resources/</guid>
		<description><![CDATA[When ever I&#8217;m working with Spring, one of the most common questions I get asked is &#8220;where can I find good quality examples and resources?&#8221; I hate to disappoint people that I have some secret stash of information, but I get everything I need from the same places as everyone else! Here&#8217;s a brief overview [...]]]></description>
			<content:encoded><![CDATA[<p>When ever I&#8217;m working with Spring, one of the most common questions I get asked is <em>&#8220;where can I find good quality examples and resources?&#8221;</em> I hate to disappoint people that I have some secret stash of information, but I get everything I need from the same places as everyone else! Here&#8217;s a brief overview of the places I visit:</p>
<ol>
<li><a href="http://static.springframework.org/spring/docs/2.0.x/reference/">Spring Framework reference documentation</a> &#8211; I&#8217;ve found the reference manual to be the best source of information on the framework. Anytime I have a question, the first place I look is here! It&#8217;s constantly updated, and it contains lots of recommendations and best practices from the Spring development team. What better recommendations can you have then the people that wrote it!</li>
<li><a href="http://forum.springframework.org/">Spring Framework support forum</a> &#8211; If you&#8217;re still left with questions after looking through the reference documentation, the Spring support forum provides a <strong>very</strong> active and friendly community. There are thousands of answers already on there, so it really pays to do a quick search before you post. If you do post a question make sure you do it the <a href="http://catb.org/~esr/faqs/smart-questions.html">smart</a> way.</li>
<li><a href="http://blog.springsource.com/">Spring Source Team Blog</a> &#8211; Spring from the source. What better place to get information and updates on what the Spring development team are doing.</li>
<li><a href="http://static.springframework.org/spring/docs/2.0.x/api/">Spring Framework Javadoc</a> &#8211; If you want to find details on the API of the Spring classes, the comprehensive JavaDoc is the place to go.</li>
<li><a href="http://www.springhub.com/">Spring Hub</a> &#8211; An independent site containing lots of links and resources related to Spring.</li>
<li><a href="http://www.springframework.org/support">Spring Framework JIRA</a> &#8211; If you think you&#8217;ve found a bug or want to propose an enhancement, JIRA is the place to go.</li>
<li><a href="http://sourceforge.net/mail/?group_id=73357">Spring Framework Mailing List</a> &#8211; If you want to follow the progress of Spring development, you can view the mailing list here or even subscribe.</li>
<li>Sample Applications &#8211; Spring ships with a number of excellent sample applications. Look through them, play with them and understand them. Many people use these examples as a base for their applications, they are an excellent starting point.</li>
<li>Source code &#8211; One of the most over looked resources that Spring has to offer is it&#8217;s source code. It&#8217;s well structured, well written and easy to follow. It&#8217;s also a great place to get answers. The source code for the unit tests is also included with the release. What more information could you be looking for?</li>
<li><a href="http://www.springframework.org/bookreview">Spring Books</a> &#8211; If you are still looking for more information, there are a wide selection of books available. There are several existing threads on Spring forum that discuss the various books and their relative merits.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2008/09/03/spring-framework-where-can-i-find-examples-and-resources/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
