DB connection pool. How to pass handle?

S

sergey

I'm new to C++ and I need to create DB connection pool.

Here is the class that I have:


class uudb
{
public:
uudb();
~uudb();

sptk::CODBCDatabase handle;
void connect(std::string login, std::string password, int server,
std::string sessionid);
void disconnect();
};


uudb::uudb() : handle("DSN=PostgreSQL;UID=serge;PWD=;DATABASE=uu")
{
cout<<"\nOpening DB ...\n";
handle.open();
}

uudb::~uudb()
{
cout<<"\nClosing DB ...\n";
handle.close();
}

void uudb::connect(string login, string password, int server, string
sessionid)
{

try {
CQuery loginQuery(&handle,"select
login:)login,:password,:server)");
loginQuery.param("login") = login;
loginQuery.param("password") = password;
loginQuery.param("server") = server; //
lexical_cast<string>(server);
loginQuery.exec();
cout<<"\nConnected\n";
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}

}

void uudb::disconnect()
{

try {
CQuery logoutQuery(&handle,"select logout()");
logoutQuery.exec();
cout<<"\nDisconnected\n";
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}

}



I need to pass this handle to other classes, so they can use it to
communicate with DB, but I have no idea how to do it.

Your help is appreciated.
 
J

Jim Langston

I'm new to C++ and I need to create DB connection pool.

Here is the class that I have:


class uudb
{
public:
uudb();
~uudb();

sptk::CODBCDatabase handle;
void connect(std::string login, std::string password, int server,
std::string sessionid);
void disconnect();
};


uudb::uudb() : handle("DSN=PostgreSQL;UID=serge;PWD=;DATABASE=uu")
{
cout<<"\nOpening DB ...\n";
handle.open();
}

uudb::~uudb()
{
cout<<"\nClosing DB ...\n";
handle.close();
}

void uudb::connect(string login, string password, int server, string
sessionid)
{

try {
CQuery loginQuery(&handle,"select
login:)login,:password,:server)");
loginQuery.param("login") = login;
loginQuery.param("password") = password;
loginQuery.param("server") = server; //
lexical_cast<string>(server);
loginQuery.exec();
cout<<"\nConnected\n";
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}

}

void uudb::disconnect()
{

try {
CQuery logoutQuery(&handle,"select logout()");
logoutQuery.exec();
cout<<"\nDisconnected\n";
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}

}



I need to pass this handle to other classes, so they can use it to
communicate with DB, but I have no idea how to do it.

Your help is appreciated.

The best way is probably with a reference.

sptk::CODBCDatabase& GetHandle( ) { return handle };
 
S

sergey

Thanks for your reply, it helped.

Now I need to get this handle(with all DSN parameters) in another
class located in different directory.
I tried this:

CPP / C++ / C Code:

void LoginServlet::service()
{
uudb* db = new uudb();
sptk::CODBCDatabase handle = db.getHandle();
//pass handle to DB model class operation
login(login, password, handle);
}


but with no success.
What's the right way to do it? Thanks.
 
J

Jim Langston

Thanks for your reply, it helped.

Now I need to get this handle(with all DSN parameters) in another
class located in different directory.
I tried this:

CPP / C++ / C Code:

void LoginServlet::service()
{
uudb* db = new uudb();
sptk::CODBCDatabase handle = db.getHandle();
//pass handle to DB model class operation
login(login, password, handle);
}


but with no success.
What's the right way to do it? Thanks.

If you coded it the way I showed, it is returning a reference.

sptk::CODBCDatabase& handle = db.getHandle;
 
R

robbins.b

If you coded it the way I showed, it is returning a reference.

sptk::CODBCDatabase& handle = db.getHandle;

You should also declare the copy constructor and overloaded assignment
operator as private so that you prevent applications from making
copies of a database handle.
 
J

Jim Langston

Jim Langston said:
If you coded it the way I showed, it is returning a reference.

sptk::CODBCDatabase& handle = db.getHandle;

Ug. I hope you realize I meant
sptk::CODBCDatabase& handle = db.getHandle();

getHandle is a function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top