473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to keep a connection alive

Hi,

I'm wanting to start a database connection at the start of an app and keep it open for all methods and classes that I'm #including in my project.

First I went about it by creating the connection in the Main source file outside of any class declarations and then declaring it external in other source files.

Expand|Select|Wrap|Line Numbers
  1. // Main cpp file 
  2. #include <mysql++>
  3.  
  4. mysql::Connection * conn;
  5.  
  6. // Class definitions follow, I open the connection in Main()
  7. if (!(conn.connect("dbName", "localhost", "user", "pwd"))) {
  8. ...
  9.  
This works ok within the same source file and although it compiles, the connection is not open in any function defined outside the file (in another source file).

Second method I tried was to pass the mysql::Connection * conn as a parameter: // Call MyFn from a method in Main source file:
MyFn(this, &conn);

connection appears alive but but the app crashes when I execute the query:

Expand|Select|Wrap|Line Numbers
  1. // Function def in Another source file
  2. void myFn(wxFrame *frame, Connection *conn)
  3. {
  4.    mysqlpp::Query query = conn.query();  <--- THIS is probably the cause !?
  5. // set up query code...
  6. // execute
  7.    mysqlpp::StoreQueryResult res = query.store();  // <--- Exception happens here
  8.  
Can anyone help me please ?
Mar 13 '08 #1
5 4979
ronverdonk
4,258 Expert 4TB
Use the pconnect, if that exists in your class.

Ronald
Mar 14 '08 #2
Use the pconnect, if that exists in your class.

Ronald
I don't find any reference to pconnection anywhere. Is it a MySQL object ?
Mar 16 '08 #3
ronverdonk
4,258 Expert 4TB
You will have to look in your programming language reference for the function to open a 'persistent connection'. Like in PHP it is command mysql_pconnect(), as opposed to command mysql_connect().

Ronald
Mar 16 '08 #4
chaarmann
785 Expert 512MB
You can use a connection-pool.

A connection pool is a thread (or process) that keeps a list of all connections. It has functions for con=pool.borrowCoonnection() and pool.returnConnection(con) .
It uses a periodic cleanup-function to go through all connections inside the list and closes the ones which are not used for a certain time.
Mar 16 '08 #5
Thanks, I'll read up on the connection pools
Mar 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Angus SC2 | last post by:
Hello, I have a connection to a mysql server on my website that I would like to keep "alive" from when a user first goes to a page that requires the connection until they close the website How...
1
by: David | last post by:
This is called "evil hack problem" because my problems are due to me trying to do an evil hack. Please don't judge me, just believe that there are reasons for what I'm trying to do :-) I've got...
1
by: Robin Tucker | last post by:
Just a quick question about connection management. My application will never need more than 1 or 2 connections about at any given time. Also, I do not expect many users to be connected at any...
0
by: Gil Strauss | last post by:
When using .net framework HttpWebRequest object, how can we set the connection header to "keep-alive" Acording to Microsoft ducomentation, setting the keepAlive property to true, should do the job,...
5
by: Darrell Sparti, MCSD | last post by:
I'm writing a remote client in C# that connects to a remote connection manager via a TcpClient. The connection and handshake work perfectly. I must then remain connected and respond to the...
2
by: David Rasmussen | last post by:
Someone once asked about this an got no answer: ...
1
by: Nuno Magalhaes | last post by:
With the sniffer ethereal I get a lot of Connections Keep Alive in some sites like google.com and that returns me the Content-Length in the HTTP response header. When the connection is Close I...
0
by: Dody Suria Wijaya | last post by:
I'm looking for a RPC library in Python that can keep its TCP connection alive for the duration of a user's login session. The reason is, each user login to the application server will impose a...
0
by: richard.krehbiel | last post by:
I've been googling for half a day now and have found answers on both sides of the argument. Baiscally it's this: I am using PHP 5.0.4 (I can upgrade if that's a solution) as an ISAPI module under...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.