-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can I use plain Tomcat with "@Transactional" feature ?
PostPosted: Fri Aug 09, 2013 2:56 am 
Newbie

Joined: Mon Jan 16, 2012 1:41 pm
Posts: 6
I have been spending many hours trying to make my web application work with @Transactional annotation without success. And now I come to the point where I'm not sure if this is possible in Tomact 7 + Spring 3 + Hibernate 4 environment (without JTA provider).

Any advices please?

Code:
....

@Autowired
org.hibernate.SessionFactory sessionFactory;

@Transactional
@RequestMapping(method = RequestMethod.GET)
protected String home() {
    Session session = sessionFactory.getCurrentSession(); // I expected the session is good to use now
    Province p = (Province) session.get(Province.class, 1L); // This causes no session found error :(


    return "home";
}


Here is the error I got:
Code:
    org.hibernate.HibernateException: No Session found for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
    ...


My spring XML (I use only ServletDispatcher, no ContextLoaderListener)
Code:
....
<tx:annotation-driven/>
<context:component-scan base-package="..."/>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/..."/>
    <property name="lookupOnStartup" value="true"/>
    <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
....


Top
 Profile  
 
 Post subject: Re: Can I use plain Tomcat with "@Transactional" feature ?
PostPosted: Sat Aug 10, 2013 1:27 pm 
Newbie

Joined: Mon Jan 16, 2012 1:41 pm
Posts: 6
It was my stupid mistake. It looks like Spring uses CGLIB to enhance @Transactional annotated method (not sure.. can anyone confirm this).

Simply changing:

Code:
protected String home() {


to

Code:
public String home() {


works like a charm. ;-)


Top
 Profile  
 
 Post subject: Re: Can I use plain Tomcat with "@Transactional" feature ?
PostPosted: Tue Mar 03, 2015 3:21 pm 
Newbie

Joined: Tue Mar 03, 2015 12:12 pm
Posts: 2
sure...it will but how the query will be that one of tome cat {cancel}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.