标题翻译
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 = "/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();
/* LOGGER.info("===etlServerAccessEntity.getAccessToken()===");
LOGGER.info(etlServerAccessEntity.getAccessToken());
LOGGER.info("isAlive=" + 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<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());
}
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("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();
}
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论