Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Jan 16th, 2006, 10:30 AM
cosmin cosmin is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Default using OpenSessionInViewFilter

I am trying to use the OpenSessionInViewFilter , but I am not entirely confortable with the examples i've found online and I have not managed to get it to work yet .

I am using Tapestry for the presentation layer , and at times , i'm returning data objects from hibernate that have been lazily initialised . The Tapestry code will call some of the lazy values , and I'm getting session closed excecptions .

I have read about the OpenSessionInViewFilter on this forum , since it seems to be a common solution for my question ( allthogh everyone else uses Struts ) . From what I gathered it looks like i need to add the following to my web.xml :
Code:
<filter>
	  <filter-name>hibernateFilter</filter-name>
	  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>


	<filter-mapping>
	  <filter-name>hibernateFilter</filter-name>
	  <url-pattern>/app</url-pattern> 
	</filter-mapping>
Tried that but I am still getting exceptions ... Is there anything else I need to do to configure this filter properly ?

Thanks
Reply With Quote
  #2  
Old Jan 17th, 2006, 02:50 AM
jmelzer jmelzer is offline
Junior Member
 
Join Date: Dec 2005
Posts: 17
Default Getting around the lazy problem

I use
Code:
Hibernate.initialize(object);
when I load the objects for a specified site, e.g. I implement a special method for a user search when I know the dependend objects will be loaded. Otherwise it is not garanteed that the correct hibernate session will bound to this object. (Think about havy load.)

regards,
Juergen
Reply With Quote
  #3  
Old Jan 17th, 2006, 11:16 AM
cosmin cosmin is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Default

where do you have that code . I'm affraid i'm using Tapestry which uses ognl , which is something similar to the JSP EL language . most of my time i get objects like this and theese would cause problems .

I need something that doesn't require me writting code in the presentation layer if possible

is that possible ?
Reply With Quote
  #4  
Old Jan 18th, 2006, 05:59 AM
jmelzer jmelzer is offline
Junior Member
 
Join Date: Dec 2005
Posts: 17
Default

You have to write it there where the transaction is fired for getting the objects...
In our application we make the transaction binding everytime a Manager is called. After the query is running I call the Hibernate.initialize(object)
Reply With Quote
  #5  
Old Jan 18th, 2006, 07:02 AM
Costin Leau's Avatar
Costin Leau Costin Leau is offline
Spring DM Lead
Spring Modules TeamSpring Team
 
Join Date: Jan 2005
Location: Bucharest, Romania
Posts: 5,015
Default

You can try this one:
Quote:
<!-- Hibernate OpenSession Filter -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>HBSessionFactory</param-value>
</init-param>
</filter>
Turn on logging and see that the HB session is opened at the beginning and at the end of the request. The internal details of the view (like OGNL) don't make a difference - we are using WebWork (which uses OGNL a lot) and Velocity and OSIV works just fine.
__________________
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
Please use [ c o d e ] [ / c o d e ] tags
Reply With Quote
  #6  
Old Jan 19th, 2006, 12:07 PM
cosmin cosmin is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Default

thanks guys , open filter worked just great , it looks like i kept getting an error due to some bad code in my presentation layer ( this was a test app and i was beeing a bit lazy ) :

transactionManagedBean.getMyDAO().save(myObject);

had to change this to

transactionManagedBean.saveMyObject(myObject);

IT's odd however that i was getting no error before cocnfiguring the filter . . .
Reply With Quote
  #7  
Old Mar 19th, 2006, 05:55 AM
cristis1 cristis1 is offline
Member
 
Join Date: Jan 2006
Location: Bucharest, Romania
Posts: 42
Default

Hello,

I'd like to use the OpenSessionInViewFilter with my SpringApp. I searched the forums/docs and did not find a clear documentation/wiki (in fact the majority of the posts were about problems encountered while trying to implement the filter ).

Do you have links to a post/documentation where this mechanism is well explained?

Thank you,
Cristian.
Reply With Quote
  #8  
