【问题标题】:Use Oracle JDBC statement caching with Ruby on Rails and oracle-enhanced_adapter通过 Ruby on Rails 和 oracle-enhanced_adapter 使用 Oracle JDBC 语句缓存
【发布时间】:2021-03-18 22:27:50
【问题描述】:

oracle-enhanced_adapter 还不支持 Oracle 的 JDBC 驱动程序系列的 JDBC 语句缓存。 如何确保我的默认 ActiveRecord JDBC 连接使用客户端 JDBC 语句缓存?

【问题讨论】:

    标签: ruby-on-rails oracle caching jdbc activerecord


    【解决方案1】:

    要激活 JDBC 语句缓存作为解决方法,请将以下代码放在 RAILS_ROOT/config/initializers 中的 .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
    

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多