当前位置: 首页 > 图文教程 > Java技术 > Web框架 > Spring结合Hibernate声明式事务配置

Web框架
浅谈Ajax与Spring集成
浅谈Mock和基于Spring容器管理事务的测试
Hibernate中一级缓存与二级缓存
浅谈Hibernate的配置信息(一)
浅谈Hibernate的配置信息(二)
浅谈Spring中使用Quartz来执行计划任务
浅谈JFreeChart实时曲线(代码)
Ajax框架中选DWR还是A4J
浅谈Hibernate中关联问题
浅谈项目开发过程中使用Struts-menu
浅谈使用模板模式简化操作Hibernate
Hibernate中加载并存储对象
浅谈Hibernate依赖对象(Dependent objects)
浅谈Hibernate中自定义值类型
ORM那些事
51培训对Hibernate中的事务详解
51培训详谈Hibernate检索
详谈Struts2的核心概念(上)
详谈Struts2的核心概念(下)
浅谈应用dom4j操作xml文件

Web框架 中的 Spring结合Hibernate声明式事务配置


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 278 ::
收藏到网摘: n/a

多时候都会用到他的声明式事务,简单的在配置文件中进行一些规则配置,利用Spring的AOP功能就能轻松搞定事务问题;这里面涉及到一个事务的传播属性问题【Propagation】,他在TransactionDefinition接口中定义,共有7种选项可用:

    PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
    PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
    PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
    PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
    PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
    PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
    PROPAGATION_NESTED:支持当前事务,新增Savepoint点,与当前事务同步提交或回滚。

 

以下为一例:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="oracle.jdbc.driver.OracleDriver">
        </property>
        <property name="url"
            value="jdbc:oracle:thin:@192.168.1.1:1521:ASDF">
        </property>
        <property name="username" value="ASDFF"></property>
        <property name="password" value="ASDFF"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>.../vo/Attachment.hbm.xml</value>
                <value>.../vo/Author.hbm.xml</value>
                <value>.../vo/Docs.hbm.xml</value>
                <value>.../vo/FileForder.hbm.xml</value>
                <value>.../vo/Files.hbm.xml</value>
                <value>.../vo/Users.hbm.xml</value>
            </list>
        </property>
    </bean>

   <!-- DAO接口的实现类 -->
    <bean id="fileDaoTarget" class=".......dao.FileDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

   <!-- Serv接口的实现类 -->
    <bean id="fileService" class="......serv.FileService">
        <property name="fileDao">
            <ref bean="fileDao"/>
        </property>
    </bean>
   
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
   
    <bean id="fileDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="proxyInterfaces">
            <list>

                <!-- 代理接口 -->
                <value>.......dao.IFileDAO</value>
            </list>
        </property>
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="target">
            <ref bean="fileDaoTarget"/>
        </property>
        <property name="transactionAttributeSource">
            <ref bean="transactionAttributeSource"/>
        </property>
    </bean>

   <!-- 事务属性配置 -->
    <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> 
        <property name="properties"> 
            <props> 
                <prop key="edit*">PROPAGATION_REQUIRED</prop> 
                <prop key="insert*">PROPAGATION_REQUIRED</prop> 
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> 
                <prop key="delete*">PROPAGATION_REQUIRED</prop> 
            </props> 
        </property> 
    </bean>
</beans>