Old Jun 9th, 2009, 04:30 AM
digz6666 digz6666 is offline
Member
 
Join Date: Dec 2008
Location: Ulaanbaatar, Mongolia
Posts: 87
Default

I've got this issue and I've declared opensessioninviewfilter in my web.xml file as mentioned above. But the problem still exist.

Here's my web.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/my-servlet.xml</param-value>
	</context-param>
	<filter>
		<filter-name>encoding-filter</filter-name>
		<filter-class>org.eon.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
        	<param-name>forceEncoding</param-name>
        	<param-value>true</param-value>
    	</init-param>
	</filter>
	<filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sessionFactory</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding-filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>myservlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value/>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>myservlet</servlet-name>
		<url-pattern>*.jspx</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>Resource Servlet</servlet-name>
		<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Resource Servlet</servlet-name>
		<url-pattern>/resources/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<error-page>
		<error-code>404</error-code>
		<location>/WEB-INF/pages/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/WEB-INF/pages/error.jsp</location>
	</error-page>
	<login-config>
		<auth-method>BASIC</auth-method>
	</login-config>
</web-app>
Am I doing something wrong?
__________________
Let's care our nature!
Reply With Quote
  #9  
Old Sep 9th, 2009, 10:02 AM
pelirr pelirr is offline
Junior Member
 
Join Date: May 2009
Posts: 4
Default

Hello, I have the same one problem, my web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>

<!-- Parameters to load Spring configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- Se elimina la referencia a comun-web-context a la espera de que se incorpore la parte comun -->
<!-- param-value>classpath*:comun-web-context.xml,classpath*:sgeser-web-context.xml</param-value-->
<param-value>classpath*:sgma-web-context.xml</param-value>
</context-param>


<!-- Parameters to load Tiles configuration -->
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEF INITIONS_CONFIG</param-name>
<param-value>/WEB-INF/sgma-tiles.xml,/org/apache/tiles/classpath-defs.xml</param-value>
</context-param>


<!-- Filtro para Autenticacion de Usuario -->
<!--
<filter>
<filter-name>ControlAcceso</filter-name>
<filter-class>es.aena.sgcomun.base.web.struts2.autenticaci on.NtlmHttpFilter</filter-class>
<init-param>
<param-name>jcifs.netbios.wins</param-name>
<param-value>10.72.101.1</param-value>
</init-param>
<init-param>
<param-name>jcifs.smb.client.domain</param-name>
<param-value>indra</param-value>
</init-param>
</filter>
-->

<!-- Filtro auxiliar para las peticiones POST -->
<!--
<filter>
<filter-name>ControlAccesoPost</filter-name>
<filter-class>es.aena.sgcomun.base.web.struts2.autenticaci on.NtlmHttpPostFilter</filter-class>
</filter>
-->

<!-- Filter to activate struts -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<!-- Filtro para todas las peticiones HTTP -->
<!--
<filter-mapping>
<filter-name>ControlAcceso</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<!-- Filtro para todas las peticiones HTTP (peticiones post)-->
<!--
<filter-mapping>
<filter-name>ControlAccesoPost</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

<!-- Filter mappings -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Filtro para el problema con el lazy -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>HBSessionFactory</param-value>
</init-param>
</filter>

<!-- Listener for Struts 2 and Spring dependency injection -->
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
</listener>


<!-- Listener configuration for Tiles 2, by default takes /WEB-INF/tiles.xml as config file -->
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener </listener-class>
</listener>

<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>es.aena.sgma.init.servlet.InitServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>

<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>

</web-app>

Should I change another thing? I am using struts2, spring and tiles. Thanks very much, and regards
Reply With Quote
  #10  
Old Nov 26th, 2009, 10:32 AM
zapp zapp is offline
Junior Member
 
Join Date: Nov 2009
Posts: 1
Default filter mapping

Quote:
Originally Posted by Costin Leau View Post
You can try this one:
...
The solution from Costin (see above) works fine, just add a filter-mapping:
Code:
<filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:36 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.