Coder Social home page Coder Social logo

demo-multi-entity-managers's Introduction

🍃 Stéphane Nicoll - Spring Engineering Team

Greetings! I'm Stéphane Nicoll, and my journey in the dynamic world of technology has been deeply intertwined with the transformative power of the Spring framework. With a passion for crafting innovative solutions and a commitment to inclusive collaboration, I'm honored to be a part of the vibrant Spring community.

🚀 About My Spring Journey: Humbled by the ever-evolving landscape of Spring, I've dedicated most of my career to navigating its intricacies and contributing to its growth. From core development to advocating best practices, my goal is to empower developers and teams to harness the full potential of the Spring ecosystem.

🤝 Collaboration in the Spring Community: The heart of Spring beats in its community, and I'm grateful to have collaborated with brilliant minds and passionate developers. Let's connect and explore how our shared experiences in the Spring ecosystem can pave the way for even greater innovation and success.

🌐 Advocating for Inclusive Practices: Beyond code, I'm a firm believer in fostering inclusive practices within the Spring community and elsewhere. Join me in creating an environment where diverse voices are not just heard but celebrated, contributing to a culture of innovation and collaboration.

demo-multi-entity-managers's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

demo-multi-entity-managers's Issues

`DataJpaTest` does not use custom config and configures a "default" persistence unit

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.

Connection timeout

When I use multiple entity manager .I Got connection reset problem some times.can u give any suggestions for this.

HibernateJpaVendorAdapter.determineDatabaseDialectClass problem

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.

Use initializeDataSourceBuilder

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.