Announcement Announcement Module
Collapse
No announcement yet.
hibernate long session (for a non-web case) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate long session (for a non-web case)

    I'm using a HibernateTransactionManager and would like to open a hibernate session that spans multiple transactions - in order to take advantage of hibernate's first level cache. My scenario is completely outside of a web request. Is there an equivalent to the OpenSessionInViewInterceptor/OpenSessionInViewFilter for non-web request usage?

    Should I just use this excerpt from OpenSessionInViewInterceptor?...
    Code:
    				Session session = SessionFactoryUtils.getSession(
    						getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
    				if (getFlushMode() == FLUSH_NEVER) {
    					session.setFlushMode(FlushMode.NEVER);
    				}
    				TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
    Thanks for the help. And thanks for all the work on spring - I'm having a good time learning about it and developing with it.

    -Gene

  • #2
    ...more details

    Here are some more details...

    Code:
    <beans>
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="configLocation"><value>classpath&#58;awards.hibernate.cfg.xml</value></property>
        </bean>
    
        <bean id="awardsDAO" class="com.xyz.awards.dao.hibernate.AwardsDAOHibernate">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
    		<property name="transactionManager"><ref local="transactionManager"/></property>
        </bean>
    
        <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        
    	<!--
    		- A parent bean definition which is a base definition for transaction proxies.
    		- It is marked as abstract, since it is never supposed to be instantiated itself.
    		- We set shared transaction attributes here, following our naming patterns.
    		- The attributes can still be overridden in child bean definitions.
    		-->
    	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    			abstract="true">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
    				<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
    				<prop key="load*">PROPAGATION_SUPPORTS,readOnly</prop>
    				<prop key="save*">PROPAGATION_SUPPORTS</prop>
    				<prop key="delete*">PROPAGATION_SUPPORTS</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="awardsManager" parent="baseTransactionProxy">
    		<property name="target">
    			<bean class="com.xyz.awards.AwardsManagerImpl">
    				<property name="awardsDAO"><ref local="awardsDAO"/></property>
    			</bean>
    		</property>
    	</bean>
    	
    	<bean id="fulfiller" class="com.xyz.awards.SimpleAwardFulfiller"/>
    	
    	<bean id="awardMessageProcessor" parent="baseTransactionProxy">
    		<property name="proxyTargetClass"><value>true</value></property> 
    		<property name="target">
    			<bean class="com.xyz.awards.AwardMessageProcessor">
    				<property name="awardsManager"><ref local="awardsManager"/></property>
    				<property name="fulfiller"><ref local="fulfiller"></ref></property>
    			</bean>
    		</property>
    	</bean>
    
    </beans>
    So what I'm looking for is a way to have a method in awardMessageProcessor open a session that is bound to the thread, so that the work it does with the awardsManager (which uses the awardsDAO - which utilizes declarative transactions) will all happen within a single hibernate session.

    -Gene

    Comment


    • #3
      any thoughts?

      Can anyone comment on how to go about using a long Hibernate session (one that spans transactions) outside of an http request?

      Comment

      Working...
      X