title: Lazy Initialization Errors from Refreshes
body: <p>How can I prevent LazyInitializationExceptions from being thrown when a page is requested multiple times?  If I simply hold Ctrl-R on a page in my webapp, I consistently receive this message in my log files.</p>
  
  <p>I have the following interceptor configured in my servlet.xml file:</p>
  
  <pre><code>&lt;mvc:interceptors&gt;
    &lt;bean 
     class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor" /&gt;  
  &lt;/mvc:interceptors&gt;
  </code></pre>
  
  <p>Yet I constantly receive the following errors:</p>
  
  <pre><code>2011-09-23 15:14:28,854 [http-8080-23] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/web-app].[springmvc]  - Servlet.service() for servlet springmvc threw exception
  org.hibernate.LazyInitializationException: illegal access to loading collection
  at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366)
  at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
  at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
  </code></pre>
  
  <p>Note: Turning logging on the Interceptor I clearly see that it's being invoked and opening / closing transactions:</p>
  
  <pre><code>2011-09-23 15:36:53,229 [http-8080-5] DEBUG org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Opening single Hibernate Session in OpenSessionInViewInterceptor
  2011-09-23 15:36:53,229 [http-8080-5] WARN  eqip.core.springmvc.extensions.interceptors.AbstractAgencyDataInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Pre handle: http://134.167.141.34:8080/web-app/main.xhtml Status: 200
  </code></pre>
  
  <p>2011-09-23 15:36:53,511 [http-8080-5] WARN  org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Narrowing proxy to class core.model.entities.Subclass - this operation breaks ==
  2011-09-23 15:36:53,511 [http-8080-5] WARN  org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Narrowing proxy to class core.model.entities.Subclass - this operation breaks ==
  2011-09-23 15:36:53,916 [http-8080-5] DEBUG org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Flushing single Hibernate Session in OpenSessionInViewInterceptor
  2011-09-23 15:36:53,916 [http-8080-5] DEBUG org.springframework.web.servlet.DispatcherServlet IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Rendering view [eqip.core.springmvc.extensions.velocity.VelocityToolsLayoutView: name 'pages/myEqip'; URL [pages/main.xhtml]] in DispatcherServlet with name 'springmvc'
  2011-09-23 15:36:54,213 [http-8080-5] DEBUG eqip.core.springmvc.extensions.velocity.VelocityToolsLayoutView IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Rendering screen content template [pages/main.xhtml]
  2011-09-23 15:36:54,384 [http-8080-5] DEBUG org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Closing single Hibernate Session in OpenSessionInViewInterceptor</p>
  
  <p>Using Spring 3.0.5, Hibernate 3.6.5, velocity 1.7</p>
  
  <p><strong>Final Fix</strong>: Was adding the following to our controller declarations: </p>
  
  <pre><code>@Scope(BeanDefinition.SCOPE_PROTOTYPE) 
  </code></pre>
  
  <p>This allowed us to keep use of our interceptors and ensure that we were getting new copies of our pre-loaded pieces on each request.</p>