Spring to Spring boot migration. Embedded Spring Data source

旧时模样 提交于 2021-01-27 23:20:37

问题


Currently, it has been used Spring Boot application with the custom Data source configuration like this one

Before

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, MongoAutoConfiguration.class})
public class SpringBootAppInitializer {

    public static void main(String[] args) {
        System.setProperty("java.awt.headless", "false");
        SpringApplication.run(SpringBootAppInitializer.class);
    }
}

@Bean
public BasicDataSource getDataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    basicDataSource.setUsername(env.getProperty("spring.datasource.username"));
    basicDataSource.setPassword(env.getProperty("spring.datasource.password"));
    basicDataSource.setUrl(env.getProperty("spring.datasource.url"));
    return basicDataSource;
}

The code above is working

After (To use an embedded data source cross out DataSourceAutoConfiguration exclusion OR vs)

@SpringBootApplication(exclude={MongoAutoConfiguration.class})
public class SpringBootAppInitializer {

    public static void main(String[] args) {
        System.setProperty("java.awt.headless", "false");
        SpringApplication.run(SpringBootAppInitializer.class);
    }
}

Also, there are two actually the same groups of Properties I'm trying to use to make it work standard one which is used in whole tutorials and the second one - embedded Hikari impl from Spring boot. the part of D:\Projects\Octava\Business\src\main\resources\application-businessProduction.properties file in Business submodule of Octava project

spring.datasource.platform=postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.data=data-postgresql.sql
spring.datasource.url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.initialization-mode=always
spring.datasource.username=postgres
spring.datasource.password=1

duplicated props for HIkari impl
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
spring.datasource.hikari.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.data-source-properties.hibernate.hbm2ddl.import_files=classpath:data-postgresql.sql
spring.datasource.hikari.jdbc-url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.hikari.username=postgres
spring.datasource.hikari.password=1

Now, I'm trying to remove custom data source creation and use embedded one. But both groups of properties doesn't work.

Here is the initialization of embedded Hikari data source

2020-05-11 20:35:23.922 ERROR 20064 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Disconnected from the target VM, address: '127.0.0.1:54586', transport: 'socket'

Process finished with exit code 1

Why this approach doesn't work, perhaps any alternatives?

UPDATE:

After IntelliJ IDEA has been restarted DataSource is being created but there is a new exception

com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-11 21:55:55.599  INFO 12668 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-05-11 21:55:55.875 ERROR 12668 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'securityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through method 'setPersistence' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [octava/config/BusinessHibernateConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java/sql/ShardingKey


2020-05-11 21:55:55.906  WARN 12668 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [HikariPool-1 housekeeper] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 java.lang.Thread.run(Thread.java:748)

回答1:


So, finally, I've fixed it.

  • The main fix was taken from here How to set up datasource with Spring for HikariCP? it was all about dependencies
  • also, check this one JDBC connection pool runs out of connections when Context reload="true" is enabled in Tomcat didn't do it in my case but perhaps will help close / destroy-methods should be added for old implementations.

Final properties are (properties with spring.datasource.hikari. prefix was removed)

spring.datasource.platform=postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.data=data-postgresql.sql
spring.datasource.url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.initialization-mode=always
spring.datasource.username=postgres
spring.datasource.password=1

Gradle changes to make it work

    compile('com.zaxxer:HikariCP:2.7.6') {
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
    compile('org.hibernate:hibernate-hikaricp:4.3.8.Final') {
        //they are pulled in separately elsewhere, to avoid version conflicts
        exclude group: 'org.hibernate', module: 'hibernate-core'
        exclude group: 'com.zaxxer', module: 'HikariCP'
    }


来源:https://stackoverflow.com/questions/61735853/spring-to-spring-boot-migration-embedded-spring-data-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!