Announcement Announcement Module
Collapse
No announcement yet.
Spring2.5 Hibernate Transaction Q Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring2.5 Hibernate Transaction Q

    Today ,i hava just got the newest spring 2.5 and then i want to test its new transaction declare. I use the spring 1 style to handle the hibernate transaction. Then i got the right answer(it rolls back. i works);
    but when i use tx:advice then i does not work. i do not know why
    here is my applicationContext, and i use the webApplicationContextListen to load it.
    PHP 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/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
        
        <bean id="proxoolDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="driverUrl" value="jdbc:mysql://localhost:3306/demo?user=dbroot&amp;password=111111&amp;useUnicode=true&amp;characterEncoding=UTF8"/>
            <property name="user" value="dbroot"/>
            <property name="password" value="111111"/>
            <property name="houseKeepingSleepTime" value="90000"/>
            <property name="prototypeCount" value="5"/>
            <property name="maximumConnectionCount" value="20"/>
            <property name="minimumConnectionCount" value="3"/>
            <property name="trace" value="true"/>
            <property name="verbose" value="true"/>
        </bean>
        
        <bean id="hibernateSessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="proxoolDataSource"/></property>
            <property name="hibernateProperties">
                <props>
                    <!-- 数据库方言 -->
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
                    <prop key="hibernate.proxool.existing_pool">true</prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                    <value>Hellovisitor.hbm.xml</value>
                </list>
            </property>
        </bean>
        
        <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="hibernateSessionFactory"/>
            </property>
        </bean>
        
        <aop:config proxy-target-class="true">
            <aop:advisor advice-ref="boAdvice" pointcut="execution(* *.bo.*.*(..))" />
        </aop:config>
        
        <tx:advice id="boAdvice" transaction-manager="hibernateTransactionManager">
            <tx:attributes>
                <!-- hibernate transaction for read and wirte -->
                <tx:method name="do*" propagation="REQUIRED" isolation="DEFAULT" timeout="-1" read-only="false" rollback-for="java.lang.Exception"/>
                <!-- hibernate transaction for read only -->
                <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" timeout="-1" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        
        <bean id="helleDAO" class="sdu.sc.yxy.wwshdemo.hello.dao.HelloDao">
            <property name="sessionFactory">
                <ref bean="hibernateSessionFactory"/>
            </property>
        </bean>
        <bean id="helloBO"  class="sdu.sc.yxy.wwshdemo.hello.bo.HelloBO">
            <property name="helloDAO">
                <ref local="helleDAO"/>
            </property>
        </bean>

    </beans>
    Then ,i want to webAction to use the helloBo ,because of checking exception in webAction , so i want to handle the exception in bo,like

    PHP Code:
    interface IA
    String b(); 

    class 
    A implement IA
    String b(){ 
         try{ 
          
    doB(); 
        }catch(
    Exception e){ 
           return 
    e.getMessage(); 
         } 
       return 
    null

    void doB() throws Exception
        ........... 


    but in fact ,that does not works . if i just handle the doB() in webAction ,i works .i want to know why?
    Last edited by henry_yue; Jan 18th, 2008, 08:28 AM. Reason: New Require

  • #2
    Your pointcut is incorrect.

    Code:
    execution(* *.bo.*.*(..))
    This will match com.bi.SomeClass.someMethod... It will not match sdu.sc.yxy.wwshdemo.hello.bo.HelloBO.somemethod. You need to add an extra '.'.

    Code:
    execution(* *..bo.*.*(..))

    Comment


    • #3
      Thanks , i just changed that ,then it works!

      Comment


      • #4
        Then ,i want to webAction to use the helloBo ,because of checking exception in webAction , so i want to handle the exception in bo,like
        PHP Code:
        interface IA{
         
        String b();
        }
        class 
        A implement IA{
         
        String b(){
             try{
              
        doB();
            }catch(
        Exception e){
               return 
        e.getMessage();
             }
           return 
        null;
         }
        void doB() throws Exception{
            ...........
        }

        but in fact ,that does not works . if i just handle the doB() in webAction ,i works .i want to know why?

        Comment


        • #5
          Your interceptor/transaction is AROUND method b an exception will thus only occur when you are OUTSIDE the method, because that is the moment a commit takes place.

          Comment


          • #6
            Originally posted by mdeinum View Post
            Your interceptor/transaction is AROUND method b an exception will thus only occur when you are OUTSIDE the method, because that is the moment a commit takes place.
            But actually i did not declare the method to be transaction ,i only declare the doB method to be transaction,so i think when the outside call b ,the transaction interceptor will not work,until the b() call the doB,when the transaction interceptor works ,but in fact ,it does not work in that way.
            if i want to do transaction like above ,how can i do ?

            Comment


            • #7
              Read the reference guide, especially chapter 6.6.3.

              Spring uses proxy based AOP, so only method calls INTO the object can be intercepted. INTERNAL method calls aren't intercepted. So your transactional stuff isn't applied on the doB method. You can only advise the b method.

              If you want to intercept internal calls you will need to use load- or compile time weaving.

              Comment

              Working...
              X