0% found this document useful (0 votes)
1 views4 pages

Config DB

Uploaded by

Quang Minh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Config DB

Uploaded by

Quang Minh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package its.com.restful.

config;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import its.com.restful.utils.Constants;

@Configuration
@EnableJpaRepositories(basePackages = {"its.com.restful.repository"},
entityManagerFactoryRef = "db1EntityManager",
transactionManagerRef = "db1TransactionManager")
public class DataSource1Config {

@Autowired
private Environment env; // Contains Properties Load by @PropertySources

@Primary
@Bean
public DataSource db1Datasource() {

DriverManagerDataSource dataSource = new DriverManagerDataSource();


dataSource.setDriverClassName(env.getProperty("db1.datasource.driver-class-
name"));
dataSource.setUrl(env.getProperty("db1.datasource.url"));
dataSource.setUsername(env.getProperty("db1.datasource.username"));
dataSource.setPassword(env.getProperty("db1.datasource.password"));

return dataSource;
}

@Primary
@Bean
public LocalContainerEntityManagerFactoryBean db1EntityManager() {
LocalContainerEntityManagerFactoryBean em = new
LocalContainerEntityManagerFactoryBean();
em.setDataSource(db1Datasource());

// Scan Entities in Package:


em.setPackagesToScan(new String[] {"its.com.restful.entity"});
em.setPersistenceUnitName("db1EntityManager"); // Important !!

//
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);

HashMap<String, Object> properties = new HashMap<>();

// JPA & Hibernate


properties.put("hibernate.dialect",
env.getProperty("db1.jpa.properties.hibernate.dialect"));
properties.put("hibernate.show-sql", env.getProperty("db1.jpa.show-sql"));

// Solved Error: PostGres createClob() is not yet implemented.


// PostGres Only:
// properties.put("hibernate.temp.use_jdbc_metadata_defaults", false);

em.setJpaPropertyMap(properties);
em.afterPropertiesSet();
return em;
}

@Primary
@Bean
public PlatformTransactionManager db1TransactionManager() {

JpaTransactionManager transactionManager = new JpaTransactionManager();


transactionManager.setEntityManagerFactory(db1EntityManager().getObject());
return transactionManager;
}

===========================================

package its.com.restful.config;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import its.com.restful.utils.Constants;

@Configuration
@EnableJpaRepositories(basePackages = {"its.com.restful.repository2"},
entityManagerFactoryRef = "db2EntityManager",
transactionManagerRef = "db2TransactionManager")
public class DataSource2Config {

@Autowired
private Environment env; // Contains Properties Load by @PropertySources

@Bean
public DataSource db2Datasource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(env.getProperty("db2.datasource.driver-class-
name"));
dataSource.setUrl(env.getProperty("db2.datasource.url"));
dataSource.setUsername(env.getProperty("db2.datasource.username"));
dataSource.setPassword(env.getProperty("db2.datasource.password"));

return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean db2EntityManager() {
LocalContainerEntityManagerFactoryBean em = new
LocalContainerEntityManagerFactoryBean();
em.setDataSource(db2Datasource());

// Scan Entities in Package:


em.setPackagesToScan(new String[] {"its.com.restful.entity2"});

em.setPersistenceUnitName("db2EntityManager");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);

HashMap<String, Object> properties = new HashMap<>();


// JPA & Hibernate
properties.put("hibernate.dialect",
env.getProperty("db2.jpa.properties.hibernate.dialect"));
properties.put("hibernate.show-sql", env.getProperty("db2.jpa.show-sql"));

// Solved Error: PostGres createClob() is not yet implemented.


// PostGres Only.
// properties.put("hibernate.temp.use_jdbc_metadata_defaults", false);

em.setJpaPropertyMap(properties);
em.afterPropertiesSet();
return em;
}

@Bean
public PlatformTransactionManager db2TransactionManager() {

JpaTransactionManager transactionManager = new JpaTransactionManager();


transactionManager.setEntityManagerFactory(db2EntityManager().getObject());
return transactionManager;
}

}
# DataSource (voicebrandname).

db1.datasource.driver-class-name=com.mysql.jdbc.Driver
db1.datasource.url=jdbc:mysql://123.31.17.59:3306/voicebrandname?
useLegacyDatetimeCode=false&characterEncoding=UTF-8
db1.datasource.username=voicebrandname
db1.datasource.password=voicebrandname@3ITS

# DataSource (vbn_mobi).
db2.datasource.driver-class-name=com.mysql.jdbc.Driver
db2.datasource.url=jdbc:mysql://123.31.17.59:3306/vbn_mobi?
useLegacyDatetimeCode=false&characterEncoding=UTF-8
db2.datasource.username=voicebrandname
db2.datasource.password=voicebrandname@3ITS

You might also like