当前位置: 首页 > 知识库问答 >
问题:

春到春的引导迁移。嵌入式Spring数据源

颜均
2023-03-14

目前,它已经被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;
}

上面的代码正在工作

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

    public static void main(String[] args) {
        System.setProperty("java.awt.headless", "false");
        SpringApplication.run(SpringBootAppInitializer.class);
    }
}
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

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
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个答案

司空温书
2023-03-14

所以,最后,我把它修好了。

  • 主要的修复是从这里开始的,如何用Spring为Hikaricp设置数据源?这一切都与依赖关系有关
  • 还请检查当启用Context reload=“true”时JDBC连接池是否用完连接。在我的例子中,Tomcat没有这样做,但可能会帮助关闭/破坏-应该为旧的实现添加方法。

最后的属性是(删除了带有spring.datasource.hikari.prefix的属性)

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
    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'
    }
 类似资料:
  • 我试图配置与。但得到这么多的麻烦与版本不同的包我想出了这个 这是的依赖项,它是的依赖项。 当我启动服务器时,一切都正常,但是当我试图用这段代码访问neo4j服务器时。 通过在控制器中调用方法,我得到了拒绝的连接。 我认为嵌入式neo4j没有启动,这就是原因。那么如何启动嵌入式服务器呢。我想如果我使用spring boot,它会自动启动,但事实并非如此。 注:文件包含这是我遵循的项目https://

  • 我有一个常见的任务问题,我可以找到任何解决方案或帮助(也许我需要传递一些属性来工作?)我使用本地服务器1.3.0.M2并创建简单的流 在日志中,我得到了这个: 2017-09-28 12:31:00.644 信息 5156 --- [ -C-1] o.. a.k.c.c.internals.AbstractCoordinator : 成功加入第 1 代的组测试 2017-09-28 12:31:0

  • 我们目前正在使用 SpringXD 将数据从各种数据源摄取到接收器。由于 Springxd 计划终止可用性,我们希望迁移到 Spring Cloud 数据流。 < li >我们为SpringXD编写了几个自定义模块。如何在SCDF使用相同的模块。在SCDF使用这些需要做什么改变。 < li >我们的集群配置了kerberos,SCDF支持kerberized kafka和hadoop吗?。 < l

  • 我正在将Firebase实时数据库迁移到Firestore,并且我有嵌套数据 我有嵌套数据,我想从中创建一个集合。 示例: 我想从FiRecovery中的嵌套数组创建一个子集合。因此我尝试了以下方法(Cloud Function): 但我得到了错误: 您有没有想过以最好的方式迁移嵌套的子数据? 雷诺更新

  • 我有一个Spring Boot和嵌入式Mongo DB的项目,我也想查找存储在那里的数据。我学习了本教程https://springframework.guru/spring-boot-with-embedd-mongoDB/

  • 问题内容: 对于单元测试(如果需要,可将其称为集成测试),我已经在Spring配置中配置了嵌入式数据库,如下所示: 现在,从命令行运行测试时,它们可以正常工作,但是最后我得到一些错误(无害,但很烦人): 现在,包含在异常中的提示通常很好,但是如何将此属性添加到嵌入式数据源?我是否必须扩展它,手动配置它以添加此类 “高级” 功能? 问题答案: 在JDBC网址jdbc:h2:〜/ test中 指定参数