gpt4 book ai didi

java - 为什么 jruby 不关闭数据库连接?

转载 作者:行者123 更新时间:2023-11-30 05:44:13 24 4
gpt4 key购买 nike

我通过 jruby 对 Oracle 的调用没有关闭它们的数据库连接。

以下是进行调用的网页中的代码:

<%
require 'jdbc_ssl_connection'

# Database settings
url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=**REMOVED**)(PORT=**REMOVED**))(CONNECT_DATA=(SERVICE_NAME=**REMOVED**)))"
output = ""
select_stmt, rest, select_sql = nil
begin
conn = OracleConnection.create(url)
# Display connection using the to_s method of OracleConnection
select_sql = "select FIELD from SCHEMA.TABLE WHERE FIELD='"+@subject["file-name"].first+"'"
select_stmt = conn.create_statement
rset = select_stmt.execute_query select_sql
while (rset.next)
output = output + rset.getString(1)
end

rescue
error = "Error:", $!, "\n"

ensure
if (!select_stmt.nil?)
select_stmt.close
end
if (!rset.nil?)
rset.close
end
if (!conn.nil?)
conn.close_connection
end
end
%>

这是与驱动程序交互的类。

# jdbc_ssl_connection.rb

require 'java'

java_import 'oracle.jdbc.OracleDriver'
java_import 'java.sql.DriverManager'
java_import 'java.util.Properties'

class OracleConnection

@conn = nil

def initialize (url)
@url = url
properties = java.util.Properties.new
properties['user'] = 'REMOVED'
properties['password'] = 'REMOVED'

# Load driver class
oradriver = OracleDriver.new
DriverManager.registerDriver oradriver
@conn = DriverManager.get_connection url, properties
@conn.auto_commit = false

end

# Add getters and setters for all attributes we wish to expose
attr_reader :url, :connection

def close_connection()
@conn.close() unless @conn
end

def prepare_call(call)
@conn.prepare_call call
end

def create_statement()
@conn.create_statement
end

def prepare_statement(sql)
@conn.prepare_statement sql
end

def commit()
@conn.commit
end

def self.create(url)
conn = new(url)
end

def to_s
"OracleConnection [url=#{@url}]"
end
alias_method :to_string, :to_s

end

代码可以工作并且非常简单。我进行了测试,数据库上有大约 100 个打开的 session 。由于某种原因,关闭连接的调用不会停止 session 。有什么想法可能是错误的吗?

最佳答案

  def close_connection()
@conn.close() unless @conn
end

由于条件,您确实想要:@conn.close if @conn

关于java - 为什么 jruby 不关闭数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55129026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com