无法在不使用 @Transactional 的情况下切换 AbstractRoutingDataSource。

huangapple 未分类评论80阅读模式
标题翻译

Can't switch AbstractRoutingDataSource without using @Transactional

问题

我有一个Spring Boot项目。如上所述,我想使用AbstractRoutingDataSource来切换数据源。我不想使用@Transactional,因为不需要事务。

但是当我传输我的方法时,似乎像是正在使用@Transactional,但是当我在单元测试中测试我的方法时,它可以正常运行。我认为这很奇怪,因为在单元测试中,@Transactional不应该起作用。

  1. public class DynamicDataSource extends AbstractRoutingDataSource {
  2. @Override
  3. protected Object determineCurrentLookupKey() {
  4. return DynamicContextHolder.peek();
  5. }
  6. }

控制器方法:

  1. @RequestMapping(value = "/pipeline/analyze/{systemId}/{dbName}/{dbSchemaName}/{dbTableName}", method = RequestMethod.GET)
  2. public String pipelineAnalyze(@PathVariable("systemId") Long systemId, @PathVariable("dbName") String dbName, @PathVariable("dbSchemaName") String dbSchemaName, @PathVariable("dbTableName") String dbTableName) throws IOException {
  3. ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
  4. TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
  5. TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
  6. tableInfoMasterPk.setSystemId(systemId);
  7. tableInfoMasterPk.setDbName(dbName);
  8. tableInfoMasterPk.setDbSchemaName(dbSchemaName);
  9. tableInfoMasterPk.setDbTableName(dbTableName);
  10. tableInfoMaster.setId(tableInfoMasterPk);
  11. Example<TableInfoMasterEntity> exampleTableInfoMaster = Example.of(tableInfoMaster);
  12. List<TableInfoMasterEntity> filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
  13. System.out.println("filteredTableInfoMaster.size()=" + filteredTableInfoMaster.size());
  14. TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
  15. pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
  16. return String.valueOf(pipelineRequirementAnalyzeService.toString());
  17. }

单元测试:

  1. @Test
  2. public void findByTemplateNameContaining() throws IOException {
  3. ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
  4. TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
  5. TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
  6. tableInfoMasterPk.setSystemId(1);
  7. tableInfoMasterPk.setDbName("DGRAMES");
  8. tableInfoMasterPk.setDbSchemaName("MESSERIES");
  9. tableInfoMasterPk.setDbTableName("TBLEMSACCESSORYSTATE_MAINTAIN");
  10. tableInfoMaster.setId(tableInfoMasterPk);
  11. Example<TableInfoMasterEntity> exampleTableInfoMaster = Example.of(tableInfoMaster);
  12. List<TableInfoMasterEntity> filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
  13. TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
  14. pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
  15. System.out.println();
  16. }

这两个方法相似,它们都调用了pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);,但结果是不同的。这里是失败的结果成功的结果

英文翻译

I have a Spring-Boot project. As mentioned above, I want to use AbstractRoutingDataSource to switch DataSouce. I don't want to use @Transactional since no transactions are needed.
But when I transfer my method, it seems like which the @Transactional is being used, but when I test my method in unit test, it runs ok, I think it's strange because in unit-test, @Transactional shouldn't work.

  1. public class DynamicDataSource extends AbstractRoutingDataSource {
  2. @Override
  3. protected Object determineCurrentLookupKey() {
  4. return DynamicContextHolder.peek();
  5. }

}

The controller method

  1. @RequestMapping(value = &quot;/pipeline/analyze/{systemId}/{dbName}/{dbSchemaName}/{dbTableName}&quot;, method = RequestMethod.GET)
  2. public String pipelineAnalyze(@PathVariable(&quot;systemId&quot;) Long systemId, @PathVariable(&quot;dbName&quot;) String dbName, @PathVariable(&quot;dbSchemaName&quot;) String dbSchemaName, @PathVariable(&quot;dbTableName&quot;) String dbTableName) throws IOException {
  3. ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
  4. /* LOGGER.info(&quot;===etlServerAccessEntity.getAccessToken()===&quot;);
  5. LOGGER.info(etlServerAccessEntity.getAccessToken());
  6. LOGGER.info(&quot;isAlive=&quot; + etlServer.isAlive());*/
  7. TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
  8. TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
  9. tableInfoMasterPk.setSystemId(systemId);
  10. tableInfoMasterPk.setDbName(dbName);
  11. tableInfoMasterPk.setDbSchemaName(dbSchemaName);
  12. tableInfoMasterPk.setDbTableName(dbTableName);
  13. tableInfoMaster.setId(tableInfoMasterPk);
  14. Example&lt;TableInfoMasterEntity&gt; exampleTableInfoMaster = Example.of(tableInfoMaster);
  15. List&lt;TableInfoMasterEntity&gt; filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
  16. System.out.println(&quot;filteredTableInfoMaster.size()=&quot; + filteredTableInfoMaster.size());
  17. TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
  18. pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
  19. return String.valueOf(pipelineRequirementAnalyzeService.toString());
  20. }

the unit test

  1. @Test
  2. public void findByTemplateNameContaining() throws IOException {
  3. ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
  4. TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
  5. TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
  6. tableInfoMasterPk.setSystemId(1);
  7. tableInfoMasterPk.setDbName(&quot;DGRAMES&quot;);
  8. tableInfoMasterPk.setDbSchemaName(&quot;MESSERIES&quot;);
  9. tableInfoMasterPk.setDbTableName(&quot;TBLEMSACCESSORYSTATE_MAINTAIN&quot;);
  10. tableInfoMaster.setId(tableInfoMasterPk);
  11. Example&lt;TableInfoMasterEntity&gt; exampleTableInfoMaster = Example.of(tableInfoMaster);
  12. List&lt;TableInfoMasterEntity&gt; filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
  13. TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
  14. pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
  15. System.out.println();
  16. }

These two method are similar, they all transfers the pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
but the results are different. here is the failure result and the success result.

huangapple
  • 本文由 发表于 2020年3月16日 15:02:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/60701574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定