Spring 3のいくつかのトランザクション構成方法

5972 ワード

1つ目の方法:

    ......


   
       
   

   
   
   
   
次の操作を行います.
@Transactional(xxxxxxx)
public void insertOrder(Order order) {
     ......

こうすれば便利なので、使いたいところに@Transactionalをつければいいです
2つ目の方法:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass">
			<value>${datasource.driverClassName}</value>
		</property>

		<property name="jdbcUrl">
			<value>${datasource.url}</value>
		</property>
		<property name="user">
			<value>${datasource.username}</value>
		</property>
		<property name="password">
			<value>${datasource.password}</value>
		</property>

		<property name="minPoolSize">
			<value>${c3p0.minPoolSize}</value>
		</property>

		<property name="maxPoolSize">
			<value>${c3p0.maxPoolSize}</value>
		</property>

		<property name="maxIdleTime">
			<value>${c3p0.maxIdleTime}</value>
		</property>

		<property name="acquireIncrement">
			<value>${c3p0.acquireIncrement}</value>
		</property>
		<property name="maxStatements">
			<value>${c3p0.maxStatements}</value>
		</property>
		<property name="initialPoolSize">
			<value>${c3p0.initialPoolSize}</value>
		</property>

		<property name="idleConnectionTestPeriod">
			<value>${c3p0.idleConnectionTestPeriod}</value>
		</property>

		<property name="numHelperThreads">
			<value>${c3p0.numHelperThreads}</value>
		</property>

		<property name="acquireRetryAttempts">
			<value>${c3p0.acquireRetryAttempts}</value>
		</property>

		<property name="breakAfterAcquireFailure">
			<value>${c3p0.breakAfterAcquireFailure}</value>
		</property>
		<property name="testConnectionOnCheckout">
			<value>${c3p0.testConnectionOnCheckout}</value>
		</property>
	</bean>

	<!--
		 ibatis 
		 <bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" /> <property
		name="configLocation" value="/WEB-INF/conf/SqlMapConfig.xml" />
		</bean>
	-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations"
			value="classpath:mappers/**/*.xml" />
		
	</bean>

	<!-- ############################## -->
	<!--   -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
	</bean>
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="change*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
	<!--  BeanNameAutoProxyCreator -->
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!--  bean  -->
		<property name="beanNames">
			<!--  bean -->
			<value>*Service</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>

この方式は、上の潜在規則に合致しなければならない.