Spring添加事物管理

来源:互联网 发布:淘宝大学官网视频教程 编辑:程序博客网 时间:2024/05/21 09:40

事务管理

-通过配置文件添加

在spring的配置文件中添加如下代码、

<!--事务管理-->    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--事物通知-->    <tx:advice id="txAdvice" transaction-manager="txManager">        <tx:attributes>            <!--find开头的方法设置为只读-->            <tx:method name="find*" read-only="true"/>            <tx:method name="get*" read-only="true"/>            <tx:method name="list*" read-only="true"/>            <!--对于所有错误都进行回滚-->            <tx:method name="*" rollback-for="Throwable"/>        </tx:attributes>    </tx:advice>    <!--aop 配置事物控制的类-->    <aop:config>        <!--通过文件路径添加事物-->    <!--<aop:pointcut id="servoceOperation" expression="execution(* main.java.service.*.*(..))"/>-->        <!--通过文件名添加事物-->        <aop:pointcut id="servoceOperation" expression="bean(*Service)"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="servoceOperation"/>    </aop:config>
  • 配置文件添加事物的好处
    • 一次配置可全局使用。
    • 不用具体到每个方法,而是对Service所有方法添加事物
  • 缺点
    • 有些方法不需要添加事物,造成资源浪费

-注解添加事物管理

在需要添加事物的方法上添加@Transactional注解

  • 使用注解控制事物方法的有点
    • 1.开发团队达成一致约定,明确注意事物方法的编程方法
    • 2.保证事务管理方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求或者剥离到事务方法外部
    • 3.不是所有的方法都需要事物,如果只有一条操作,只读操作不需要事物管理控制

- org/objectweb/asm/Type

我在添加事物事报了如下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘personService’ defined in file [H:\代码\itcastTax\out\production\itcastTax\main\java\service\PersonService.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type

到最后发现是因为缺少com.springsource.org.objectweb.asm-3.2.0.jar导致的
添加后解决

再此特别感谢这两位大神的博客
http://blog.csdn.net/anjingka/article/details/40392775

http://blog.csdn.net/liu709127859/article/details/7977682

0 0