0

How to use OpenSessionInViewListener with ZK+Spring?

asked 2009-09-10 13:45:42 +0800

tomCat gravatar image tomCat
276 3

updated 2009-09-10 13:45:59 +0800

Hi,

i have the follwing configuration:

zk.xml:

<zk>
	<listener>
    	    <description>Spring TransactionSynchronizationManager handler</description>
    	    <listener-class>org.zkoss.zkplus.spring.SpringTransactionSynchronizationListener</listener-class>
	</listener>		
</zk>

web.xml:

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>TEST_Lazy</display-name>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/conf/spring/spring-config.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
      
	<filter>
		<filter-name>hibernateFilter</filter-name>
			<filter-class>
				org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
			</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>false</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
           ...
	<welcome-file-list>
             ...
	</welcome-file-list>			
</web-app>

spring-config.xml:

<beans ...>
	<!-- Hibernate SessionFactory mit Annotationen -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource"><ref bean="dataSource"/></property>
		<property name="hibernateProperties">
			<props>
                                   ...
			</props>
		</property>
		<property name="annotatedClasses">
			<list>				
				<value>model.Address</value>
				<value>model.Keyword</value>			
			</list>
		</property>	
	</bean>
	
	<bean id="addressController" class="controller.AddressController">
		<constructor-arg ref="addressService" />
	</bean>
	
	<bean id="addressDao" class="dao.impl.GenericDaoImpl">
		<constructor-arg index="0" value="model.Address" />
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<bean id="addressService" class="service.impl.GenericServiceImpl">
		<constructor-arg ref="addressDao" />
	</bean>
</beans>

GenericDaoImpl.java

public class GenericDaoImpl<T, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<T, PK> {

	private Class<T> persistentClass;
	
	public GenericDaoImpl(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}
	
	@Override
	public T get(PK id) throws DataAccessException {
		//return (T) sessionFactory.getCurrentSession().get(this.persistentClass, id);
		return (T) getHibernateTemplate().get(this.persistentClass, id);
	}
}

Is the configuration correct? The Problem is, that the session is opened and closed when i execute "get()" method of my service object.
I expected, that the session should be kept open by the OpenSessionInViewFilter....
What am I doing wrong?

Best regards,
tomCat

delete flag offensive retag edit
Be the first one to reply this discussion!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-09-10 13:45:42 +0800

Seen: 252 times

Last updated: Sep 10 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More