Results 1 to 7 of 7

Thread: Spring2.5 Hibernate Transaction Q

  1. #1
    Join Date
    Jan 2008
    Posts
    4

    Default Spring2.5 Hibernate Transaction Q(Changes New)

    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 at 08:28 AM. Reason: New Require

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.*.*(..))
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2008
    Posts
    4

    Default

    Thanks , i just changed that ,then it works!

  4. #4
    Join Date
    Jan 2008
    Posts
    4

    Default

    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?

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Jan 2008
    Posts
    4

    Default

    Quote 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 ?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •