Spring boot connect with multiple schemas in mysql

Viewed 14375

I am not able to configure on how to connect to multiple schemas with spring boot. Until now with spring 4 and XML configuration I was able to only put the DB URL like: jdbc:mysql://180.179.57.114:3306/?zeroDateTimeBehavior=convertToNull and in the entity class specify the schema to use and thus able to connect to multiple schemas.

However with Spring Boot I am not able to achieve the same. If in application.properties I only specify the JDBC URL without schema it gives error:

No database selected

even though I have specified the schema name in entity class. Please suggest how can I achieve the same in Spring Boot? Thanks.

3 Answers

Here is the code which you can use:

public static SessionFactory buildSessionFactory(DatabaseData dbData) {
    SessionFactory sessionFactory = null;
    try {

        if (dbData != null) {

            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(dbData.getDataSourceDriver());
            dataSource.setUrl("jdbc:mysql://" + dbData.getDatabaseIP() + ":" + dbData.getDatabasePort() + "/" + dbData.getDatabaseSchema()
                    + "?autoReconnect=true&useSSL=false");
            dataSource.setUsername(dbData.getDatabaseUserName());
            dataSource.setPassword(dbData.getDatabasePassword());

            LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);

            Properties hibernateProperties = new Properties();

            hibernateProperties.put("hibernate.show_sql", false);

            sessionFactoryBean.setHibernateProperties(hibernateProperties);
            sessionFactoryBean.setPackagesToScan("com.***.***.entity");
            sessionFactoryBean.afterPropertiesSet();
            return sessionFactoryBean.getObject();
        }
    } catch (Exception ex) {
        logger.error("Initial SessionFactory creation failed.", ex);
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory;
}

where , the DatabaseData class specifies as follows:

@Data
public class DatabaseData {

    private String databaseIP;
    private String databasePort;
    private String databaseName;
    private String databaseSchema;
    private String databaseUserName;
    private String databasePassword;
    private String dataSourceDriver;
    private int timeout;
}

And for each schema in your database, you can create an object of this class either by reading the properties from the properties file or by any other program. I hope this will solve your problem.

You can configure multiple datasource in the application.properties file and use approapriate datasource in your entity classes. Since, the sample example is already in the below mentioned link, I am directly pointing to the same. Please refer to the link below and check if it solves your problem:

https://www.ccampo.me/java/spring/2016/02/13/multi-datasource-spring-boot.html

Basically what it specifies is that you can configure multiple datasources and specify the desired one in your Entity

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
Related