spring transaction processing demo

1. What is transaction and why use transaction

   

    A transaction is a single unit of work. If a transaction succeeds, all data modifications made in that transaction are committed and become a permanent part of the database. If the transaction encounters an error and must be cancelled or rolled back, all data modifications are cleaned up.

 

E.g:

    I want to modify a travel product. First, find out the corresponding group tour product according to productId, then query the corresponding itinerary information table according to productId, modify it, and save it; then find out the price table according to productId, then modify and save it; When I have completed the above two actions (and completed successfully), this product modification is complete! If one of them fails to modify, the product modification fails! At this time, I can use the transaction to control, either success (commit) or failure (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 injects sessionFactory, HibernateTransactionManager will be covered in a later blog       
         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 is a transaction proxy, injected with tracsactionManager and 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>

   Then the ProviderFacadeImpl implementation class has used transaction control!

  

   Generally spring transaction management is placed in the service layer, because the dao layer is generally a relatively fine-grained operation, and the service can be a more complex addition, deletion, modification and query! And using spring to manage transactions, you only need to configure it in the configuration file, and there is no need to explicitly call it every time in the service layer code, saving the amount of code!

 

such as:
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
……
tx.commit();


    or:

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

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326346133&siteId=291194637