I have an application that runs Spring MVC.
I need it to access 2 different databases in my app (one is a PostgreSQL and the other one is a MySQL database).
How do I configure this using just annotations or application.properties file?
Regards.
Spring boot allows you to connect to multiple databases by configuring multiple data sources in a single spring boot application using hibernate and JPA. Spring boot enables repositories to connect to multiple databases using JPA from a single application.
So, to use multiple data sources, we need to declare multiple beans with different mappings within Spring's application context. The configuration for the data sources must look like this: spring: datasource: todos: url: ... username: ...
With the xml one you have to be explicit in which datasource Spring Batch uses. If you don't declare it explicitly with Java based configuration it will try to detect the datasource to work, which will only work in case a single datasource is detected. YOu could try annotating the one to use for Batch with @Primary .
this is how you set up multiple data sources on spring xml file, here is mine for example, hope it helps
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/gl?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="2238295" />
</bean>
<bean id="mainDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/gl_main?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="2238295" />
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="sfAccounting"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.gl.domain.accounting" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="sfCommon"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="mainDataSource" />
<property name="packagesToScan" value="com.gl.domain.common" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txnManagerAccounting"/>
<tx:annotation-driven transaction-manager="txnManagerCommon"/>
<bean id="txnManagerAccounting"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sfAccounting" />
</bean>
<bean id="txnManagerCommon"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sfCommon" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
You can also try to define multiple datasources & assign one of them as primary.
Here is the demo code.
The Primary Datasource:
@MapperScan(basePackages = "com.demo.mysqldao",
sqlSessionFactoryRef = "mysqlSqlSessionFactory")
@Configuration
public class MysqlDatabaseConfig {
@Value("${mysql.datasource.url}")
String jdbcUrl;
@Value("${mysql.datasource.username}")
String jdbcUser;
@Value("${mysql.datasource.password}")
String jdbcPass;
@Value("${mysql.datasource.driverClassName}")
String jdbcProvider;
BasicDataSource src = null;
Logger log = LoggerFactory.getLogger(MysqlDatabaseConfig.class);
@Bean(name = "mysqlDataSource")
@Primary
@PostConstruct
public DataSource mysqlDataSource() {
if (jdbcUrl == null) {
throw new RuntimeException("initialization datasource error with null jdbcUrl");
}
log.info("Using JDBC ------------> " + jdbcUrl);
if (src == null) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(jdbcProvider);
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(jdbcUser);
dataSource.setPassword(jdbcPass);
dataSource.setMaxActive(100);
dataSource.setMinIdle(3);
dataSource.setMaxIdle(10);
dataSource.setMinEvictableIdleTimeMillis(60 * 1000);
dataSource.setNumTestsPerEvictionRun(100);
dataSource.setRemoveAbandoned(true);
dataSource.setRemoveAbandonedTimeout(60 * 1000);
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(true);
dataSource.setTestWhileIdle(true);
dataSource.setTimeBetweenEvictionRunsMillis(30 * 60 * 1000);
src = dataSource;
}
return src;
}
@Autowired
@Qualifier(value = "mysqlDataSource")
DataSource mysqlDataSource;
@Bean("mysqlTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(mysqlDataSource);
}
@Bean("mysqlSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mysqlDataSource);
return sessionFactory.getObject();
}
Another Datasource (In my demo, it's sqlserver)
@MapperScan(basePackages = "com.demo.sqlserverdao",
sqlSessionFactoryRef = "sqlserverSqlSessionFactory")
@Configuration
public class SQLServerDatabaseConfig {
@Value("${sqlserver.datasource.url}")
String jdbcUrl;
@Value("${sqlserver.datasource.username}")
String jdbcUser;
@Value("${sqlserver.datasource.password}")
String jdbcPass;
@Value("${sqlserver.datasource.driverClassName}")
String jdbcProvider;
BasicDataSource src = null;
@Bean(name = "sqlServerDataSource")
@PostConstruct
public DataSource sqlServerDataSource() {
if (jdbcUrl == null) {
throw new RuntimeException("initialization sqlserver datasource error with null jdbcUrl");
}
if (src == null) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(jdbcProvider);
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(jdbcUser);
dataSource.setPassword(jdbcPass);
dataSource.setMaxActive(100);
dataSource.setMinIdle(3);
dataSource.setMaxIdle(10);
dataSource.setMinEvictableIdleTimeMillis(60 * 1000);
dataSource.setNumTestsPerEvictionRun(100);
dataSource.setRemoveAbandoned(true);
dataSource.setRemoveAbandonedTimeout(60 * 1000);
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(true);
dataSource.setTestWhileIdle(true);
dataSource.setTimeBetweenEvictionRunsMillis(30 * 60 * 1000);
src = dataSource;
}
return src;
}
@Autowired
@Qualifier(value = "sqlServerDataSource")
DataSource sqlServerDataSource;
@Bean("sqlserverTransactionManager")
public DataSourceTransactionManager sqlserverTransactionManager() {
return new DataSourceTransactionManager(sqlServerDataSource);
}
@Bean("sqlserverSqlSessionFactory")
public SqlSessionFactory sqlserverSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(sqlServerDataSource);
return sessionFactory.getObject();
}
Ref:https://stackoverflow.com/a/27679997/6037575
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With