1、 在testcontext下的事务操作

步骤1:继承类:extends AbstractTransactionalJUnit4SpringContextTests   

步骤2:申明容器:@ContextConfiguration(locations = { "classpath:normandy/spring/normandy-domain-beans.xml" })

步骤3:申明事物:@TransactionConfiguration(defaultRollback = false)

步骤4:对数据源的事务配置

  1. <bean name="transactionManager" 
  2.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
  3.     <property name="dataSource" ref="dataSource" /> 
  4. </bean> 
  5.  
  6. <tx:annotation-driven transaction-manager="transactionManager" /> 

  默认情况下, 处于testcontext监控下的类是使用事务操作的。如果某个方法不需要事物,则需要进行显示的说明。

  1. @Test 
  2.     @Transactional(propagation=Propagation.NOT_SUPPORTED, isolation=Isolation.DEFAULT) 
  3.     public void testPublish4Length() throws IllegalArgumentException, 
  4.             InterruptedException { 
  5.         List<ConfigValue> configValues0 = configValueUtil.getAllConfigValue(); 
  6.         int versionInit0 = 0
  7.         for (ConfigValue configValue : configValues0) { 
  8.             ConfigKey configKey = configValue.getConfigKey(); 
  9.             if (normandyKey.equals(configKey.getGlobeUid())) { 
  10.                 versionInit0 = configValue.getVersion(); 
  11.             } 
  12.         } 
  13.         System.out.println("==============" + versionInit0); 
  14.  
  15.         try { 
  16.             simpleNormandyClient.publish(normandyKey, normandyValue); 
  17.             Thread.sleep(2000); 
  18.             // 取得version值 
  19.             List<ConfigValue> configValues1 = configValueUtil 
  20.                     .getAllConfigValue(); 
  21.             int versionInit = 0
  22.             for (ConfigValue configValue : configValues1) { 
  23.                 ConfigKey configKey = configValue.getConfigKey(); 
  24.                 if (normandyKey.equals(configKey.getGlobeUid())) { 
  25.                     versionInit = configValue.getVersion(); 
  26.                 } 
  27.             } 
  28.             System.out.println("==============" + versionInit); 
  29.  
  30.             simpleNormandyClient.publish(normandyKey, normandyValue); 
  31.             Thread.sleep(2000); 
  32.             List<ConfigValue> configValues2 = configValueUtil 
  33.                     .getAllConfigValue(); 
  34.  
  35.             int versionLast = 0
  36.             // 取得version值,version=version+1 
  37.             for (ConfigValue configValue : configValues2) { 
  38.                 ConfigKey configKey = configValue.getConfigKey(); 
  39.                 if (normandyKey.equals(configKey.getGlobeUid())) { 
  40.                     versionLast = configValue.getVersion(); 
  41.                 } 
  42.             } 
  43.             System.out.println("==============" + versionLast); 
  44.  
  45.             assertEquals(versionInit + 1, versionLast); 
  46.         } catch (IllegalArgumentException e) { 
  47.             throw new IllegalArgumentException("version检查出错"); 
  48.         } 
  49.     } 
测试结果:
  1. ==============79 
  2. [New I/O client worker #1-1 15/06/11 06:57:33:033 CST]  WARN transport.DubboClientTransport: 373 Fire reconnect event of 10.20.136.19:9022
  3. ==============80 
  4. ==============81 
 
【注意】
1、默认情况下,遇到运行期例外:回滚数据;非运行期例外:不回滚数据