簡體   English   中英

使用 Oracle JDBC 語句緩存與 Ruby on Rails 和 oracle-enhanced_adapter

[英]Use Oracle JDBC statement caching with Ruby on Rails and oracle-enhanced_adapter

oracle-enhanced_adapter 尚不支持 Oracle 的 JDBC 驅動程序系列的 JDBC 語句緩存。 如何確保我的默認 ActiveRecord JDBC 連接使用客戶端 JDBC 語句緩存?

要激活 JDBC 語句緩存作為解決方法,請將以下代碼放在 RAILS_ROOT/config/initializers 中的 a.rb 文件中

# Extend oracle-enhanced_adapter by some missed features
# Peter Ramm, 2020-12-07
require 'active_record/connection_adapters/oracle_enhanced/jdbc_connection'

ActiveRecord::ConnectionAdapters::OracleEnhanced::JDBCConnection.class_eval do
  alias :org_new_connection :new_connection                                     # remember original implementation

  # Number of SQL cursor to keep open in database even if application closes them after each execution
  JDBC_STATEMENT_CACHE_SIZE = 100

  def new_connection(config)
    raw_connection = org_new_connection(config)                                 # call original implementation first
    Rails.logger.debug("..JDBCConnection.new_connection: Check JDBC implicit statement caching")

    # Allow Oracle JDBC driver to cache cursors
    unless raw_connection.getImplicitCachingEnabled
      Rails.logger.debug("..JDBCConnection.new_connection: Activate JDBC implicit statement caching")
      raw_connection.setImplicitCachingEnabled(true)
    end

    # hold up to 100 cursors open
    if raw_connection.getStatementCacheSize != JDBC_STATEMENT_CACHE_SIZE
      Rails.logger.debug("..JDBCConnection.new_connection: Set JDBC implicit statement caching from #{raw_connection.getStatementCacheSize} to #{JDBC_STATEMENT_CACHE_SIZE}")
      raw_connection.setStatementCacheSize(JDBC_STATEMENT_CACHE_SIZE)
    end

    raw_connection                                                              # return result of original method
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM