从Spring到Spring的启动迁移.嵌入式Spring数据源 [英] Spring to Spring boot migration. Embedded Spring Data source

查看:94
本文介绍了从Spring到Spring的启动迁移.嵌入式Spring数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,已将Spring Boot应用程序与自定义数据源配置一起使用,例如

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

之前

@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;
}

上面的代码有效

之后(要使用嵌入式数据源,请删除DataSourceAutoConfiguration排除项或vs)

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);
    }
}

此外,我试图使用两组实际上相同的属性来使其按标准使用,一组在整个教程中使用,而第二组-来自Spring boot的嵌入式Hikari impl.Octava项目的Business子模块中的 D:\ Projects \ Octava \ Business \ src \ main \ resources \ application-businessProduction.properties 文件的一部分

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.

这是嵌入式Hikari数据源的初始化

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?

更新:

重新启动IntelliJ IDEA之后,将创建数据源,但是有一个新异常

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)

推荐答案

因此,最后,我已将其修复.

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.

最终属性是(已删除带有spring.datasource.hikari.前缀的属性)

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'
    }

这篇关于从Spring到Spring的启动迁移.嵌入式Spring数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