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

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

Can't switch AbstractRoutingDataSource without using @Transactional

问题

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

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

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicContextHolder.peek();
    }

}

控制器方法:

@RequestMapping(value = "/pipeline/analyze/{systemId}/{dbName}/{dbSchemaName}/{dbTableName}", method = RequestMethod.GET)
public String pipelineAnalyze(@PathVariable("systemId") Long systemId, @PathVariable("dbName") String dbName, @PathVariable("dbSchemaName") String dbSchemaName, @PathVariable("dbTableName") String dbTableName) throws IOException {

    ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();

    TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
    TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
    tableInfoMasterPk.setSystemId(systemId);
    tableInfoMasterPk.setDbName(dbName);
    tableInfoMasterPk.setDbSchemaName(dbSchemaName);
    tableInfoMasterPk.setDbTableName(dbTableName);
    tableInfoMaster.setId(tableInfoMasterPk);
    Example<TableInfoMasterEntity> exampleTableInfoMaster = Example.of(tableInfoMaster);

    List<TableInfoMasterEntity> filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
    System.out.println("filteredTableInfoMaster.size()=" + filteredTableInfoMaster.size());
    TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
    pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);

    return String.valueOf(pipelineRequirementAnalyzeService.toString());
}

单元测试:

@Test
public void findByTemplateNameContaining() throws IOException {
    ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
    TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
    TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
    tableInfoMasterPk.setSystemId(1);
    tableInfoMasterPk.setDbName("DGRAMES");
    tableInfoMasterPk.setDbSchemaName("MESSERIES");
    tableInfoMasterPk.setDbTableName("TBLEMSACCESSORYSTATE_MAINTAIN");
    tableInfoMaster.setId(tableInfoMasterPk);
    Example<TableInfoMasterEntity> exampleTableInfoMaster = Example.of(tableInfoMaster);
    List<TableInfoMasterEntity> filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
    TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
    pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
    System.out.println();
}

这两个方法相似,它们都调用了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.

public class DynamicDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
    return DynamicContextHolder.peek();
}

}

The controller method

@RequestMapping(value = &quot;/pipeline/analyze/{systemId}/{dbName}/{dbSchemaName}/{dbTableName}&quot;, method = RequestMethod.GET)
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 {

    ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();

   /* LOGGER.info(&quot;===etlServerAccessEntity.getAccessToken()===&quot;);
    LOGGER.info(etlServerAccessEntity.getAccessToken());
    LOGGER.info(&quot;isAlive=&quot; + etlServer.isAlive());*/


    TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
    TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
    tableInfoMasterPk.setSystemId(systemId);
    tableInfoMasterPk.setDbName(dbName);
    tableInfoMasterPk.setDbSchemaName(dbSchemaName);
    tableInfoMasterPk.setDbTableName(dbTableName);
    tableInfoMaster.setId(tableInfoMasterPk);
    Example&lt;TableInfoMasterEntity&gt; exampleTableInfoMaster = Example.of(tableInfoMaster);


    List&lt;TableInfoMasterEntity&gt; filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
    System.out.println(&quot;filteredTableInfoMaster.size()=&quot; + filteredTableInfoMaster.size());
    TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
    pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);


    return String.valueOf(pipelineRequirementAnalyzeService.toString());
}

the unit test

@Test
public void findByTemplateNameContaining() throws IOException {
    ETLServerAccessEntityInterface etlServerAccessEntity = etlServer.getETLServerAccessEntity();
    TableInfoMasterEntity tableInfoMaster = new TableInfoMasterEntity();
    TableInfoMasterEntityPk tableInfoMasterPk = new TableInfoMasterEntityPk();
    tableInfoMasterPk.setSystemId(1);
    tableInfoMasterPk.setDbName(&quot;DGRAMES&quot;);
    tableInfoMasterPk.setDbSchemaName(&quot;MESSERIES&quot;);
    tableInfoMasterPk.setDbTableName(&quot;TBLEMSACCESSORYSTATE_MAINTAIN&quot;);
    tableInfoMaster.setId(tableInfoMasterPk);
    Example&lt;TableInfoMasterEntity&gt; exampleTableInfoMaster = Example.of(tableInfoMaster);
    List&lt;TableInfoMasterEntity&gt; filteredTableInfoMaster = tableInfoMasterService.findAll(exampleTableInfoMaster);
    TableInfoMasterEntity tableInfoMasterEntity = filteredTableInfoMaster.get(0);
    pipelineRequirementAnalyzeService.analyze(tableInfoMasterEntity);
    System.out.println();
}

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:

确定