【问题标题】:JDBCtemplate not closing the inactive connectionJDBC模板不关闭非活动连接
【发布时间】:2021-09-10 15:15:36
【问题描述】:

我有一个从 oracle DB 中检索 sysdate 的 API。

我已使用 JDBCtemplate 执行查询,它按预期显示结果。但是当我检查 DB 中的 JDBC 会话时,我可以看到 Oracle 中有 10 个非活动会话。当我启动应用程序时,不会创建此会话。

在访问 API 时,此空闲会话会在数据库中创建并一直保留到应用程序关闭。在 Spring 文档中提到 JDBC 模板会自动关闭连接。但就我而言,它始终在 oracle 中保持 10 个非活动连接。我这里没有使用任何池化机制。

任何人都可以就此提出建议吗?

package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
//import org.springframework.data.repository.query.Param;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class SessionCheck {
    
    @Autowired
    public DataSource datasource;
    
    @GetMapping("/Check")     
    public Date checkdat() {
    
        
        JdbcTemplate template = new JdbcTemplate(datasource);
        Date sysdate = (Date) template.queryForObject("select sysdate from dual", Date.class);
       
      
               return sysdate;
        
                
    }

}


application.properties

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url =jdbc:oracle:thin:@(DESCRIPTION....
spring.datasource.username= xyz
spring.datasource.password=****

【问题讨论】:

    标签: spring-boot spring-jdbc jdbctemplate


    【解决方案1】:

    我已经在我的应用程序中配置了 hikariCP 池,现在空闲会话减少到 1。最小空闲属性有助于关闭由 jdbctemplate 创建的空闲会话。

     spring.datasource.hikari.maximum-pool-size=10
    spring.datasource.hikari.minimum-idle=1
    spring.datasource.hikari.connection-timeout=20000 
    spring.datasource.hikari.idle-timeout=300000 
    spring.datasource.hikari.max-lifetime=1200000
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 2012-08-08
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多