Feb 05 2009

Spring配置事务的几种方式

Category: 技术ssmax @ 12:53:26

这几天搞spring的事务,试过了几种方式,现在归纳一下,前面两种都是xml格式的,后面一种是AspectJ的

1、最基本的,基于bean定义的方式,这种方式就是通过BeanNameAutoProxyCreator和拦截器来实现,比较适用于旧版本的Spring

<beans>
    <!– Transaction manager for a single Hibernate SessionFactory (alternative to JTA) –>
    <bean id=”transactionManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
        <property name=”sessionFactory”>
            <ref bean=”sessionFactory”/>
        </property>
    </bean>

    <bean id=”transactionInterceptor” class=”org.springframework.transaction.interceptor.TransactionInterceptor”>
        <property name=”transactionManager” ref=”transactionManager”/>?
          <property name=”transactionAttributes”>
             <props>
                  <prop key=”*”>PROPAGATION_REQUIRED</prop>
                  <prop key=”find*”>PROPAGATION_REQUIRED,readOnly</prop>
             </props>
          </property>
    </bean>

    <bean class=”org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”>
        <property name=”beanNames”>
            <value>*Service,*Manager</value>
        </property>
        <property name=”interceptorNames”>
            <list>
                <value>transactionInterceptor</value>
                <!– 此处增加新的Interceptor –>
            </list>
        </property>
    </bean>

    <bean class=”org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor”>
        <property name=”transactionInterceptor” ref=”transactionInterceptor”/>?
    </bean>

    <bean id=”userManager” class=”some.package.UserManagerImpl” autoWire=”byName”/>

</beans>

 

2、这种方式是基于xml的aop设置,这种方式就是基于aop的xml配置,有切入点和通知,好处是不用在下面的Bean里面用AspectJ

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
     xmlns:aop=”http://www.springframework.org/schema/aop”
     xmlns:tx=”http://www.springframework.org/schema/tx”
     xsi:schemaLocation=”
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>

<bean id=”testPropertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer” scope=”singleton”>
 <property name=”location”>
  <value>classpath:service.properties</value>
 </property>
</bean>

<bean id=”testDataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource” scope=”singleton”>
 <property name=”driverClass” value=”com.mysql.jdbc.Driver”></property>
 <property name=”jdbcUrl” value=”${jdbc.url}”></property>
 <property name=”user” value=”${jdbc.user}”></property>
 <property name=”password” value=”${jdbc.password}”></property>
        <property name=”autoCommitOnClose” value=”false”/>
        <property name=”initialPoolSize” value=”3″/>
        <property name=”minPoolSize” value=”3″/>
        <property name=”maxPoolSize” value=”10″/>
        <property name=”maxIdleTime” value=”7200″/>
        <property name=”acquireIncrement” value=”3″/>
        <property name=”checkoutTimeout” value=”15000″/>
        <property name=”maxIdleTimeExcessConnections” value=”3600″/>
</bean>

<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
 <property name=”dataSource” ref=”testDataSource”/>
</bean>
 
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
 <tx:attributes>
  <tx:method name=”get*” read-only=”true” />
  <tx:method name=”*” propagation=”REQUIRED” />
 </tx:attributes>
</tx:advice>

<aop:config>
 <aop:pointcut id=”testOperation” expression=”execution(* com.test.ooxx.BeanB.*(..))”/>
 <aop:advisor advice-ref=”txAdvice” pointcut-ref=”testOperation”/>
</aop:config>

<bean id=”Bean_A” class=”com.test.ooxx.BeanA” scope=”singleton”>
 <property name=”dataSource” ref=”testDataSource”></property>
</bean>
<bean id=”Bean_B” class=”com.test.ooxx.BeanB” scope=”prototype”>
<property name=”a” ref=”Bean_A”></property>
</bean>
</beans>

 

3、最后这种就是AspectJ的定义

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
     xmlns:aop=”http://www.springframework.org/schema/aop”
     xmlns:tx=”http://www.springframework.org/schema/tx”
     xsi:schemaLocation=”
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>

<bean id=”testPropertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer” scope=”singleton”>
 <property name=”location”>
  <value>classpath:service.properties</value>
 </property>
</bean>

<bean id=”testDataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource” scope=”singleton”>
 <property name=”driverClass” value=”com.mysql.jdbc.Driver”></property>
 <property name=”jdbcUrl” value=”${jdbc.url}”></property>
 <property name=”user” value=”${jdbc.user}”></property>
 <property name=”password” value=”${jdbc.password}”></property>
        <property name=”autoCommitOnClose” value=”false”/>
        <property name=”initialPoolSize” value=”3″/>
        <property name=”minPoolSize” value=”3″/>
        <property name=”maxPoolSize” value=”10″/>
        <property name=”maxIdleTime” value=”7200″/>
        <property name=”acquireIncrement” value=”3″/>
        <property name=”checkoutTimeout” value=”15000″/>
        <property name=”maxIdleTimeExcessConnections” value=”3600″/>
</bean>

<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
 <property name=”dataSource” ref=”testDataSource”/>
</bean>
<tx:annotation-driven transaction-manager=”txManager”/>

<bean id=”Bean_A” class=”com.test.ooxx.BeanA” scope=”singleton”>
 <property name=”dataSource” ref=”testDataSource”></property>
</bean>
<bean id=”Bean_B” class=”com.test.ooxx.BeanB” scope=”prototype”>
<property name=”a” ref=”Bean_A”></property>
</bean>
</beans>

 

这种方式在类方法里面要用AspectJ声明:

@Transactional(readOnly=false, propagation=Propagation.REQUIRED)
 public boolean process() {…}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.