At a distant space view post, we chat about codes.

Spring Data JPA excludes database in certain environment/profile.

In some scenario, we just don’t need the database at all, but Spring Data JPA gets error at start up complaining no DB connection.

We can exclude the database connection classes from being created. Then include them back when needed.

First we exclude them from Spring Boot auto-configuration.

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
		DataSourceTransactionManagerAutoConfiguration.class })
@ComponentScan(basePackages = { "com.yourapp" })
public class yourApplication {
	public static void main(String[] args) {, args);

By including, I means we reconfigure Spring Data JPA, when a DB is needed in certain environment/profile.

public class DataScourceConfig {

    private Environment env;

    public DataSource dataSource() {
        return DataSourceBuilder

    public PlatformTransactionManager transactionManager()
        EntityManagerFactory factory = entityManagerFactory().getObject();
        return new JpaTransactionManager(factory);

    public HibernateExceptionTranslator hibernateExceptionTranslator()
        return new HibernateExceptionTranslator();

    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();




        return factoryBean;

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("", env.getProperty("spring.jpa.hibernate.ddl-auto"));
        return properties;


Btw this whole app is on GitHub, in CrawlerWithDB folder.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spring Data JPA excludes database in certain environment/profile.