我有2个数据源。主数据库编写良好,因此我将其与JPA一起用于多个查询。相反,辅助数据源使用一个非常丑陋的数据库,但是我只需要执行一个大查询(而无需执行其他任何操作)。 在此link之后,我能够(在weblogic上)设置辅助数据源,所以现在我的目标是在辅助数据源上调用本机查询。
这是我的代码:
application.properties
spring.datasource.jiano.jndi-name=jdbc/JianoDS
spring.datasource.jiano.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.jiano.hikari.connection-timeout=60000
spring.datasource.jiano.hikari.maximum-pool-size=5
spring.datasource.sgu.jndi-name=jdbc/sguDatasource
spring.datasource.sgu.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.sgu.hikari.connection-timeout=60000
spring.datasource.sgu.hikari.maximum-pool-size=5
Spring boot main:
@ComponentScan
@SpringBootApplication
public class BemonitorcaaApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(BemonitorcaaApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(BemonitorcaaApplication.class);
}
}
我在下面添加了用于处理多个datasource的类:
@Configuration
public class DatasourceConfig {
@Value("${spring.datasource.jiano.jndi-name}")
private String primaryJndiName;
@Value("${spring.datasource.sgu.jndi-name}")
private String secondaryJndiName;
private JndiDataSourceLookup lookup = new JndiDataSourceLookup();
@Primary
@Bean(destroyMethod = "") // destroy method is disabled for Weblogic update app ability
public DataSource primaryDs() {
return lookup.getDataSource(primaryJndiName);
}
@Bean(name = "sguDs", destroyMethod = "") // destroy method is disabled for Weblogic update app ability
public DataSource secondaryDs() {
return lookup.getDataSource(secondaryJndiName);
}
}
控制器:
@RestController
@RequestMapping("/test")
public class IapaController {
@Autowired
IapaService iapaService;
@PersistenceContext
EntityManager em;
@RequestMapping("/v1/the-only-query-on-2nd-datasource")
public List<String> test2ndDS() {
List<String> itemList = em.createQuery("Select a.text ......." )
.getResultList(); //Not working
return itemList ;
}
@RequestMapping("/v1/primary-ds-is-working-fine")
public List<IapaTipiAndamenti> test1stDS() {
return iapaService.test1stDS(); //This is working, here for example I will use a typical jpa findAll
}
//...other jpa methods for the primary datasource
}
实体管理器无法正常工作,我试图在DatasourceConfig
内添加实体管理器配置,但是它不起作用。 (例如,我没有要扫描的软件包,因为我只对辅助数据源执行本机查询,该查询返回原始类型,因此没有域或存储库类。)
如何修复实体管理器? (我正在使用Spring Boot 1.5.17.RELEASE)
答案 0 :(得分:1)
您好,您可以将简单的JdbcTemplate对象与第二个数据源一起使用
@Configuration
public class DatasourceConfig {
@Value("${spring.datasource.jiano.jndi-name}")
private String primaryJndiName;
@Value("${spring.datasource.sgu.jndi-name}")
private String secondaryJndiName;
private JndiDataSourceLookup lookup = new JndiDataSourceLookup();
@Primary
@Bean(destroyMethod = "") // destroy method is disabled for Weblogic update app ability
public DataSource primaryDs() {
return lookup.getDataSource(primaryJndiName);
}
@Bean(name = "sguDs", destroyMethod = "") // destroy method is disabled for Weblogic update app ability
public DataSource secondaryDs() {
return lookup.getDataSource(secondaryJndiName);
}
@Bean
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(secondaryDs());
}
}
然后在您的控制器中尝试:
@RestController
@RequestMapping("/test")
public class IapaController {
@Autowired
IapaService iapaService;
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping("/v1/the-only-query-on-2nd-datasource")
public List<String> test2ndDS() {
String query = "Select * ....";
List<String> itemList = (List<String>) jdbcTemplate.queryForList(query, String.class);
return itemList ;
}
@RequestMapping("/v1/primary-ds-is-working-fine")
public List<IapaTipiAndamenti> test1stDS() {
return iapaService.test1stDS(); //This is working, here for example I will use a typical jpa findAll
}
//...other jpa methods for the primary datasource
}