1

I have a spring boot application that uses spring data jdbc and connects to two databases. One of them is postgres and one is oracle. I access them via jdbc repositories (org.springframework.data.repository.Repository).

That works very well in general but all the repositories use the same dialect. How can I use different jdbc dialects based on the jdbc repository that is used for the query?

2 Answers 2

2

Maybe this helps someone else in the future:

It is possible to implement your own version of JdbcRepositoryFactoryBean and hard code the Dialect there. The custom class can then be used with the @EnableJdbcRepositories annotation.

Example configuration for an oracle database:

@EnableJdbcRepositories(basePackageClasses = my.oracle.DbRepository.class,
  jdbcOperationsRef = "secondaryNamedParameterJdbcTemplate",
  transactionManagerRef = "secondaryTransactionManager",
  repositoryFactoryBeanClass = OracleRepositoryFactoryBean.class)

Example of custom factory bean:

public class OracleRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends JdbcRepositoryFactoryBean<T, S, ID> {
   // override constructor and set dialect
   // override setDialect to be safe
}

If there is a more elegant solution please add it.

Sign up to request clarification or add additional context in comments.

Comments

1

In the process of setting the dialect, you may also need to set the Converter and MappingContext, if you need database specific JDBC converters.

I inject the application context into a configuration class (using implements ApplicationContextAware storing the context as a static), and then use that to retrieve my database specific Dialog, MappingContext and Converters.

Note: As of spring-data-relational version 3.2, the JdbcOperations, and DataAccessStrategy are passed in if set using @EnableJdbcRepositories, it is the other 3 mentioned above that need to be set using this method.

public class MyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
        extends JdbcRepositoryFactoryBean<T, S, ID> {

    public MyRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    @Override
    public void setMappingContext(RelationalMappingContext mappingContext) {
        JdbcMappingContext myContext = MyConfig.getBean("myJdbcMappingContext", JdbcMappingContext.class);
        super.setMappingContext(myContext);
    }

    @Override
    public void setDialect(Dialect dialect) {
        Dialect myJdbcDialect = MyConfig.getBean("myJdbcDialect", Dialect.class);
        super.setDialect(myJdbcDialect);
    }

    @Override
    public void setConverter(JdbcConverter converter) {
        JdbcConverter myConverter = MyConfig.getBean("myJdbcConverter", JdbcConverter.class);
        super.setConverter(myConverter);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.