spring多线程事务控制

主要测试内容是两个线程同时对一个表操作时,后一个CCC类中的select之后commit,是否会对AAA类的insert操作rollback产生影响。测试结果是互不影响。

 

spring   xml   配置文件:

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

<!--  数据源连接 -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName">
   <value>oracle.jdbc.driver.OracleDriver</value>
  </property>
  <property name="url">
   <value>jdbc:oracle:thin:@192.168.0.203:1521:orcl</value>
  </property>
  <property name="username">
   <value>YQZL</value>
  </property>
  <property name="password">
   <value>YQZL</value>
  </property>
 </bean>

<!-- 事务处理 -->

 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
 </bean>

<!--  JDBC模版 -->
 <bean id="jdbcTemplate"
  class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource" />

 </bean>
 <!-- 配置spring容器本身提供的线程池任务执行器 -->
 <bean id="taskExecutor"
  class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  <property name="corePoolSize" value="3" />
  <property name="keepAliveSeconds" value="200" />
  <property name="maxPoolSize" value="5" />
  <property name="queueCapacity" value="25" />
 </bean>

<!--  注入类  -->
 <bean id="aaa" class="com.test.AAA">  
  <property name="jdbcTemplate">
   <ref bean="jdbcTemplate" />
  </property>
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
 </bean>

<!-- MAIN类 -->
 <bean id="dd" class="com.test.BBB">
  <property name="taskExecutor">
         <ref bean="taskExecutor"/>
        </property>  
 </bean> 

<!--  注入类  -->
 <bean id="ccc" class="com.test.CCC">  
  <property name="jdbcTemplate">
   <ref bean="jdbcTemplate" />
  </property>
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
 </bean>
 

</beans>

 

 

==================================================>

AAA类:

 

 

package com.test;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class AAA implements Runnable {
 
 private JdbcTemplate jdbcTemplate;
 private DataSourceTransactionManager transactionManager;
 
 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 public DataSourceTransactionManager getTransactionManager() {
  return transactionManager;
 }
 public void setTransactionManager(
   DataSourceTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
 }
 public String LSL(){
  return "aaaaaaaaaaa";
 }
 
 public void run() {
  
  try {
   DefaultTransactionDefinition def = new DefaultTransactionDefinition();

   TransactionStatus status=transactionManager.getTransaction(def);
   jdbcTemplate.update("insert into TEST1 VALUES('ASDF')");
   Thread.sleep(20000);
   transactionManager.rollback(status);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     System.out.println("执行事务AAA");
   }

 
}

 

==========================================================>

BBB  类

 

 

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class BBB {
 private ThreadPoolTaskExecutor taskExecutor;
 
 public ThreadPoolTaskExecutor getTaskExecutor() {
  return taskExecutor;
 }
 public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
  this.taskExecutor = taskExecutor;
 }
 /**
  * @Title: main
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param @param args    设定文件
  * @return void    返回类型
  * @throws
  */
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
  BBB b=(BBB)context.getBean("dd");
  AAA aaa=(AAA)context.getBean("aaa");
  CCC ccc=(CCC)context.getBean("ccc");
  // TODO Auto-generated method stub
  //for(int k = 0; k < 10; k++) {   
   b.taskExecutor.execute(aaa); 
   b.taskExecutor.execute(ccc);
     //}
 }
 

}

 

===============================================>

 

package com.test;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class CCC implements Runnable {
 
 private JdbcTemplate jdbcTemplate;
 private DataSourceTransactionManager transactionManager;
 
 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 public DataSourceTransactionManager getTransactionManager() {
  return transactionManager;
 }
 public void setTransactionManager(
   DataSourceTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
 }

 
 public void run() {
  
  DefaultTransactionDefinition def = new DefaultTransactionDefinition();

  TransactionStatus status=transactionManager.getTransaction(def);
  jdbcTemplate.update("select * from TEST1");
  //Thread.sleep(20000);
  transactionManager.commit(status);
     System.out.println("执行事务CCC");
   }

 
}

 

你可能感兴趣的:(spring,多线程,bean,String,Class,insert)