• 测试ssh框架中hibernate的事务


        <!-- 配置切面 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.xxx.lobs.manager.impl.*.*(..))"
                id="myPoint" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint" />
        </aop:config>
    
        <tx:advice id="txAdvice" transaction-manager="tm">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <!-- <tx:method name="updateBatch" propagation="REQUIRED"/> -->
                <tx:method name="delete*" propagation="REQUIRED" />
                <!-- <tx:method name="deleteBatch" propagation="REQUIRED"/> -->
                <!--其他的方法,将只有只读事务 测试事务 -->
                <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
            </tx:attributes>
        </tx:advice>

    step1:类放在com.xxx.lobs.manager.impl下面,方法名字是update2,预期结果正常执行,结果正常

    step2:将测试类放在这个包中com.xxx.lobs.manager.impl.test 预期结果,不能正常执行,结果报错不能获取事务(org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread)

    step3:将测试类放在这个包中com.xxx.lobs.manager.impl.test 预期结果。修改配置文件为<aop:pointcut expression="execution(* com.xxx.lobs.manager.impl.*.*.*(..))"    预期结果可以正常执行,结果不能正常执行(

    Could not obtain transaction-synchronized Session for current thread

    step4:将测试类放在这个包中com.xxx.lobs.manager.impl.test 预期结果,修改配置文件为<aop:pointcut expression="execution(* com.xxx.lobs.manager.impl..*.*(..))" 预计结果不可以正常执行,结果正常执行了,

    step5:将测试类放在这个包中com.xxx.lobs.manager.impl.test 预期结果,修改配置文件为<aop:pointcut expression="execution(* com.xxx.lobs.manager.impl..*.*(..))" ,在update时制造一个异常,预计结果不可以正常执行 结果不可以正常运行,说明这样方法可以指定模块。

    step6:将测试类不放在这个包中com.xxx.lobs.manager.impl.test,和这个包同级 预期结果,修改配置文件为<aop:pointcut expression="execution(* com.xxx.lobs.manager.impl..*.*(..))" ,测试事事务是否存在,预计结果未知,正常

    step7:将测试类不放在这个包中com.xxx.lobs.manager.impl.test,和这个包同级 预期结果,修改配置文件为<aop:pointcut expression="execution(* com.xxx.lobs.manager.impl..*.*(..))" ,制造异常判断事务是否存在。,预计报异常事务存在,结果事务存在

    结论

    配置切点时这样配置 <aop:pointcut expression="execution(* com.xxx.lobs.manager.impl..*.*(..))"

    很多教程都少了个. 这样配置的好处是可以在impl中加一个模块的包把不同的类放在对应的模块的包里面,如果没有包也可以正常工作

    step9:配置文件如下,测试myupdate2方法是否具有事务

    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <!--其他的方法,将只有只读事务 测试事务 -->
    <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />

    复制udpate2的方法,没有异常数据的情况下,预计不具备事务,结果正常

    step10. 在9的基础上制造异常,预计还可以更改一条数据,结果还可以更改一条数据,不具备事务。

    结论,方法名字匹配配置文件中的配置才可以具备事务 

  • 相关阅读:
    sqlite轻量型数据库笔记
    WPF学习之MVVM笔记
    Halcon 圆测量与直线测量
    深入浅出WPF_读书笔记
    研华轴卡PCI1245L配ADAM3956接线板与台达ASD-A2伺服驱动器和松下A5伺服驱动器
    dataGridView添加ComboBox 每行绑定不同的集合,显示默认值
    VS2015 注释英文
    java的安装环境配置详细步骤
    万能正则解析 json 数据 解析成键值对
    http 异步 接收 回传 数据文字和文件流
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/6971075.html
Copyright © 2020-2023  润新知