| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 893 人关注过本帖
标题:如何通过spring在service层配置事务?
取消只看楼主 加入收藏
zhufeifei
Rank: 1
等 级:新手上路
威 望:2
帖 子:402
专家分:0
注 册:2006-8-11
收藏
 问题点数:0 回复次数:0 
如何通过spring在service层配置事务?
本人对hibernate和spring不是太熟,不知道如何用spring声明配置事务。因为我的一个Service实现类需要对多个表进行写入操作,一旦某个表操作失败可以回滚。具体部分代码贴上来,希望放谁可以帮我解决这个问题,没有分可以给大家了,谢谢!
1.service的实现类方法(想在这个加上事务)
        /**
     * 新增阀门并保存
     */
    @Override
    public void save(CompanyEntity company, SamplerEntity sampler, ValveEntity entity){        
                //希望在这三步中有一步出错,可以回滚
        if(company != null && sampler != null && entity != null ){
          valveDao.save(entity);        //操作第一张表
          samplerDao.save(sampler);     //操作第二张表
          companyDao.save(company);     //操作第三张表
        }else{
            throw new ProgramException("对象为空");
        }
    }
2.spring的配置文件applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www. xmlns:xsi="http://www.
        xmlns:p="http://www. xmlns:context="http://www.
        xmlns:jee="http://www. xmlns:aop="http://www.
        xmlns:tx="http://www.
        xsi:schemaLocation="
            http://www. http://www.
            http://www. http://www.
            http://www. http://www.
            http://www. http://www.
            http://www. http://www.
   
    <context:annotation-config /><!-- 通过@Resource标记来注入 bean -->
    <context:component-scan base-package="com.rlht" />
   
   
     <context:property-placeholder location="classpath:system.properties"/>
        <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
            p:driver="${hibernate.connection.driver_class}"
            p:driverUrl="${hibernate.connection.url}"
            p:user="${hibernate.connection.username}"
            p:password="${hibernate.connection.password}"
            p:alias="${proxool.pool_alias}"
            p:houseKeepingTestSql="${proxool.house-keeping-test-sql}"
             p:testBeforeUse="${proxool.test-before-use}"
            p:maximumConnectionCount="${proxool.maximum-connection-count}"
            p:minimumConnectionCount="${proxool.minimum-connection-count}"
            p:prototypeCount="${proxool.prototype-count}"
            p:trace="true"/>

    <bean name="lobHandler"
      class="org.springframework.jdbc.support.lob.DefaultLobHandler"
      lazy-init="default">
     </bean>            

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
        p:dataSource-ref="dataSource">

        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:com/rlht</value>
            </list>
        </property>   
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>   
                <prop key="hibernate.hbm2dll.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                </entry>
            </map>
        </property>
    </bean>
    <!--bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /-->
   
    <bean id="hibernateTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>

    <!--bean id="jtaTxManager" class="org.springframework.transaction.jta.JtaTransactionManager" p:userTransaction="jotm"/-->  
      
    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* com.rlht*.service.*.*(..))"/>
        <aop:advisor pointcut-ref="serviceOperation" advice-ref="tx-advice-hibernate"/>
    </aop:config>
   
   
    <tx:advice id="tx-advice-hibernate" transaction-manager="hibernateTxManager">
      <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
      </tx:attributes>
    </tx:advice>
    <context:mbean-export/>


</beans>
搜索更多相关主题的帖子: hibernate company spring public null 
2011-08-04 16:21
快速回复:如何通过spring在service层配置事务?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056523 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved