Results 1 to 2 of 2

Thread: How to troubleshoot "No Session found for current thread" exception on a JSP?

  1. #1
    Join Date
    Apr 2010
    Posts
    104

    Default How to troubleshoot "No Session found for current thread" exception on a JSP?

    Hi,

    I'm using Spring 3.1.0.RELEASE, Spring's transaction manager, and Hibernate 4.0.1.Final. Through Hibernate, I'm retrieving a bean, whose property I want to display on a JSP page. But when I try and display the property on the JSP page, I get the baffling exception ...

    Code:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'user' on field 'userEventFeeds': rejected value [1]; codes [methodInvocation.user.userEventFeeds,methodInvocation.userEventFeeds,methodInvocation.java.util.Set,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.userEventFeeds,userEventFeeds]; arguments []; default message [userEventFeeds]]; default message [Property 'userEventFeeds' threw exception; nested exception is org.hibernate.HibernateException: No Session found for current thread]
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
    	org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    	org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    	org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
    	org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    	org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    It looks like all the data isn't being loaded when I first load the bean. How can I force everything to be eagerly loaded? Here's the part of the JSP where I try and access the property ...

    Code:
    	<form:form method="Post" action="eventfeeds.jsp" commandName="user">
    	number of event feeds: ${fn:length(user.userEventFeeds)}
    Here's the relevant part of the domain object ...

    Code:
    @Entity
    @Table(name = "USERS")
    public class Registration implements UserDetails, Serializable {
    	...
    	@OneToMany(fetch = FetchType.EAGER, targetEntity = EventFeed.class)
        @JoinTable(name = "USER_EVENT_FEEDS", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "EVENT_FEED_ID") })
    	private Set<EventFeed> userEventFeeds = new HashSet<EventFeed>(0);
    Here's the controller where I load in the object ...

    Code:
    @Controller
    @RequestMapping("/eventfeeds.jsp")
    public class UserEventFeedsController {
    	...
    	@ModelAttribute("user")
    	public UserDetails getUser(final Principal principal) {
    		final String username = principal.getName().toLowerCase();
    		final Registration foundUser = userService.findUserByUsername(username);
    		return (UserDetails) foundUser;
    	}	// getUser
    Here's the service ...

    Code:
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Service("userService")
    public class UserServiceImpl implements UserService {
    	...
    	@Override
    	public Registration findUserById(Integer id) {
    		final Registration searchUser = new Registration();
    		if (id != null) { 
    			searchUser.setId(id);
    		}	// if
    		return usersDao.getUser(searchUser);
    	}
    and here's the DAO ...

    Code:
    @Repository("usersDao")
    public class UsersDaoImpl implements UsersDao {
    	
    	@Autowired
    	private SessionFactory sessionFactory;
    
    	...
    	@Override
    	public Registration getUser(final Registration user) { 
    		final Session session = sessionFactory.getCurrentSession();
    		Registration foundUser = null;
    		
    		final Criteria crit = session.createCriteria(Registration.class);
    		final String username = user.getUsername();
    		if (username != null) { 
    			final String formattedUsername = username.trim().toLowerCase();
    			crit.add(Restrictions.eq("username", formattedUsername));
    		}	// if
    		final Integer id = user.getId();
    		if (id != null) { 
    			crit.add(Restrictions.eq("id", user.getId()));
    		}	// if
    		final List results = crit.list();
    		if (results != null && results.size() > 0) { 
    			foundUser = (Registration) results.get(0);	
    		}	// if
    		
    		return foundUser;
    	}	// getUser
    Here's how I configure the application context ...

    Code:
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        	<property name="url" value="jdbc:mysql://localhost:3306/myproj"/>
        	<property name="username" value="myproj"/>
        	<property name="password" value="password"/>
        	<property name="maxActive" value="10"/> 
        	<property name="minIdle" value="5"/> 
        	<!-- SELECT 1 is a simple query that returns 1 row in MySQL -->
        	<property name="validationQuery" value="SELECT 1"/> 
    	</bean>
    
    	<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>com.myco.myproj.domain.Registration</value>
    				<value>com.myco.myproj.domain.Role</value>
    				<value>com.myco.myproj.domain.EventFeed</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="show_sql">true</prop>
    				<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    		p:sessionFactory-ref="sessionFactory" />
    
    	<tx:annotation-driven />
    Any ideas how to attack this problem? - Dave

  2. #2
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, see this post, may be useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •