This example demonstrates how to use otl_connect_pool.
#include <iostream>
#include <thread>
using namespace std;
#include <stdio.h>
#define OTL_DB2_CLI // Compile OTL 4.0/DB2-CLI
#define OTL_CONNECT_POOL_ON
#include <otlv4.h> // include the OTL 4.0 header file
using ConnectPoolType=otl_connect_pool<otl_connect,otl_exception>;
void f1(ConnectPoolType* p)
{
ConnectPoolType& pool=*p;
ConnectPoolType::connect_ptr p1=pool.get();
if(!p1){
cerr<<"p1 is empty<<"<<endl;
return;
}
try{
cout<<"Using a connect from the pool for the first time..."<<endl;
p1->direct_exec("drop table test_tab",0);
p1->direct_exec("create table test_tab(f1 int)");
otl_stream o(3,"INSERT INTO test_tab VALUES(:f1<int>)",*p1);
o<<10;
o.flush();
o.close();
otl_stream s(3,"SELECT f1 FROM test_tab",*p1);
int f1;
while(!s.eof()){
s>>f1;
cout<<"F1="<<f1<<endl;
}
}catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
}
pool.put(std::move(p1));
}
void f2(ConnectPoolType* p)
{
ConnectPoolType& pool=*p;
ConnectPoolType::connect_ptr p2=pool.get();
if(!p2){
cerr<<"p2 is empty<<"<<endl;
return;
}
try{
cout<<"Using a connect from the pool for the second time..."<<endl;
p2->direct_exec("drop table test_tab2",0);
p2->direct_exec("create table test_tab2(f1 int)");
otl_stream o(3,"INSERT INTO test_tab2 VALUES(:f1<int>)",*p2);
o<<10;
o.flush();
o.close();
otl_stream s(3,"SELECT f1 FROM test_tab2",*p2);
int f1;
while(!s.eof()){
s>>f1;
cout<<"F1="<<f1<<endl;
}
}catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
}
pool.put(std::move(p2));
}
int main()
{
otl_connect::otl_initialize(1); // initialize the database API for multi-threaded environment
try{
ConnectPoolType pool;
// open a pool of 8 connections with auto_commit set to false.
pool.open("scott/tiger@db2cli",false,8);
// spawn two threads and call f1() and f2() concurrently.
thread t1(f1,&pool);
thread t2(f2,&pool);
// wait until both threads are finished.
t1.join();
t2.join();
// shrink the minimum pool size from 8 to 4
pool.shrink_pool(4);
// close the pool.
pool.close();
}catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.var_info<<endl; // print out the variable that caused the error
}
return 0;
}
Using a connect from the pool for the first time...
Using a connect from the pool for the second time...
F1=10
F1=10
Copyright © 1996-2024, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com .
Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.