By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

DB connection pool. How to pass handle?

P: n/a
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.

Mar 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
<se****@total-knowledge.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
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 };
Mar 20 '07 #2

P: n/a
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.

Mar 20 '07 #3

P: n/a
<se****@total-knowledge.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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;
Mar 21 '07 #4

P: n/a
On Mar 20, 6:13 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
<ser...@total-knowledge.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
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;
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.

Mar 21 '07 #5

P: n/a

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:4F***************@newsfe06.lga...
<se****@total-knowledge.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>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;
Ug. I hope you realize I meant
sptk::CODBCDatabase& handle = db.getHandle();

getHandle is a function.
Mar 21 '07 #6

P: n/a
Thanks guys, your were very helpful!

Mar 21 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.