首页 > 解决方案 > 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连接

提前致谢

标签: javaspring-bootjpawildfly

解决方案


推荐阅读