zoukankan      html  css  js  c++  java
  • Spring整合Hibernate 二

    Spring大战Hibernate之声明式的事务管理

    Spring配置文件:

    添加事务管理类的bean:

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    该类由Spring提供,如果是hibernate4那就用"org.springframework.orm.hibernate4.HibernateTransactionManager"(Spring2不支持hibernate4)。

    把sessionFactory注入给该bean的sessionFactory属性

    配置事务的通知:

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="getUser" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    transaction-manager="txManager":这个事务通知所用的事务管理bean是刚才所定义的管理bean。

    为名为 getUse 的方法添加事务。该事务是只读的(read-only="true")。

    名字以 add、save 开头的方法添加事务。该事务传播特性为REQUIRED(propagation="REQUIRED")。

    事务通知的属性解释:

    一 propagation:事务的传播特性(支持程度),默认为REQUIRED
         1.REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
         2.SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
         3.MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
         4.REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
         5.NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
         6.NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
         7.NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

    二 isolation:事务的隔离级别
        1. ISOLATION_DEFAULT:使用数据库默认的事务隔离级别。
        2. ISOLATION_READ_UNCOMMITTED:未提交读。这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
        3. ISOLATION_READ_COMMITTED :提交读。保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
        4. ISOLATION_REPEATABLE_READ :重复读。这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。
        5. ISOLATION_SERIALIZABLE:存列化。这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻读。

    三 timeout:超时的时间(秒)  -1:不限制

    四 rollback-for:指定需要回滚的的异常类型,默认是针对unchecked exception回滚

    添加事务的切入点

    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(public * com.startspring.service..*.*(..))" />
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
    </aop:config>

    指定切面为事务通知 txAdvice。指定切入点为txPointcut。

    ---------------------------------------------------------------------------------------------------------------

    要使用<tx:advice> <aop:config> 等标签,需要先添加tx、aop命名空间,如下:

    <?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.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            http://www.springframework.org/schema/tx 
                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    -----------------------------------------------------------------------------------------------------------------

    这时候可以把相应方法里事务开起和提交的语句去掉,让Spring来管理事务。如:

    public void save(Student student) {
        Session session = sessionFactory.getCurrentSession();
        //session.beginTransaction();
        session.save(student);
        //session.getTransaction().commit();
    }

    把session.beginTransaction();和session.getTransaction().commit();去掉。如果运作不报错则事务配置成功。

    注意!!必须把hibernate的 hibernate.current_session_context_class 这项属性去掉!否则事务是不起效的!!也就是hibernate配置文件的<property name="current_session_context_class">thread</property> 或者Spring配置文件的 <prop key="hibernate.current_session_context_class">thread</prop>

  • 相关阅读:
    volley框架使用
    Insert Interval
    candy(贪心)
    Best Time to Buy and Sell Stock
    Best Time to Buy and Sell Stock III
    distinct subsequences
    edit distance(编辑距离,两个字符串之间相似性的问题)
    trapping rain water
    word break II(单词切分)
    sudoku solver(数独)
  • 原文地址:https://www.cnblogs.com/likailan/p/3463372.html
Copyright © 2011-2022 走看看