2008年10月11日星期六

A demo of "connection_pool" in soci.

Source File.

//soci.c
#include <soci.h>
#include <soci-postgresql.h>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <exception>

using namespace soci;
using namespace std;


connection_pool *pool = NULL;

void create_pool()
{
pool = new connection_pool(5);
int pool_size = 5;
session *s[pool_size];
int i = 0;

//Open connection on every session in the pool
for(i = 0; i < pool_size; i ++)
{
s[i] = new session(*pool);
s[i]->open(postgresql, "user=postgres password=milk dbname=mydb host=localhost");
}

//Return every session to pool
for(i = 0; i < pool_size; i ++)
{
delete s[i];
}
}

bool getName(string &name)
{
cout << "Enter name: ";
return cin >> name;
}

int main()
{
try
{
create_pool();

session sql(*pool);

int count;
sql << "select count(*) from phonebook", into(count);

cout << "We have " << count << " entries in the phonebook.\n";

string name;
while (getName(name))
{
string phone;
indicator ind;
sql << "select phone from phonebook where name = :name",
into(phone, ind), use(name);

if (ind == i_ok)
{
cout << "The phone number is " << phone << '\n';
}
else
{
cout << "There is no phone for " << name << '\n';
}
}
}
catch (exception const &e)
{
cerr << "Error: " << e.what() << '\n';
}
}

Makefile:

all:
g++ -g -I/usr/include/postgresql -I/usr/local/include/soci -lsoci_postgresql -lsoci_core soci.c -o soci

--
有一种人,不知道是因为DNA的变异还是前世的夙缘,总是无法安稳下来。他们的生命之流如同咆哮奔涌的大河,没有一刻能够停顿下来。在寂静无人的深夜里,无梦相扰的安睡中,心中也有猛兽会随时醒来,躁动不安,永无宁日。

没有评论: