Spring事务机制

来源:互联网 发布:sql 清空数据库 编辑:程序博客网 时间:2024/05/19 16:33

    1、Spring事务机制
        声明式事务、事务管理器
    2、hibernate事务
        step1: 配置数据源
        step2:配置sessionfactory (同上)
        step3:配置事务管理器
            <bean id="myTransactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                    <ref bean="mySessionFactory" />
                </property>
            </bean>     
        step4:创建事务服务代理
            <bean id="saleService"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="proxyInterfaces">
                <value>lab7.SaleService</value>
            </property>
            <property name="transactionManager">
                <ref bean="myTransactionManager" />
            </property>
            <property name="target">
                <ref bean="saleServiceTarget" />
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
        注:
            事务属性描述格式:
            传播行为,隔离级别,只读事务(readonly),回滚规则
            在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常)
            ,会回滚事务。对于受查异常,会提交事务。
            如果即使发生了某种受查异常,也要回滚事务,可以用  “- 异常类型“来声明。
            同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
             
    3、简化事务配置
        继承、自动代理
         
    4、使用标注来进行事务管理(可选)
        (1)引入相应命名空间,参见spring参考文档
        (2)加入<tx:annotation-driven transaction-manager="myTransactionManager"/>
        (3)在service类中,使用@Transactional标记
        可参考工程spring中的t4

原创粉丝点击