Spring Boot 应用程序无法连接到 Jenkins Pipeline 中的 MySQL 数据库

问题描述 投票:0回答:1

我正在使用 MySQL 开发一个基本的 Spring Boot Web 应用程序。当我尝试在 Jenkins 中运行它时,遇到无法连接到数据库的问题。该应用程序在本地主机上完美运行,并且数据库可访问。以下是我的设置详细信息以及我遇到的错误。


pipeline {
    agent any
    tools {
        maven "MAVEN3"
        jdk "OracleJDK17"
    }

    stages {
        stage('Fetch code') {
            steps {
                git branch: 'main', url: 'https://github.com/kuanmol/crud-stud'
            }
        }

        stage('Build') {
            steps {
                sh 'mvn package -DskipTests'
            }
            post {
                success {
                    echo 'Now Archiving it...'
                    archiveArtifacts artifacts: '**/target/*.jar'
                }
            }
        }

        stage('UNIT TEST') {
            steps {
                sh 'mvn test'
            }
        }
    }
}

pom.xml配置:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>studentcrud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>studentcrud</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

应用程序属性


`spring.application.name=studentcrud`
`spring.datasource.url=jdbc:mysql://localhost:3306/studentdb`
`spring.datasource.username=root`
`spring.datasource.password=anmolkumar`
`spring.jpa.hibernate.ddl-auto=create-drop`
`spring.jpa.show-sql=true`

错误日志:


:: Spring Boot ::                (v3.3.3)

2024-09-07T12:04:45.581Z  INFO 9444 --- \[studentcrud\] \[           main\] c.e.s.StudentcrudApplicationTests        : Starting StudentcrudApplicationTests using Java 17.0.12 with PID 9444 (started by jenkins in /var/lib/jenkins/workspace/please hoja)
2024-09-07T12:04:45.584Z  INFO 9444 --- \[studentcrud\] \[           main\] c.e.s.StudentcrudApplicationTests        : No active profile set, falling back to 1 default profile: "default"
2024-09-07T12:04:46.655Z  INFO 9444 --- \[studentcrud\] \[           main\] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-09-07T12:04:46.737Z  INFO 9444 --- \[studentcrud\] \[           main\] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 67 ms. Found 1 JPA repository interface.
2024-09-07T12:04:47.490Z  INFO 9444 --- \[studentcrud\] \[           main\] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo \[name: default\]
2024-09-07T12:04:47.565Z  INFO 9444 --- \[studentcrud\] \[           main\] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.5.2.Final
2024-09-07T12:04:47.613Z  INFO 9444 --- \[studentcrud\] \[           main\] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2024-09-07T12:04:48.055Z  INFO 9444 --- \[studentcrud\] \[           main\] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2024-09-07T12:04:48.093Z  INFO 9444 --- \[studentcrud\] \[           main\] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-09-07T12:04:49.847Z  WARN 9444 --- \[studentcrud\] \[           main\] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata

java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)" because the return value of "org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()" is null
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:116) \~\[hibernate-core-6.5.2.Final.jar:6.5.2.Final\]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:290) \~\[hibernate-core-6.5.2.Final.jar:6.5.2.Final\]

\[INFO\]
\[INFO\] Results:
\[INFO\]
\[ERROR\] Errors:
\[ERROR\]   StudentcrudApplicationTests.contextLoads » IllegalState Failed to load ApplicationContext for \[WebMergedContextConfiguration@2fb48970 testClass = com.example.studentcrud.StudentcrudApplicationTests, locations = \[\], classes = \[com.example.studentcrud.StudentcrudApplication\], contextInitializerClasses = \[\], activeProfiles = \[\], propertySourceDescriptors = \[\], propertySourceProperties = \["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"\], contextCustomizers = \[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1c742ed4, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@69c81773, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@210ab13f, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@71687585, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@1f9f6368, org.springframework.boot.test.context.SpringBootTestAnnotation@d06be235\], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null\]
\[INFO\]

详情:

使用 mvn test 和 mvn test -X 在我的本地计算机上成功运行 Maven 测试。 我的 Web 应用程序在本地主机上运行良好,并且可以访问 MySQL 数据库。 我正在寻求帮助来解决 Jenkins 环境中的此问题。任何意见或建议将不胜感激! 想要构建成功。

mysql spring-boot jenkins jenkins-pipeline spring-jdbc
1个回答
0
投票

你找到解决办法了吗? 我也有同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.