Announcement Announcement Module
Collapse
No announcement yet.
How to use Hibernate transactions with tx:advice ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use Hibernate transactions with tx:advice ?

    Hello,

    I'm trying to use the new way to handle hibernate's transactions with tx:advice.
    I have a simple DAO (bookDAO) and a test class with a method test() (bookDAOTest) to test the DAO.

    If I try to call
    Code:
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    inside bookDAO or bookDAOTest, I have this exception :

    Code:
    org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionInfo(TransactionAspectSupport.java:105)
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus(TransactionAspectSupport.java:81)
    	at com.test.BookDAOTest.test(BookDAOTest.java:43)
    	at com.test.BeansTest.main(BeansTest.java:113)
    Exception in thread "main"
    Am I missing something ? Is there something else to do to activate transaction management ?

    Find below my applicationContext.xml :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <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/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">
           
    	<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="mappingResources">
        <list>
            <value>Book.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.use_outer_join">true</prop>
        </props>
      </property>
      <property name="dataSource">
        <ref bean="dataSource"/>
      </property>
    </bean>
    
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName">
        <value>oracle.jdbc.driver.OracleDriver</value>
      </property>
      <property name="url">
        <value>db-url</value>
      </property>
      <property name="username"><value>aaa</value></property>
      <property name="password"><value>aaa</value></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>
        
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
          <tx:method name="get*" read-only="true" />
          <tx:method name="*"/>
        </tx:attributes>
      </tx:advice>
      
      <aop:config>
        <aop:pointcut id="Operation" expression="execution(* *Book*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="Operation"/>
      </aop:config>
    
          <bean id="bookDAOTest" class="com.test.BookDAOTest">
            <property name="bookDAO" ref="bookDAO"/>
          </bean>
    
          <bean id="bookDAO" class="com.test.BookDAOImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
          </bean>
      
    </beans>
    Thanks,

    Laurent

  • #2
    Not sure but I think you have to change your pointcut expression.

    Code:
      <aop:config>
        <aop:pointcut id="Operation" expression="execution(* *..*Book*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="Operation"/>
      </aop:config>

    Comment


    • #3
      Not working

      I already tried a lot of expressions, but none are working, so I suppose expression is not the problem

      I really don't know where the problem is, and I'm quite surprised to find so few documentation about tx:advice on the web.

      Comment


      • #4
        I think that your pointcut expression is incorrect, so the bookDAO is not getting proxied.

        Try to cast bookDAO to BookDAOImpl
        Code:
        BookDAOImpl bookDAOImpl = (BookDAOImpl) bookDAO;
        You will get a class cast exception if the bookDAO has been proxied.

        Comment


        • #5
          Originally posted by Nelis View Post
          I already tried a lot of expressions, but none are working, so I suppose expression is not the problem
          Well your current pointcut tells to intercept all *Book* instances which will not include yours (if I'm correct). The pointcut *..*Book* will intercept all *Book* instances in all packages.

          For testing purpose you could also try to change your expression to com.test.BookDAO* just to test if it is working.

          Comment

          Working...
          X