【问题标题】:Spring to Spring boot migration. Embedded Spring Data sourceSpring 到 Spring 引导迁移。嵌入式 Spring 数据源
【发布时间】:2020-08-27 08:34:18
【问题描述】:

目前,它已在 Spring Boot 应用程序中使用了像这样的自定义数据源配置

之前

@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 排除 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 引导的嵌入式 Hikari impl。 Octava项目的Business子模块中的D:\Projects\Octava\Business\src\main\resources\application-businessProduction.properties文件部分

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

现在,我正在尝试删除自定义数据源创建并使用嵌入式数据源。但是两组属性都不起作用。

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

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

为什么这种方法不起作用,也许还有其他选择?

更新:

重新启动 IntelliJ IDEA 后,正在创建数据源,但出现新异常

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)

【问题讨论】:

  • 我不完全清楚您要做什么。您的属性显示未嵌入的 postgresql 数据库的配置。从您的调试断点来看,您的属性似乎没有被加载。如果您实际上是在尝试配置嵌入式数据库(而不是 postgres),那么检查一下显而易见的情况:您在运行时类路径上有一个嵌入式数据库(H2、HSQL、Derby 等),对吗?如果您没有得到有用的答案,您可能想尝试澄清您的问题。
  • 属性没问题,因为它可以从 Environment bean 访问(参见前面的部分);我根本不想使用嵌入式数据库。只是尝试仅使用属性来使用 Spring Boot OOTB 数据源 bean 配置(DataSource.class)。因此,主要目标是删除手动创建 DataSource bean(在我的情况下为 BasicDataSource 暗示)仅此而已。 UPD:重新启动我的 Intelij IDEA 数据源开始工作,但 Web 应用程序 [ROOT] 似乎启动了一个名为 [HikariPool-1 管家] 的线程,但未能停止它。这很可能会造成内存泄漏。

标签: java hibernate spring-boot spring-data-jpa datasource


【解决方案1】:

所以,我终于把它修好了。

最终属性是(带有 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 更改以使其工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2017-12-01
    • 2013-03-14
    • 2017-05-30
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多