java - spring-boot wildfly 与不同 dbms 的双 jndi 连接
问题描述
我正在springboot中的一个项目(但部署到wildfly)中我们必须使用2个不同的数据库,一个是postgres,一个是oracleDb,因为我们在wildfly上部署springboot(是的,我知道它不是很干净,但我们可以'暂时不要更改客户端的架构)我想管理与数据源服务器端的连接并使用springboot连接到wildfly公开的jndi,这应该可以完美地工作,因为一切都应该是动态的,我已经设法连接到dbs 但hibernate都使用postgres方言,这是springboot上的配置:
postgres 数据源:
@Configuration
@PropertySource(value = "classpath:META-INF/spring/application.properties")
@EnableJpaRepositories({
"com.client.product.common.model"
})
@EnableTransactionManagement
public class productJpaConfiguration implements JpaConfigurationTemplate {
@Autowired
private Environment env;
@Override
public Environment environment() {
return env;
}
@Override
public String jndiName() {
return env.getProperty("postgres.ds.jndi.name");
}
@Override
public String[] packagesToScan() {
return new String[] {
"com.client.product.common.model"
};
}
@Override
public void override(final LocalContainerEntityManagerFactoryBean value) {
value.setValidationMode(ValidationMode.NONE);
}
@Override
public JpaPropertiesHandler customJpaPropertiesHandler() {
return (value) -> this.put(value, "hibernate.show_sql", "false");
}
}
OracleDb 数据源:
@Configuration
@PropertySource(value = "classpath:META-INF/spring/application.properties")
@EnableJpaRepositories("com.client.product.extsrv.pas.oracleDB.service.dao")
public class oracleDBJpaConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource(){
DriverManagerDataSource ds = null;
// DataSource ds = null;
try{
Context initialContext = new InitialContext();
ds = (DriverManagerDataSource) initialContext.lookup(env.getProperty("oracleDB.ds.jndi.name"));
if(null!=ds){
ds.getConnection();
}
} catch (NamingException | SQLException e) {
e.printStackTrace();
}
return ds;}
}
这是wildfly上的ds配置:
<datasources>
<datasource jndi-name="java:jboss/datasources/PostgresDBDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://host:5432/dbname?characterEncoding=utf8</connection-url>
<driver>postgresql</driver>
<security>
<user-name>user1</user-name>
<password>password1</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/OracleDBDS" pool-name="OracleDS" enabled="true">
<connection-url>jdbc:oracle:thin:@host:1521:dbname</connection-url>
<driver>oracle</driver>
<pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>user2</user-name>
<password>password2</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="postgresql" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
<driver name="oracle" module="com.oracle">
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
</driver>
</drivers>
</datasources>
另一个奇怪的事情是,oracle 查询实际上是原生的和显式的,如下所示:
@Repository
public interface oracleDsDAO extends CrudRepository<Entity, Long> {
@Query(value = "SELECT field1, " +
"field2, " +
"field3, " +
"field4 " +
"FROM DB.EXISTING_VIEW " +
"WHERE field1 IN (:idList) ", nativeQuery = true)
List<RdaBlocks> extractThingList(@Param("idList") List<Long> idList);
}
所以我得到方言异常感觉很奇怪......正如我所说的连接正在工作但查询正在抛出 SqlGrammarException 因为hibernate也在oracle db上使用postgre方言有没有办法解决这个问题?我总是可以重构配置并通过springboot直接连接到数据库,但我认为,因为我们在AP上,最好使用jndi连接
提前致谢
解决方案
推荐阅读
- javascript - 验证必填字段是否具有单击 vue.js 中的上传按钮的值
- android - 如何处理 RxJava 中多个链式 Observable 的错误?
- apache - 如何在 Jenkins 中禁用 http 到 https 重定向?
- dm-script - 如何在 DM 脚本中更改浮动托盘的宽度
- postgresql - postgresql 过程多选择语句结果作为输出
- javascript - 如何在本机反应中获取嵌套的json数组
- c - E2 Studio 调试
- postgresql - 将复杂的 postgres 查询转换为 Spring Hibernate 存储库的 JPQL
- apache-kafka - 如何根据连接器名称获取 Kafka 源连接器架构
- ios - 如何从标签栏视图控制器返回?