为什么OraOLEDB的语句缓存功能不像JDBC的PreparedStatement?

时间:2016-05-31 16:30:50

标签: oracle

根据Oracle 12c文档https://docs.oracle.com/cd/E47955_01/win.121/e18594/using.htm#BABCHCDD,语句缓存功能消除了每次执行时解析SQL语句的需要。但是当我查看假设的sscce创建的跟踪文件时,我看到了这种模式:

PARSE
EXEC
FETCH
EXEC
FETCH
EXEC
FETCH
...
EXEC
FETCH

我希望看到的是这种模式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.DataAccess.Client;
namespace OracleTest {
   class Program {
      static void Main(string[] args) {
         string connectionString = "Statement Cache Size=75; Self Tuning=False;";
         OracleConnection connection = new OracleConnection(connectionString);
         connection.Open();
         Console.WriteLine(connection.StatementCacheSize);
         string commandString = "select dummy from dual where dummy = :d";
         OracleParameter parameter = new OracleParameter("d", OracleDbType.Varchar2);
         parameter.Value = "X";
         OracleCommand command = new OracleCommand(commandString);
         command.Connection = connection;
         command.AddToStatementCache = true;
         command.Parameters.Add(parameter);
         string trace = "begin dbms_monitor.session_trace_Enable(binds=>true, waits=>true); end;";
         OracleCommand traceCommand = new OracleCommand(trace);
         traceCommand.Connection = connection;
         traceCommand.ExecuteNonQuery();
         command.Prepare(); // docs say this is a no-op but I tried anyway
         for (int i=1; i<= 10; i++) {
            OracleDataReader reader = command.ExecuteReader();
            while (reader.Read()) {
               Console.WriteLine("a row");
            }
         }
      }
   }
}

这里是代码,连接描述已从connectionString变量中删除:

var array = [];

[CODE]

$("divID").on("click", function(){

  array.push($(this).attr("id"));

[CODE]

});

根据文档,如果您想要完全控制该功能,您必须做三件事:

  • 声明语句缓存的大小
  • 将自我调整设置为false
  • 将OracleCommand的AddToStatementCache成员设置为true

我相信我已经满足了所有要求,但数据提供商并没有按照我的意愿去做。

1 个答案:

答案 0 :(得分:1)

我已经测试了你的代码并且关闭了一个阅读器。
当读者关闭时,代码按预期工作:

   for (int i = 1; i <= 10; i++)
    {
        OracleDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine("a row");
        }
        reader.Close(); // <== close it here
    }
PARSING IN CURSOR #422052712 len=48 dep=0 uid=113 oct=3 lid=113 tim=130588488352 hv=359073571 ad='7ffc22173b90' sqlid='c4hbknhaqf1t3'
select /*edek*/ dummy from dual where dummy = :d
END OF STMT
PARSE #422052712:c=0,e=51,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=272002086,tim=130588488351
BINDS #422052712:
 Bind#0
  oacdty=01 mxl=32(01) mxlc=01 mal=00 scl=00 pre=00
  oacflg=03 fl2=1000010 frm=01 csi=170 siz=32 off=0
  kxsbbbfp=1928ba18  bln=32  avl=01  flg=05
  value="X"
EXEC #422052712:c=0,e=82,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=272002086,tim=130588488501
WAIT #422052712: nam='SQL*Net message to client' ela= 2 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588488558
FETCH #422052712:c=0,e=51,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,plh=272002086,tim=130588488666
STAT #422052712 id=1 cnt=1 pid=0 pos=1 obj=142 op='TABLE ACCESS FULL DUAL (cr=3 pr=0 pw=0 time=46 us cost=2 size=2 card=1)'
WAIT #422052712: nam='SQL*Net message from client' ela= 27757 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588516497
BINDS #422052712:
 Bind#0
  oacdty=01 mxl=32(01) mxlc=01 mal=00 scl=00 pre=00
  oacflg=03 fl2=1000010 frm=01 csi=170 siz=32 off=0
  kxsbbbfp=192bffe0  bln=32  avl=01  flg=05
  value="X"
EXEC #422052712:c=0,e=122,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=272002086,tim=130588516748
WAIT #422052712: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588516872
FETCH #422052712:c=0,e=84,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,plh=272002086,tim=130588516895
WAIT #422052712: nam='SQL*Net message from client' ela= 364 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588517302
BINDS #422052712:
 Bind#0
  oacdty=01 mxl=32(01) mxlc=01 mal=00 scl=00 pre=00
  oacflg=03 fl2=1000010 frm=01 csi=170 siz=32 off=0
  kxsbbbfp=1928ba18  bln=32  avl=01  flg=05
  value="X"
EXEC #422052712:c=0,e=69,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=272002086,tim=130588517411
WAIT #422052712: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588517456
FETCH #422052712:c=0,e=37,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,plh=272002086,tim=130588517475
WAIT #422052712: nam='SQL*Net message from client' ela= 280 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=130588517782
BINDS #422052712:
 Bind#0
  oacdty=01 mxl=32(01) mxlc=01 mal=00 scl=00 pre=00
  oacflg=03 fl2=1000010 frm=01 csi=170 siz=32 off=0
  kxsbbbfp=192bffe0  bln=32  avl=01  flg=05
  value="X"