@Transactional with xml-defined bean not working. (OSGi)

268 views Asked by At

I can't get @Transactional annotation to work with xml-defined bean. I don't know if xml definition has anything to do with it. Maybe it's the issue with OSGi.

<bean id="myDao"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="txManager"/>
        <property name="target" ref="myDao_t"/>
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_MANDATORY,
                    timeout_60,
                    -Exception
                </prop>
            </props>
        </property>
    </bean>

When I specify a proxy with xml like above it works.

I have <tx:annotation-driven transaction-manager="txManager" /> specified in the same bundle-context.xml where the bean definition is.

MyDao is just a simple class implementing interface with one method.

There is no exception, it just doesn't create proxy for myDao.

What might be missing?

<bean id="myPlanner" class="com.something.planner.MyPlanner">
     <property name="myDao" ref="myDao" />
</bean>
1

There are 1 answers

6
Aaron Digulla On

Try this:

  1. The class of the bean myDao should be MyDao (the type of the DAO instead of TransactionProxyFactoryBean).
  2. Put the annotation @Transactional on each public method in MyDao.java.

Spring should then automatically create a proxy for you. The approach in your question looks like "I try to create and configure the proxy factory myself".

While there surely is away to do that, I don't know exactly how you would do that. Instead, I rely on <tx:annotation-driven> and the @Transactional annotation.

EDIT You're using Spring 2.5.6A.

I just checked and @Transactional was added with Spring 1.2. But I'm not sure when <tx:annotation-driven> was added. The related EnableTransactionManagement was added with 3.1.

But the XML element is in this schema: http://www.springframework.org/schema/tx/spring-tx-2.5.xsd so it should be available in 2.5.

Maybe you're missing the necessary AOP libraries (cglib) on the classpath?