views:

613

answers:

2

I posted this to the spring forums, sorry for the xpost.

I am new to spring. I am working on an existing project that uses spring 1.2.8 (old, I know), and java 1.5 so annotations should work.

I am trying to use the @Transactional annotation on a concrete class, following the docs at: http://static.springsource.org/spring/docs/1.2.8/reference/transaction.html#d0e6062

So I have something like so:

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

<bean id="MyDAO"
  class="com.company.package.dao.spring.MyDAOImpl">
  <property name="dataSource" ref="DataSource" />
</bean>

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
  <property name="transactionInterceptor" ref="txInterceptor"/>
</bean>

<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager"/>
  <property name="transactionAttributeSource">
    <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
  </property>
</bean>

and I annotate my class:

@Transactional(propagation = Propagation.REQUIRED)
public class MyDAOImpl extends JdbcDaoSupport implements MyDAO{
...
}

When I run it I can see in my debug logs that spring is finding all of the classes: Code:

01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator]
01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor]
01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#329f3d]

but after that there is no mention of annotations or transactions. I don't even know if there are supposed to be. I am verifying in my mysql log that the queries are not being performed transactionally.

Any ideas?

+1  A: 

One thing that I frequently overlook is that the proxy can only intercept calls if they are made from outside the class itself: If you have one method calling a transactional method in the same class, it will not be wrapped by the proxy. But that's when individual methods are annotated, not the whole class, so it's probably not what's causing your problem.

John
+1  A: 

Ignore the DEBUG lines (they just say you havent specified id or name you just have a bean class="")

Have you put the line,

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

You also need to add the schemaLocation at the top something like

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;
</beans>

Otherwise the annotations dont get processed :)

Calm Storm
Hey, thanks for the reply. I am using spring 1.2.8, against my will, but that is the case. I believe tx:annotation-driven isn't introduced until spring 2.0. Plus, the documentation I linked to for 1.2.8 makes no mention of it...
DanInDC