permaculture playing cards*
The moose likes Spring and the fly likes Problem with Transaction Management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem with Transaction Management" Watch "Problem with Transaction Management" New topic
Author

Problem with Transaction Management

Selva Duraisamy
Greenhorn

Joined: Oct 20, 2001
Posts: 6
Hi,

I am trying to create a simple application which registers a user using Spring 3, Hibernate and mySQL. I am trying to do as per the instrauctions given in http://static.springsource.org/sprin...ansaction.html

using the annotations. The service implementation class is marked with @Transactional

and the Transaction Manager is defined using XML configuration like:

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>


<bean id="txManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource">
<ref bean="myDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQL Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.jdbc.batch_size">1500</prop>
</props>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="annotatedClasses">
<list>
<value>com.x.y.z.User</value>
</list>
</property>
</bean>

the Datasource is pointing to MySQL db.

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3308/xyz"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

The sessionFactory is autowired to the DAO Impl.

@Autowired
private SessionFactory sessionFactory;

and when I try to get the current session like:

public void saveUser(User user) {
logger.info("Registering the user : "+sessionFactory.getCurrentSession());
sessionFactory.getCurrentSession().saveOrUpdate(us er);
}

it throws the exception:

[#|2011-07-26T13:09:28.579+0100|SEVERE|glassfish3.0.1|javax.e nterprise.system.std.com.sun.enterprise.v3.service s.impl|_ThreadID=30;_ThreadName=Thread-1;|org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:700)

so it seems that the transaction is not created correctly. This is the point where the session needs to be created, becasue its kind of entry point to the applciation.

The Service class is annonated with:

@Transactional(readOnly = true)
public class UserServiceImpl implements UserService{

and the method is annonated with:

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void addUser(User user) {
userDao.saveUser(user);
}

is there anything else I have missed out? The documentation has a Note:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.

but all my xml configurations are part of db.xml which is imported in the root-context.xml.

I have tried defining <tx:advice> as well. But the error persisted. any help regarding this would be greatly appreciated.

thanks in advance.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Can you post the complete link please? And please make sure to use code tags when posting code in the forum.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
jQuery in Action, 2nd edition
 
subject: Problem with Transaction Management
 
Similar Threads
DAO for web application
calling a method in hibernate DAO through JMX export MBean
Hibernate doesn't use Oracle's native isolation levels
Error with Transaction Interceptor
running before I can crawl ( or how to add Spring to an existing Hibernate standalone app)