Spring 事务

1.利用Bean继承的方式 :Spring 事务的配置如下:


    txProxyTemplate的abstract="true",说明此类是一个抽象类,也可以不设置abstract属性,设置lazy-init="true"

也是把他当作抽象类,因为应用使用的是他的子bean,没必要的到代理类的实例。

    transactionManager是为事务代理bean注入使用适用于Hibernte的事务管理器

<beans> <!-- on spring context starting up, frech the application context --> <bean id="beansUtil" class="com.krm.util.BeansUtil"></bean> <!-- Transaction template for Managers, from: http://blog.exis.com/colin/archives/2004/07/31/concise-transaction-definitions-spring-11/ --> <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="dataCollect">PROPAGATION_REQUIRED</prop> <prop key="getData*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="sort*">PROPAGATION_REQUIRED</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="set*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="forward*">PROPAGATION_REQUIRED</prop> <prop key="reject*">PROPAGATION_REQUIRED</prop> <prop key="recover*">PROPAGATION_REQUIRED</prop> <prop key="recycle*">PROPAGATION_REQUIRED</prop> <prop key="freeze*">PROPAGATION_REQUIRED</prop> <prop key="transfer*">PROPAGATION_REQUIRED</prop> <prop key="change*">PROPAGATION_REQUIRED</prop> <prop key="build*">PROPAGATION_REQUIRED</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="*WithLog">PROPAGATION_REQUIRED</prop> <prop key="prepare*">PROPAGATION_REQUIRED</prop> <prop key="init*">PROPAGATION_REQUIRED</prop> <prop key="import*">PROPAGATION_REQUIRED</prop> <prop key="fill*">PROPAGATION_REQUIRED</prop> <prop key="restore*">PROPAGATION_REQUIRED</prop> <prop key="oper*">PROPAGATION_REQUIRED</prop> <prop key="batch*">PROPAGATION_REQUIRED</prop> <prop key="need*">PROPAGATION_REQUIRED</prop> <!--<prop key="get*">PROPAGATION_REQUIRED</prop> --> <prop key="copy*">PROPAGATION_REQUIRED</prop> <prop key="extractive*">PROPAGATION_REQUIRED</prop> <prop key="getNeedCarryInfo">PROPAGATION_REQUIRED</prop> <prop key="parseAndCompareItem">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <!-- Generic manager that can be used to do basic CRUD operations on any objects --> <bean id="uploadService" parent="txProxyTemplate"> <property name="target"> <bean class="com.krm.slsint.common.services.impl.UploadServiceImpl"> <property name="uploadDAO" ref="uploadDAO" /> </bean> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>

2.如果不使用继承的方式,则还需要配置目标范围,并且去掉abstract="true"属性

    <!-- 为事务代理bean设置目标bean --> <property name="target"> <!-- 采用嵌套bean配置目标bean--> <bean class="test.dao.impl.TaoTemplatesDAOImpl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </property> 

 

  定义一个hibernate的SessionFactory

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <!-- module part start -->
                <!--以下用来列出所有的PO映射文件-->
                <value>com/krm/slsint/module/vo/xxx.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.jdbc.batch_size">20</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
            </props>
        </property>
        <property name="lobHandler">
            <bean class="com.krm.slsint.util.UniversalLobHandler" />
        </property>
    </bean>

  

  定义JNDI数据源:

    <!-- JNDI DataSource for J2EE environments -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName"><value>java:comp/env/jdbc/managedb</value></property>
    </bean>

  定义DBCP数据源:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>${hibernate.connection.driver_class}</value>
        </property>
        <property name="url">
            <value>${hibernate.connection.url}</value>
        </property>
        <property name="username" value="${hibernate.connection.username}" />
        <property name="password" value="${hibernate.connection.password}" />
        <property name="maxIdle" value="500" />
        <property name="maxActive" value="4000" />
        <property name="maxWait" value="100" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="180" />
        <property name="logAbandoned" value="true" />
        <property name="initialSize" value="10" />
    </bean>  

  更多数据源定义方法不再例举。

 

你可能感兴趣的:(Spring 事务)