How can I run Spring with MongoDB disabled, but still installed?

后端 未结 2 850
时光取名叫无心
时光取名叫无心 2021-01-14 10:24

I was reading through this Git issue: https://github.com/spring-projects/spring-boot/issues/7589 with regards to Java Spring boot and am trying to figure out a way to bypass

2条回答
  •  野的像风
    2021-01-14 10:51

    I had the same issue. I want my application to be able to run offline as well, queue stuff and from time to time flush to database. This means it needs to be able to launch offline and connect whenever internet is available. Ofc, Spring crashed my app when it did not succeed in connecting. What worked for me was this:

    @Configuration
    @EnableJpaRepositories(bootstrapMode = BootstrapMode.LAZY)
    public class DataSourceConfig {
    
    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("xxx");
        hikariConfig.setJdbcUrl("xxx");
        hikariConfig.setUsername("xxx");
        hikariConfig.setPassword("xxx");
    
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setPoolName("springHikariCP");
        hikariConfig.setInitializationFailTimeout(-1);
    
        hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
        hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");
        HikariDataSource dataSource = new HikariDataSource(hikariConfig);
    
        return dataSource;
    }
    
    @Bean
    public EntityManager entityManager() {
        return entityManagerFactory().getObject().createEntityManager();
    }
    
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("blabla");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(additionalProperties());
        return em;
    }
    
    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL55Dialect");
    
        return properties;
    }
    }
    

    Also, place this in your App class

    @SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
    

    application.yml looks like this:

    #Spring properties
    spring:
      jpa:
        hibernate:
          ddl-auto: update
      datasource:
        url: "xxx"
        username: xxx
        password: xxx
        driver-class-name: com.mysql.cj.jdbc.Driver
        continue-on-error: true
      hikari:
        connection-timeout: 2000
        initialization-fail-timeout: -1
        auto-commit: true
    

    This works for me, hopefully it will work for you guys as well, I saw many posts around here searching for a solution to this. Good luck!

提交回复
热议问题