1

The issue I am having is that I use Spring to manage and load hibernate for my web application. I am currently using OpenSessionInViewFilter. This works as intended when I am viewing the application, but not so well when I am trying to access hibernate from non-view related activities such as a Quartz task or some Runnable thread I create to help with some tasks. This causes the Lazy initialize exception and no session available exceptions to occur.

Here is how I currently use Spring to manage Hibernate

<bean id="mainDataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    [..DB config..]
</bean>

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource">
        <ref bean="mainDataSource"/>
    </property>
</bean>

<bean id="txManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    <property name="dataSource"><ref local="mainDataSource"/></property>
</bean>

I then configure DAO objects which extend HibernateDaoSupport and inject them into service classes

<bean id="myDAO"
    class="package.myDAO">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

<bean id="mySvcTarget" class="package.myService">
    <property name="myDAO"><ref bean="myDAO"/></property>
</bean> 

<bean id="myService" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref bean="txManager"/>
    </property>

    <property name="target">
        <ref bean="mySvcTarget"/>
    </property>

    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

So then in my application, myService is injected into my controller classes so I use that to get access to my DAO's. For my situation though it appears I need to access my DAO's (or service preferably) some other way and manually open and close my hibernate sessions since my service classes only seem to be open during view session. I am not exactly sure the best way to do this. All the hibernate configurations are there already in Spring so I'm assuming its just a matter or calling them somehow.

Raymond Holguin
  • 1,050
  • 2
  • 12
  • 35

1 Answers1

0

First of all those additional services that you're using (non-views) should be visible by Spring. The simplest way to do it is to use @Service annotation. And to make it work you can add <context:component-scan base-package="your.package"> in your configuration.

After this, if Spring sees your service as a bean, it should be enough to use @Transactional annotation to have Hibernate session in it.

sap1ens
  • 2,877
  • 1
  • 27
  • 30
  • applying `@Transactional` will work at all. No need to call `Session session=sessionFactory.openSession();` in service's CRUD methods? As @Raymond wants to "manually open and close hibernate sessions" – Amogh Aug 02 '14 at 04:37
  • ..And what about `filter` written for `org.springframework.orm.hibernate.support.OpenSessionInViewFilter` in web.xml, it is remain same no need to remove this filter. – Amogh Aug 02 '14 at 04:53
  • @Amogh to Clarify, the only reason I said i stated I wanted to manually open and close session was because I thought that was the only way to accomplish what i need. If i don't have to do that I would prefer not too. – Raymond Holguin Aug 04 '14 at 17:48
  • @sap1ens unfortunately this did not work. I did as you suggested and still get the lazy initializatino error. Just for safe measure I put the transaction annotation on every method I call as well as label the classes I use as Transactional but still no luck – Raymond Holguin Aug 04 '14 at 19:07