sadasafsdfdsf 2014-06-25 23:19
浏览 302
已采纳

SPring事务怎么写配置过得怎么写到方法里面

一个方法同时实现一个修改和新增数据。某一个失败就事务回滚。一下是方法和事务配置:
public void updateSteelTagCheckingConfirm(String steelTagId,String recReviseTime,String status,String recRevisor) throws Exception
{
List list = steelPipeLabelDao.querySteelTagChecking();
SteelTagCheckingModel steelTagCheckingModel = (SteelTagCheckingModel) list.get(0);
steelTagCheckingModel.setId(null);
steelTagCheckingModel.setRecCreateTime(new Date());
steelTagCheckingModel.setRecReviseTime(new Date());
steelTagCheckingModel.setStatus(status);
steelTagCheckingModel.setRecRevisor(recRevisor);
steelPipeLabelDao.querySteelTagCheckingConfirm(steelTagId);//修改
steelPipeLabelDao.insertSteelTagCheckingConfirm(steelTagCheckingModel);//新增

}

以下是SPring事务配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
default-autowire="no">

<!-- ========================= RESOURCE DEFINITIONS ========================= -->















<!-- Configurer that replaces ${...} placeholders with values from a properties file -->  

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:bsteel.properties</value>
            <value>classpath:javaMail.properties</value>
        </list>
    </property>
</bean>

 <!-- Pool DataSource
<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="loginTimeout">
        <value>1800</value>
    </property>
    <property name="maxStatements">
        <value>50</value>
    </property>
    <property name="maxStatementsPerConnection">
        <value>20</value>
    </property>
    <property name="minPoolSize">
        <value>5</value>
    </property>
    <property name="maxPoolSize">
        <value>30</value>
    </property>
    <property name="acquireIncrement">
        <value>2</value>
    </property>
    <property name="driverClass">
        <value>${jdbc.driverClassName}</value>
    </property>
    <property name="jdbcUrl">
        <value>${jdbc.url}</value>
    </property>
    <property name="user">
        <value>${jdbc.username}</value>
    </property>
    <property name="password">
        <value>${jdbc.password}</value>
    </property>
</bean>

-->


<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingDirectoryLocations">
        <list>
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/qs/model
            </value>
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/rf/model
            </value>
            <value>   
                hibernate3/com/baosight/baosteel/bsi/cs/ct/model
            </value>
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/sv/model
            </value>
            <!-- add by TanGuiyuan -->
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/sv2/model
            </value>
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/oq/model
            </value>                
            <value>
                hibernate3/com/baosight/baosteel/bsi/common/model
            </value>
            <value>
                hibernate3/com/baosight/baosteel/bsi/helper/model
            </value>
            <value>
                hibernate3/com/baosight/baosteel/bsi/cs/fq/model
            </value>
            <!-- added by frankie 2008-5-4 -->
            <value>   
                hibernate3/com/baosight/baosteel/bsi/cs/ce/bxgHistory/model
            </value>
            <value>   
                hibernate3/com/baosight/baosteel/bsi/cs/ce/baoxin/model
            </value>
            <value>   
                hibernate3/com/baosight/baosteel/bsi/cs/ce/luobao/model
            </value>
            <value>   
                hibernate3/com/baosight/baosteel/bsi/cs/ce/model
            </value>

        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.dialect}
            </prop>
            <prop key="hibernate.cache.provider_class">
                org.hibernate.cache.EhCacheProvider
            </prop>
            <prop key="hibernate.cache.use_minimal_puts">true</prop>
            <prop key="hibernate.max_fetch_depth">2</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop
                key="hibernate.bytecode.use_reflection_optimizer">
                true
            </prop>
            <prop key="hibernate.jdbc.batch_size">0</prop>
        </props>
    </property>
</bean>


<!-- Transaction manager for a single Hibernate SessionFactory  -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="baseTransactionProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract="true">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <!--<prop key="*">PROPAGATION_REQUIRED</prop> --> 
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="get*">PROPAGATION_REQUIRED</prop>
            <prop key="query*">PROPAGATION_REQUIRED</prop>
            <prop key="list*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

  • 写回答

2条回答 默认 最新

  • iteye_15645 2014-06-26 08:48
    关注

    楼主如果一点概念都没有,建议先了解下底层的事物管理机制。
    [code="java"]
    try{
    begin trans
    update a
    add b
    trans.commit()
    }catche(e){
    trans.rollback()
    }
    [/code]
    我们现在使用spring的事物管理,实际是使用AOP的横切概念, 也就是把那些事务管理部分剥离掉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)