【问题标题】:When I start my spring boot application, my database is not initiating当我启动我的 Spring Boot 应用程序时,我的数据库没有启动
【发布时间】:2021-08-06 05:46:28
【问题描述】:

我有一个 spring-boot 测试应用程序,我在 application.yml 文件中提供了以下配置。当我使用 mvn test 启动服务器时,它不会启动数据库连接,这可能是什么问题?我是否缺少 pom.xml 中的任何依赖项?

spring.datasource.driverClassName = com.ibm.db2.jcc.DB2Driver
spring.datasource.url=jdbc:db2://<fullURL>
spring.datasource.username = <uname>
spring.datasource.password = <password>

以下是我与 spring-boot 相关的依赖项

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.4.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>

这是我的启动日志,其中我没有看到数据库连接日志

[INFO] Running com.test.installments.customers.data.processor.test.RunTest
11:11:30.823 [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.test.in
stallments.customers.data.processor.test.RunTest], using SpringBootContextLoader
11:11:30.829 [main] INFO  o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.test.installments.customers.data.
processor.test.RunTest]: no resource found for suffixes {-context.xml, Context.groovy}.
11:11:30.976 [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]:
 [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springfr
amework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecut
ionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.we
b.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframe
work.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.te
st.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.tes
t.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.co
ntext.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
11:11:30.992 [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBefor
eModesTestExecutionListener@162be91c, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@2488b073, org.springframework.boot.test.mock.mo
ckito.MockitoTestExecutionListener@1c9f0a20, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@55787112, org.springfra
mework.test.context.support.DirtiesContextTestExecutionListener@1cd201a8, org.springframework.test.context.transaction.TransactionalTestExecutionListener@7db82169,
 org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1992eaf4, org.springframework.test.context.event.EventPublishingTestExecutionListener@f74e83
5, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@3276732, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionLi
stener@3f28bd56, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@31e3250d, org.springframework.boot.test.aut
oconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@19fe4644, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListe
ner@21d8bcbe, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@5be067de]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.3)

11:11:31.362 [main] INFO  c.m.i.c.data.processor.test.RunTest - Starting RunTest using Java 11.0.8 on test with PID 17676 (started by test in C:\git\c
ustomers-data-processor-tests)
11:11:31.362 [main] INFO  c.m.i.c.data.processor.test.RunTest - The following profiles are active: dev
11:11:31.446 [main] INFO  c.m.i.c.data.processor.test.RunTest - Started RunTest in 0.422 seconds (JVM running for 1.403)
-----------Databse driver name---------- com.ibm.db2.jcc.DB2Driver
11:11:32.161 [main] DEBUG com.intuit.karate.Suite - [config] classpath:karate-config.js
11:11:32.858 [main] INFO  com.intuit.karate - karate.env system property was: null
11:11:32.932 [main] INFO  com.intuit.karate - [print] Total number of rows
---------------------------------------------------------
feature: classpath:com/test/installments/customers/data/processor/test/fileuload/uploadfile.feature

【问题讨论】:

  • 您尝试使用 spring boot 连接哪个数据库?
  • 尝试连接db2,但后来也尝试连接oracle,但都没有成功。
  • 你没有显示任何试图与数据库对话的地方。如果您使用的是典型的池设置,则您的池在需要之前不会创建连接。 (另请注意,您不应指定 Spring 依赖项的版本;它们是受管理的,并且您可能会引入不兼容性。您会收到警告并应注意它们。)

标签: java spring spring-boot


【解决方案1】:

我注意到的几件事:

  1. 您提到了application.yml,但文件的格式是属性
  2. 您需要在依赖项中添加驱动程序,以便 Spring Boot 也可以使用。
  3. 如果您正在运行测试,并且在src/test/resources 中有一个applicatio.yml,它将覆盖src/main/resources 中的那个

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 1970-01-01
    • 2017-02-19
    • 2019-08-03
    • 2022-07-26
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多