Java spring boot wildfly与不同dbms的双jndi连接
我正在springboot(但部署到wildfly)的一个项目中工作,在这个项目中,我们必须使用两个不同的数据库,一个是postgres,一个是oracleDb 因为我们在wildfly上部署springboot(是的,我知道它不是很干净,但我们现在不能改变客户端的架构),所以我想管理 连接到数据源服务器端并使用springboot连接到wildfly公开的jndi,这应该可以完美地工作,因为 应该是动态的,我已经设法连接到两个dbs,但hibernate对这两个dbs都使用postgres方言,以下是springboot上的配置: postgres数据源:Java spring boot wildfly与不同dbms的双jndi连接,java,spring-boot,jpa,wildfly,Java,Spring Boot,Jpa,Wildfly,我正在springboot(但部署到wildfly)的一个项目中工作,在这个项目中,我们必须使用两个不同的数据库,一个是postgres,一个是oracleDb 因为我们在wildfly上部署springboot(是的,我知道它不是很干净,但我们现在不能改变客户端的架构),所以我想管理 连接到数据源服务器端并使用springboot连接到wildfly公开的jndi,这应该可以完美地工作,因为 应该是动态的,我已经设法连接到两个dbs,但hibernate对这两个dbs都使用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");
}
}
@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;}
}
OracleDb数据源:
@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");
}
}
@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>
jdbc:postgresql://host:5432/dbname?characterEncoding=utf8
postgresql
用户1
密码1
jdbc:oracle:thin:@host:1521:dbname
神谕
1.
5.
真的
用户2
密码2
org.h2.jdbcx.JdbcDataSource
org.postgresql.xa.PGXADataSource
oracle.jdbc.driver.OracleDriver
另一件奇怪的事情是,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);
}
@存储库
公共接口oracleDsDAO扩展了crudepository{
@查询(value=“选择字段1,”+
“字段2”+
“字段3”+
“字段4”+
“从DB.EXISTING_视图”+
“其中字段1位于(:idList)”,nativeQuery=true)
List extractThingList(@Param(“idList”)List idList);
}
所以感觉很奇怪,我得到了方言例外。。。
正如我所说的,连接正在工作,但是查询正在抛出SqlGrammarException,因为hibernate也在oracle数据库上使用postgre方言
有办法解决这个问题吗?
我总是可以重构配置并通过springboot直接连接到db,但我认为,因为我们在AP上,所以最好使用jndi连接
提前感谢您的项目中是否有
persistence.xml
文件?