Skip to content

Instantly share code, notes, and snippets.

@dimitrisli
Created October 14, 2015 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitrisli/9a7c04f2d0a52f91d8f0 to your computer and use it in GitHub Desktop.
Save dimitrisli/9a7c04f2d0a52f91d8f0 to your computer and use it in GitHub Desktop.
Spring 4 DB Config - Spring Data JPA and Hibernate - Multiple DBs - Separate Datasources/TransactionManagers/EntityManagers/
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:my/db.properties")
@EnableJpaRepositories(
entityManagerFactoryRef = "DB1EntityManagerFactory",
transactionManagerRef = "DB1TransactionManager",
basePackages = "package.to.db1.dao")
public class DB1Config {
@Autowired private Environment env;
@Bean(name = "DB1datasource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("my.driver.clazz.name"));
dataSource.setUrl("myJDBCUrl");
return dataSource;
}
@Bean(name = "DB1datasourceProxyListeners")
public ChainListener datasourceProxyListeners() {
ChainListener chainListener = new ChainListener();
List<QueryExecutionListener> listeners = new ArrayList<>();
SLF4JQueryLoggingListener slf4jListener = new SLF4JQueryLoggingListener();
slf4jListener.setLogLevel(SLF4JLogLevel.valueOf(env.getProperty("the.loglevel")));
listeners.add(slf4jListener);
listeners.add(new DataSourceQueryCountListener());
chainListener.setListeners(listeners);
return chainListener;
}
@Bean(name = "DB1datasourceProxy")
public DataSource dataSourceProxy() {
ProxyDataSource proxyDataSource = new ProxyDataSource();
proxyDataSource.setDataSource(dataSource());
proxyDataSource.setListener(datasourceProxyListeners());
return proxyDataSource;
}
@Bean(name = "DB1JpaVendorAdapter")
public JpaVendorAdapter jpaVendorAdapter(){
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
//If proxy datasource then we don't need
hibernateJpaVendorAdapter.setShowSql(false);
hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.SQLServer2012Dialect");
return hibernateJpaVendorAdapter;
}
@Bean(name = "DB1EntityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
//datasource or proxy datasource
lef.setDataSource(dataSourceProxy());
lef.setJpaVendorAdapter(jpaVendorAdapter());
lef.setPackagesToScan("package.to.db1.dao.entity");
lef.setPersistenceUnitName("DB1PersistenceUnit");
lef.afterPropertiesSet();
return lef.getObject();
}
@Bean(name = "DB1EntityManager")
public EntityManager entityManager(){
return entityManagerFactory().createEntityManager();
}
@Bean(name = "DB1TransactionManager")
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager((entityManagerFactory()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment