You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地运行Spring Boot连接Oracle数据库时网络连接失败求助

Spring Boot连接Oracle数据库报错:网络适配器无法建立连接

错误日志

Could not open JPA EntityManager for transaction
Unable to acquire JDBC Connection [IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=IEChbPx1QbKHU7IgPuOdrQ==)] [n/a]
The Network Adapter could not establish the connection (CONNECTION_ID=IEChbPx1QbKHU7IgPuOdrQ==)
Connection refused: connect, socket connect lapse 1 ms. localhost 1522 0 (2/2) true

本地运行的Spring Boot应用查询Oracle数据库时触发上述错误,已通过SQL Developer验证URL、端口、服务名、用户名密码均有效,但问题仍存在。以下是排查与解决步骤:

排查与解决步骤

  • 修正URL格式
    当前URL存在空格注释,需严格遵循thin模式格式:jdbc:oracle:thin:@//localhost:1522/your_service_name,确保服务名字段无多余字符或空格。

  • 消除依赖/配置冲突
    同时引入spring-boot-starter-jdbc和spring-boot-starter-data-jpa可能导致数据源适配冲突:

    • 可移除spring-boot-starter-jdbc依赖,JPA已包含JDBC核心功能
    • 或注释掉spring.datasource.type=oracle.jdbc.pool.OracleDataSource,让Spring Boot自动适配数据源
  • 验证驱动版本兼容性
    ojdbc11对应Oracle 12cR2及以上版本,若数据库版本较低,需替换为对应驱动(如ojdbc8适配12cR1/18c/19c)。

  • 检查端口与防火墙
    尽管SQL Developer能连接,仍需确认:

    • 用netstat -ano | findstr 1522(Windows)或lsof -i :1522(Linux/Mac)验证1522端口被Oracle进程监听
    • 本地防火墙未阻止Java进程访问1522端口
  • 调整主键生成策略
    Oracle的IDENTITY策略仅支持12c及以上版本的自增列,若数据库版本低于12c,需将@GeneratedValue(strategy = GenerationType.IDENTITY)替换为GenerationType.SEQUENCE并配置对应序列,或使用TABLE策略。


相关配置与代码

pom.xml

<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-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc11</artifactId>
        <scope>runtime</scope>
    </dependency>       
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ucp</artifactId>
    </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>

application.properties

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

server.port=2024
server.servlet.context-path= --removed

spring.datasource.url=jdbc:oracle:thin:@//localhost:1522/--removed servicename
spring.datasource.username=
spring.datasource.password=

spring.datasource.type=oracle.jdbc.pool.OracleDataSource
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jackson.serialization.fail-on-empty-beans=false
spring.jpa.database=oracle
spring.application.name=LITGEMService

主类LitApplication.java

package com.LITIntegration.main;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LitApplication implements CommandLineRunner  {

    @Autowired
    private LitLirRepository litLirRepo;        
    public static void main(String[] args) {
        SpringApplication.run(LitApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        List<LitLirEntity> lirview = litLirRepo.findAll();
        lirview.forEach(System.out :: println);
    }

}

实体类LitLirEntity.java

package com.LITIntegration.main;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "removed")
public class LitLirEntity {
    @Id
    @Column(name = "LICENSE_INVENTORY_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    private String REVIEW_ID;
    private String NOTES;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getREVIEW_ID() {
        return REVIEW_ID;
    }
    public void setREVIEW_ID(String rEVIEW_ID) {
        REVIEW_ID = rEVIEW_ID;
    }
    public String getNOTES() {
        return NOTES;
    }
    public void setNOTES(String nOTES) {
        NOTES = nOTES;
    }
    @Override
    public String toString() {
        return "LitLirEntity [id=" + id + ", REVIEW_ID=" + REVIEW_ID + ", NOTES=" + NOTES + "]";
    }
}

仓库接口LitLirRepository.java

package com.LITIntegration.main;

import org.springframework.data.jpa.repository.JpaRepository;

public interface LitLirRepository extends JpaRepository<LitLirEntity, Long>{

}

内容的提问来源于stack exchange,提问作者Manoj Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:45:15