Note that there are some explanatory texts on larger screens.

plurals
  1. PONo Hibernate Session bound to thread
    primarykey
    data
    text
    <p>I am using Spring MVC and Hibernate.</p> <p>I want to use the OpenSessionInViewFilter to allow lazy loading to work properly in the view layer.</p> <p>The OpenSessionInViewFilter requires a root application context so i added a ContextLoaderListener and moved my non view related configuration files to it from the DispatcherServlet.</p> <p>The app-config.xml config file contains the datasource related beans.</p> <p>When i load the app-config.xml using the ContextLoaderListener instead of DispatcherServlet, i get the error message</p> <pre><code>WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/XXXX/app/jobs] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet' </code></pre> <p>UPDATE: fixed this error by adding a component-scan to mvc-config.xml</p> <p>but now i get</p> <pre><code>org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here </code></pre> <p>My understanding is that DispatcherServlet inherits beans from the root context, so moving definitions from the servlet to the root context should make no difference.</p> <p><strong>web.xml</strong></p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;singleSession&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;flushMode&lt;/param-name&gt; &lt;param-value&gt;AUTO&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;sessionFactoryBeanName&lt;/param-name&gt; &lt;param-value&gt;sessionFactory&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/app-config.xml /WEB-INF/spring/other-config.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Handles all requests into the application --&gt; &lt;servlet&gt; &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/mvc-config.xml &lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;!-- Maps all /app requests to the DispatcherServlet for handling --&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/app/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p><strong>app-config.xml</strong></p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt; &lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt; &lt;context:component-scan base-package="com.mycompany.app" /&gt; &lt;!-- SQL Server --&gt; &lt;bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:jtds:sqlserver://x.x.x.x/XXX" /&gt; &lt;property name="username" value="XXX" /&gt; &lt;property name="password" value="XXX" /&gt; &lt;/bean&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="datasource" /&gt; &lt;property name="configLocation"&gt; &lt;value&gt;/WEB-INF/hibernate.cfg.xml&lt;/value&gt; &lt;/property&gt; &lt;property name="configurationClass"&gt; &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;value&gt;hibernate.dialect=org.hibernate.dialect.SQLServerDialect&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;tx:annotation-driven /&gt; &lt;bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p><strong>mvc-config.xml</strong></p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt; &lt;!-- Configures support for @Controllers --&gt; &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="com.mycompany.app" /&gt; &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt; &lt;property name="definitions"&gt; &lt;list&gt; &lt;value&gt;/WEB-INF/defs/general.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload