Announcement Announcement Module
No announcement yet.
Transaction will not start in AbstractDependencyInjectionSpringContextTests Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction will not start in AbstractDependencyInjectionSpringContextTests

    I have a very simple use case that isn't working the way I expect. I was using AbstractTransactionalDataSourceSpringContextTests, but I wanted to make sure that I had my Spring transactions set up properly, so per some advice I read from the forums, I switched to using AbstractDependencyInjectionSpringContextTests.

    I have a 'Peer' class (same thing as a DAO). I have transaction advice set up for that class (just to test out this problem, I turned on the default transaction settings for all methods in the class). I turned on Log4J logging for all Spring classes.

    When I call a method in the Peer class (called MyPeer), what I expect to see is the message from AbstractPlatformTransactionManager "Creating new transaction with name..." or anything relative to starting a transaction, but I do not. I do see messages indicating the Peer class is getting proxied. And, if I use AbstractTransactionalDataSourceSpringContextTests and let the Spring test class start transactions for me, I do see the right messages, so I know I have my logging set up properly.

    Also, I tried putting the following code just inside the method being called in the MyPeer class, but it throws an exception indicating that no transaction has been started:

    Are my expectations of a transaction starting simply wrong? Here is my Spring config:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <bean id="basePeer" class="" scope="prototype">
    	<property name="dataSource" ref="myDataSource"/>
    <bean id="componentPeer" class="" parent="basePeer" scope="prototype" />
    <bean id="myPeer" class="" parent="basePeer" scope="prototype" />
    <!-- The transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
    <tx:advice id="txAdvice" transaction-manager="myTransactionManager">
    		<tx:method name="*" propagation="REQUIRED" />
    	<aop:pointcut id="peerOperation" expression="execution(**.*Peer.*(..))"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="peerOperation"/>
    <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="myDataSource"/>
    Thanks for any input,

  • #2

    After stepping through some of the JdkDynamicAopProxy code, I realized that, though the MyPeer class was getting proxied, it wasn't matching the pointcut (in AspectJExpressionPointcut, shadowMatch.neverMatches() was coming back true). I guess I had assumed that since it got proxied, it was matching.

    I misunderstood the matching rules. I needed this:

    It's hard to see, but there's two dots after the "xyz" instead of one. I had assumed '*' would match anything inbetween 'xyz' and the '*Peer' class in the fully qualified class name, but it doesn't (MyPeer is in a subpackage).

    As an aside, I had a hard time finding the AspectJ source code. I didn't see it in the Spring distribution, and digging through the Eclipse repositories gave me lots of AspectJ stuff, but I think it was either old releases or just tool source code.