我通过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个打开的会话。由于某种原因,关闭连接的调用并未停止会话。任何想法可能有什么问题吗?
答案 0 :(得分:1)
def close_connection()
@conn.close() unless @conn
end
由于有条件,您确实想要:@conn.close if @conn