Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Session

    Hi

    I am new to spring and hibernate. I have a question about how hibernate session is getting created. This is what I have in my web.xml
    Code:
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    Few other configuration files...


    Application-datasource.xml
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="lobHandler">          
    			<ref bean="oracleLobHandler"/>      
    		</property>
    		<property name="mappingDirectoryLocations" value="classpath*:com/biogenidec/ttp/data/"/>		
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
    				<prop key="hibernate.show_sql">false</prop>
    				<prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>
    				<prop key="hibernate.generate_statistics">true</prop>
    				<prop key="hibernate.default_batch_fetch_size">0</prop>
    				<prop key="hibernate.cache.use_structured_entries">true</prop>
    				<prop key="hibernate.cache.use_query_cache">true</prop>
    				<prop key="hibernate.use_sql_comments">true</prop>
    				<prop key="hibernate.jdbc.batch_size">0</prop>		
    				<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>	 				
    			</props>
    		</property>
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	
    	<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
    	  	<property name="nativeJdbcExtractor">
    	    	<ref bean="nativeJdbcExtractor"/>
    		</property>	
    	</bean>
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" destroy-method="close">
    		<property name="jndiName"><value>com.bam.touch</value></property>
     	</bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    <bean id="txServiceProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save">PROPAGATION_REQUIRED</prop>
    				<prop key="load">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>				
    			</props>
    		</property>
    	</bean>
    
    <bean id="txProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="attach">PROPAGATION_REQUIRED</prop>			
    				<prop key="save*">PROPAGATION_REQUIRED</prop>				
    				<prop key="delete*">PROPAGATION_REQUIRED</prop>
    				<prop key="create*">PROPAGATION_REQUIRED</prop>
    				<prop key="replicate*">PROPAGATION_REQUIRED</prop>
    				<prop key="getGuaranteed*">PROPAGATION_REQUIRED</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    Application-dao.xml
    Code:
    <bean id="queryDAO" parent="txProxyTemplate" >
    		<property name="target" ref="queryDAOTarget" />
    	</bean>
    	<bean id="queryDAOTarget" class="com.biogenidec.ttp.dao.hibernate.QueryDAOHibernate" >
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    Application-service.xml
    Code:
    <bean id="queryService" parent="txServiceProxyTemplate" >
    		<property name="target" ref="queryServiceTarget" />
    	</bean>
    	<bean id="queryServiceTarget" class="com.biogenidec.ttp.service.QueryService" >
    		<property name="queryDAO" ref="queryDAO" />
    	</bean>
    My question is how hibernate session is created and how is it passed on to the service, dao layers? When is the session destroyed?

    Thanks

  • #2
    As you have OpenSessionInViewFilter configured, the Session lifecycle is tied to this. It's bound to thread local for use within the request. As for closing this is dependent on the singleSession flag. I would take a look at the java doc or the source code.
    http://www.springframework.org/docs/...iewFilter.html

    Comment


    • #3
      As you have OpenSessionInViewFilter configured, the Session lifecycle is tied to this. It's bound to thread local for use within the request.
      hibernate session gets created from session factory. So my question is does OpenSessionInViewFilter looks for the session factory and creates a session and attaches it to the request? After the passing the controller what happens to the session? Is it attached to the service bean in the application-service.xml?

      Comment


      • #4
        Sorry what I meant was it's bound to a threadLocal variable for the duration of that request. Therefore anything within that call can get access to the same session. If you have a look at HibernateTemplate and HibernateTransactionManager, you should be able to see the code your after.

        Comment

        Working...
        X