📄 otl3_ex113.htm
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta content="text/html; charset=iso-8859-1"
http-equiv="Content-Type">
<meta content="Sergei Kuchin" name="Author">
<meta content="Mozilla/4.76 [en] (Win95; U) [Netscape]"
name="GENERATOR">
<meta
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library"
name="KeyWords">
<title>OTL 4.0, Example 113 (OTL stream pooling, Oracle 8/8i)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 113 (OTL stream pooling, Oracle 7/8/8i)</h1>
</center>
<h1>
Example 113 (OTL stream pooling, Oracle 7/8/8i)</h1>
This example demonstrates the OTL <a href="otl3_stream_pooling.htm">stream
pooling</a> for Oracle 8/8i. The same example can be used with Oracle
7,
if #define OTL_ORA7 is used instead of #define OTL_ORA8 (OTL_ORA8I).
<h2>Source Code</h2>
<pre>#include <iostream><br>using namespace std;<br><br>#include <stdio.h><br><pre>// Uncomment the line below when OCI7 is used with OTL<br>// #define OTL_ORA7 // Compile OTL 4.0/OCI7 <br>#define OTL_ORA8 // Compile OTL 4.0/OCI8<br>#define OTL_STL // turn on OTL in the STL compliance mode<br>#define <a
href="otl3_compile.htm#OTL_STREAM_POOLING_ON">OTL_STREAM_POOLING_ON</a> <br> // turn on OTL stream pooling.<br> // #define OTL_STREAM_POOLING_ON line <br> // can be commented out the number of iterations in<br> // the select() loop can be increased, and the difference <br> // in performace with and without OTL_STREAM_POOLING_ON can<br> // be benchmarked. The difference should grow with the overall<br> // number of streams to be used in one program.<br><br>#include <otlv4.h> // include the OTL 4.0 header file<br><br><a
href="otl3_connect_class.htm">otl_connect</a> db; // connect object<br><br>void insert()<br>// insert rows into table<br>{ <br> <a
href="otl3_stream_class.htm">otl_stream</a> o(50, // buffer size<br> "insert into test_tab values(:f1<int>,:f2<char[31]>)", <br> // SQL statement<br> db // connect object<br> );<br> char tmp[32];<br><br> for(int i=1;i<=100;++i){<br> sprintf(tmp,"Name%d",i);<br> o<<i<<tmp;<br> }<br>#ifdef OTL_STREAM_POOLING_ON<br> o.<a
href="otl3_stream_class.htm#close">close</a>(false); // do not save the stream in the stream pool.<br> // in other words, destroy it on the spot, since<br> // the stream is not going to be reused later.<br>#else<br> o.close();<br>#endif<br>}<br><br>void select()<br>{ // when this function is called in a loop,<br> // on the second iteration of the loop the streams i1, i2 will<br> // will get the instances of the OTL stream from the stream<br> // pool, "fast reopen", so to speak.<br><br> <a
href="otl3_stream_class.htm">otl_stream</a> i1(50, // buffer size<br> "select * from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",<br> // SELECT statement<br> db // connect object<br> ); <br> // create select stream<br> <br> <a
href="otl3_stream_class.htm">otl_stream</a> i2(33, // buffer size<br> "select f1,f2 from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",<br> // SELECT statement<br> db // connect object<br> ); <br> // create select stream<br><br>// i1 and i2 are NOT similar, because their buffer sizes as well<br>// as SQL statements are not equal. It will generate two entry points in the<br>// OTL stream pool.<br> <br> int f1;<br> char f2[31];<br><br> i1<<2<<2; // assigning :f11 = 2, :f12 = 2<br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i1.eof()){ // while not end-of-data<br> i1>>f1>>f2;<br> cout<<"I1==> f1="<<f1<<", f2="<<f2<<endl;<br> }<br><br> i2<<3<<3; // assigning :f11 = 2, :f12 = 2<br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i2.eof()){ // while not end-of-data<br> i2>>f1>>f2;<br> cout<<"I2==> f1="<<f1<<", f2="<<f2<<endl;<br> }<br><br>} // destructors of i1, i2 will call the close()<br> // function for both of the streams and the OTL stream<br> // instances will be placed in the stream pool.<br><br>int main()<br>{<br> <a
href="otl3_connect_class.htm">otl_connect::otl_initialize</a>(); // initialize the environment<br> try{<br><br> db.rlogon("scott/tiger"); // connect to the database<br>#ifdef OTL_STREAM_POOLING_ON<br> db.<a
href="otl3_connect_class.htm#set_stream_pool_size">set_stream_pool_size</a>(2); <br> // set the maximum stream pool size and actually initializes <br> // the stream pool.<br> // if this function is not called, the stream pool<br> // gets initialized anyway, with the default size of 32 entries.<br>#endif<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "drop table test_tab",<br> otl_exception::disabled // disable OTL exceptions<br> ); // drop table<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "create table test_tab(f1 int, f2 varchar(30))"<br> ); // create table<br><br> insert(); // insert records into table<br> for(int i=1;i<=10; ++i){<br> cout<<"===================> Iteration: "<<i<<endl;<br> select(); // select records from table<br> }<br> }<br><br> catch(<a
href="otl3_exception_class.htm">otl_exception</a>& p){ // intercept OTL exceptions<br> cerr<<p.msg<<endl; // print out error message<br> cerr<<p.stm_text<<endl; // print out SQL that caused the error<br> cerr<<p.var_info<<endl; // print out the variable that caused the error<br> }<br><br> db.logoff(); // disconnect from the database<br><br> return 0;<br><br>}</pre>
<h2>
Output</h2>
<pre>===================> Iteration: 1<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 2<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 3<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 4<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 5<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 6<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 7<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 8<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 9<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br>===================> Iteration: 10<br>I1==> f1=2, f2=Name2<br>I1==> f1=3, f2=Name3<br>I1==> f1=4, f2=Name4<br>I2==> f1=3, f2=Name3<br>I2==> f1=4, f2=Name4<br>I2==> f1=5, f2=Name5<br>I2==> f1=6, f2=Name6<br><br><br><br><br><br><br><br><br><br><hr
width="100%"></pre>
<center><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a><a
href="home.htm">Go<br>Home</a></center>
<p>Copyright © 1996, 2008, Sergei Kuchin, email: <a
href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,<br><a
href="mailto:skuchin@gmail.com">skuchin@gmail.com<bgmailript
language="JavaScript"><!-- hide from old browsers
var modDate = new Date(document.lastModified)
document.write("<i> Last Updated:</i> " + (modDate.getMonth()+1) + "/" +
modDate.getDate() + "/" + "0"+(modDate.getYear())%100+".");
//-->
</script></a>.<br></p>
<p><i>Permission to use, copy, modify and redistribute this document<br>for<br>any purpose is hereby granted without fee, provided that the above<br>copyright<br>notice appear in all copies.</i>
</p>
</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -