Announcement Announcement Module
Collapse
No announcement yet.
Transaction will not start in AbstractDependencyInjectionSpringContextTests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    TransactionAspectSupport.currentTransactionStatus().isNewTransaction()
    Are my expectations of a transaction starting simply wrong? Here is my Spring config:

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
        
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    </bean>     
        
    
    <bean id="basePeer" class="com.xyz.BasePeer" scope="prototype">
    	<property name="dataSource" ref="myDataSource"/>
    </bean>
    	
    <bean id="componentPeer" class="com.xyz.ComponentPeer" parent="basePeer" scope="prototype" />
        
    <bean id="myPeer" class="com.xyz.MyPeer" 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:attributes>           
    		<tx:method name="*" propagation="REQUIRED" />
    	</tx:attributes>
    </tx:advice>
    	
    <aop:config>
    	<aop:pointcut id="peerOperation" expression="execution(* com.xyz.*.*Peer.*(..))"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="peerOperation"/>
    </aop:config>
    
    <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="myDataSource"/>
    </bean> 
            
    </beans>
    Thanks for any input,
    Alan

  • #2
    [solved]

    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:

    Code:
    expression="execution(* com.xyz..*.*Peer.*(..))"
    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.

    Thanks,
    Alan

    Comment

    Working...
    X