Search code examples
springhibernatewebspherejtaspring-transactions

No Session found for current thread using JTA TransactionManager


I'm having some trouble migrating to hibernate 4 while using a JTA Transaction Manager.
Maybe you can help, cause I'm all out of ideas.

Software:

Spring: 3.1.1.RELEASE
Hibernate: 4.1.3.Final
Database: Oracle 11g
Application: Ejb 3.0 deployed in Websphere Application Server 7.0 (using @Interceptors(SpringBeanAutowiringInterceptor.class) to initialize the spring context)

Spring configuration:

<context:annotation-config />
<context:component-scan base-package="myServicePackage,myDaoPackage" />

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/MyDataSource" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="myModelPackage" />
    <property name="mappingResources" value="queries.xml" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
</bean>

<tx:jta-transaction-manager />
<!-- <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" /> -->
<tx:annotation-driven />

Service:

@Service
public class TradeService {
    @Autowired
    private TradeDao tradeDao;
}

Dao:

@Repository
@Transactional
public class TradeDao {
    @Autowired
    private SessionFactory sessionFactory;

    public Trade getTrade(){
        return (Trade) sessionFactory.getCurrentSession().getNamedQuery("get_trade").uniqueResult();
    }

Throws:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at myDaoPackage.TradeDao.getTrade(TradeDao.java:26)

Notes:

  1. I've also tried using @Transactional on the service instead of the dao. Same result.
  2. Similar config, but using Spring 3.0.5.RELEASE, Hibernate 3.6.5.Final, and TradeDao extending HibernateDaoSupport works.

So, does anyone have any ideas?
I've been trying a lot of things all day today.
My head is spinning and I'm annoyed, so I must be missing something very simple, but at this point I can't really tell :)


Solution

  • I finally got this working.

    As I was expecting I was missing just some small configuration :)

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</prop>
        </props>
    </property>
    

    I found these configuration options earlier while browsing, but for some reason they didn't work when I tried them. Probably I had something else wrong at that time... (see Transaction strategy configuration chapter)

    Thank you all for trying to help!