【发布时间】:2018-11-18 12:48:44
【问题描述】:
我需要它在一个数据库(MySQL)中访问 2 个不同的架构。
我在这里写了两个配置类:
package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"twodb.webfi","twodb.mc"},
entityManagerFactoryRef = "entityManagerFactory1",
transactionManagerRef = "transactionManager1",
considerNestedRepositories = true)
public class FirstConfig {
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager1()
{
EntityManagerFactory factory = entityManagerFactory1().getObject();
return new JpaTransactionManager(factory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory1()
{
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(Boolean.TRUE);
vendorAdapter.setShowSql(Boolean.TRUE);
factory.setDataSource(dataSource1());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(new String[] {"twodb.webfi.entities","twodb.mc.model"});
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();
}
@Bean(destroyMethod = "close")
public HikariDataSource dataSource1() {
System.out.println("<--HikariDataSource1-->");
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getProperty("ui.db.driver"));
dataSourceConfig.setJdbcUrl(env.getProperty("ui.db.url"));
dataSourceConfig.setUsername(env.getProperty("ui.db.username"));
dataSourceConfig.setPassword(env.getProperty("ui.db.password"));
dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000))); // 10 min
dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000))); // 30 min
dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));
return new HikariDataSource(dataSourceConfig);
}
}
这里是另一个配置类:
package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"twodb.webfi","twodb.mc"},
entityManagerFactoryRef = "entityManagerFactory2",
transactionManagerRef = "transactionManager2",
considerNestedRepositories = true)
public class SecondConfig {
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager2()
{
EntityManagerFactory factory = entityManagerFactory2().getObject();
return new JpaTransactionManager(factory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory2()
{
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(Boolean.TRUE);
vendorAdapter.setShowSql(Boolean.TRUE);
factory.setDataSource(dataSource2());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(new String[] {"twodb.webfi.entities","twodb.mc.model"});
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();
}
@Bean(destroyMethod = "close")
public HikariDataSource dataSource2() {
System.out.println("<--HikariDataSource2-->");
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getProperty("web.db.driver"));
dataSourceConfig.setJdbcUrl(env.getProperty("web.db.url"));
dataSourceConfig.setUsername(env.getProperty("web.db.username"));
dataSourceConfig.setPassword(env.getProperty("web.db.password"));
dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000))); // 10 min
dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000))); // 30 min
dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));
return new HikariDataSource(dataSourceConfig);
}
}
在这里我遇到了异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webCommonDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected single matching bean but found 2: entityManagerFactory1,entityManagerFactory
谁能指出我如何通过使用 JPA + Hibernate 和 Spring-boot 来使用多个数据库的正确方向?
我是 mysql 中的 ui 和 web 架构。两者都是同一个数据库。
【问题讨论】:
-
使用
@Primary作为数据源之一,transactionmanagers -
我可以在哪里使用这个@primary
-
这对我有用。要求:spring boot 2.2.11.RELEASE stackoverflow.com/a/67716454/16044144
-
这个解决方案对我有用!!! enter link description here
标签: java spring spring-boot spring-security spring-data-jpa