spring事务处理demo

1.事务是什么及为什么使用事务

   

    事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分。如果事务遇到错误且必须取消或回滚,则所有数据修改均被清除。

例如:

    我要修改一个旅游产品,首先是根据productId查出对应的团队游产品,然后根据productId查询出对应的行程信息表并进行修改,保存;然后再根据productId查出价格表,再进行修改,保存;当以上的两个动作我都完成之后(并且成功完成),这个产品修改就算完成了!假如其中之一修改失败,即产品修改失败!这个时候我就可以用到事务去控制了,要么成功(commit),要么失败(roolback)。

2.demo

    spring-hibernate.xml:

   

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" abstract="false"     //sessionFactory注入hibernatePropertities,mappingResources,以及lobHandler
          singleton="true" lazy-init="default" autowire="default" dependency-check="default">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
        <!--  -->
        <property name="hibernateProperties">
            <props>
	        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.cache.use_minimal_puts">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>   
                <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/joey/framework/persistence/MenuItem.hbm.xml</value>
                <value>com/joey/security/persistence/mapping/LogHistory.hbm.xml</value>
                <value>com/ctol/mango/pge/productmanager/persistence/mapping/ScheduledFlight.hbm.xml</value>
            </list>
        </property>
        <property name="lobHandler">
        	<ref bean="oracleLobHandler" />
       	</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"          // transactionManager注入了sessionFactory,HibernateTransactionManager 在后面博客会讲到       
         abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
</bean>
 
<bean id="baseTransactionProxy" abstract="true"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" singleton="true"     // TransactionProxyFactoryBean为事务代理,注入了tracsactionManager和transactionAttributes
          lazy-init="default" autowire="default" dependency-check="default">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <!-- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
</bean>

    spring-factory.xml:

<bean id="providerFacade" parent="baseTransactionProxy" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
        <property name="target">
            <bean class="com.ctol.mango.pge.productmanager.business.facade.impl.ProviderFacadeImpl" parent="facade" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
                <property name="providerDAO">
                    <ref bean="providerDAO"/>
                </property>
            </bean>
        </property>
</bean>

   则ProviderFacadeImpl这个实现类已使用了事务控制!

  

   一般spring事务管理放在service层,因为dao层一般都是比较细粒度话的操作,而service可以是比较复杂的增删改查!并且用spring管理事务,只需要在配置文件配置即可,无需在service层代码中每次都显式调用,节省了代码量!

诸如:
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
……
tx.commit();


    或者:

    Connection dbconn = getConnection();  
    dbconn.setAutoCommit(false);  
    ……  
    dbconn.commit();  

 

猜你喜欢

转载自lopez.iteye.com/blog/2209961