views:

39

answers:

1

ok, enough. I couldn't make this work. I am a newbie to Spring transactions and using the @Transactional annotation in my service to manage transactions. Below is my spring bean configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"&gt;

  <tx:annotation-driven transaction-manager="txManager"/>

  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <jee:jndi-lookup id="dataSource" jndi-name="jdbc/myapp"/> 

  <!-- other <bean/> definitions here -->

</beans>

and I annotate my service:

@Transactional
public class MyServiceImpl implements MyService {
...
}

I notice two things

  1. The connection that I get in my DAO [using DataSourceUtils.getConnection(dsName)] has the autocommit enabled [true].
  2. As far as I debugged there doesn't look to be any transaction that has begun during my service method invocation.

Anyone had this problem?

A: 

I use Hibernate with the HibernateTransactionManager myself, so I'm not too familiar with how the JDBC transaction manager works. Have you inspected the callstack to see whether or not TransactionInterceptor is in there ? If it is, then the transactional annotation is working, and you may just be missing something else. If you haven't looked for it, what makes you think that it's not working ? To eliminate the obvious, have you explicitly set a setting in your JDBC configuration to disable autocommit ?

Alex Marshall
I don't see a TransactionInterceptor in my callstack - To whatever extent I understand looking at the code, the DataSourceTransactionManager's doBegin method didn't get invoked as the method creates connection and explicitly makes the auto commit to false. When I followed my debugger, I noticed that the connection is created only during the call DataSourceUtil.getConnection(dsName) [using dataSource.getConnection() with autocommit enabled] and there wasn't one already available to it.
santhakr
You haven't defined the tx namespace in your <beans> root element. How is the application even starting ? You'll need to have this in there (like the jee namespace) before you can start using the tx-namespace features.
Alex Marshall
Sorry, thats my bad while copying the code to here. I do have the tx namespace. Sorry again.
santhakr