snicoll / demo-multi-entity-managers Goto Github PK
View Code? Open in Web Editor NEWDemo how to use several entity managers with Spring Boot
Demo how to use several entity managers with Spring Boot
Hello @snicoll,
I just try to apply this in my code. But don't succeed.
I noticed that there is no @primary datasource in your configuration, I found this weird.
I got to debug, your code and noticed that the entity manager is connected to
jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false
instead of this in your configuration:
app.customer.datasource.url=jdbc:h2:mem:customers;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
app.order.datasource.url=jdbc:h2:mem:orders;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
It seems that the auto-configuration is set instead of the one you specified.
HibernateJpaVendorAdapter.determineDatabaseDialectClass
I'm setting the database manually (adapter.setDatabase(Database.POSTGRESQL);
), because its missing on jpaProperties:
Caused by: java.lang.NullPointerException: null
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.determineDatabaseDialectClass(HibernateJpaVendorAdapter.java:184)
Here is my adaptation of your primary source with hikari data source on it and the manual setting:
package xxxxxx;
///Imports omitted...
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "defaultEntityManagerFactory",
transactionManagerRef = "defaultTransactionManager",
basePackages = {"br.org.pti.basi.domain.repository"})
public class DefaultPersistenceConfig {
private final PersistenceUnitManager persistenceUnitManager;
public DefaultPersistenceConfig(ObjectProvider<PersistenceUnitManager> persistenceUnitManager) {
this.persistenceUnitManager = persistenceUnitManager.getIfAvailable();
}
@Bean
@ConfigurationProperties("spring.jpa")
public JpaProperties customJpaProperties() {
return new JpaProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties defaultDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
//@ConfigurationProperties(prefix = "spring.datasource.properties")
public HikariDataSource defaultDataSource() {
return defaultDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
public LocalContainerEntityManagerFactoryBean defaultEntityManagerFactory( JpaProperties customJpaProperties) {
System.out.println(customJpaProperties.getProperties().toString());
EntityManagerFactoryBuilder builder = this.createEntityManagerFactoryBuilder( customJpaProperties );
return builder
.dataSource(defaultDataSource())
.packages("br.org.pti.basi.domain.entity")
.persistenceUnit("default")
.build();
}
@Bean
@Primary
public JpaTransactionManager defaultTransactionManager( EntityManagerFactory defaultEntityManager ) {
return new JpaTransactionManager(defaultEntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder( JpaProperties customJpaProperties ) {
JpaVendorAdapter jpaVendorAdapter = this.createJpaVendorAdapter(customJpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter, customJpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
adapter.setDatabase(Database.POSTGRESQL);
return adapter;
}
}
PS: How can I use customer or order entityManager (autowired bean)?
Also, @ConfigurationProperties(prefix = "app.order.datasource.properties")
: This section of the application.properties does not exists on your project.
I believe the parameter should be named orderJpaProperties rather than customerJpaProperties?
Doesn't look like it hurts anything, but it confused me a bit since I'm still learning how all the Spring magic works.
When I use multiple entity manager .I Got connection reset problem some times.can u give any suggestions for this.
DataSourceProperties#initializeDataSourceBuilder
allows to initialize the builder and offer similar features than the auto-configuration (for instance auto-detect the driver based on the url).
It would be nice if this project showcases that in action.
Steps to repeat
git clone
cd demo-multi-entity-manager
mvn clean test
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.