473,511 Members | 14,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Long-term database connections vs. short-lived connections

36 New Member
Hi, I'm a bit new to MySQL; here's a philisophical question.

I'm working on getting my head round apache2+mod_python+mysql for web applications. It all seems pretty straight-forward, and I'm making great progress so far.

However, I'd like to know your opinions on whether it is better to open a database connection once, globally, at the beginning of each module, and keep it open "forever" or to do a "drive by query"; i.e. open/query/close every time.

The way mod_python works, is that a python process is started for each apache server thread, and this is run "forever" - until the server is shut down/restarted. If I choose to open the connection once only at the head of each module, the connections could become broken (due to timeout/loss of network connection/whatever) and cause a crash. On the other hand, if I go the "drive by" route, and the site becomes very busy (theoretically, of course) this could result in a large number of connection opens and closes, which could be computationally expensive.

I suppose a mid-ground, "managed persistent connection" may solve this but the code complexity is probably worse.

Thanks for any input!

regards,
-cybervegan
Jul 22 '08 #1
2 3345
Atli
5,058 Recognized Expert Expert
Hi.

Given the stateless nature of the web, would it really be a good idea to keep the connection open forever?

I'm not really understanding how this would work either. Would this single connection be serving multiple clients, or would a new "eternal" connection be made for each client?

If it is the former, would that not be risking performance hits, as the single connection would most likely have to complete a task for one client before it could start one for a second client. If the server became busy, would it not risk forming huge queues of pending queries?

If it is the latter, would it not still be hurting performance in the long run, as each "eternal" connection would be occupying a connection to the server, even tho the user may be long gone?

In the long run, simply opening a connection on each request and closing it when the request has been served sounds to me a more stable method.

If that is causing problems, a mid-level database connection management layer may be a good idea.
Some software to manage the connections, re-use connections for multiple requests and open new once if the load becomes to heavy.

... which is probably just about the same thing you said... o well :P
Jul 23 '08 #2
cybervegan
36 New Member
Hi.

Given the stateless nature of the web, would it really be a good idea to keep the connection open forever?
That's exactly my point. I'm breaking the app down into modules which will provide functions for working with each table within the database - providing centralised queries, updates and deletes, so that the code will remain easy to maintain.

I'm aware that database connections can go stale for many reasons, but also that constantly opening new connections and closing them again after only one operation could be expensive for the db server.

I'm not really understanding how this would work either. Would this single connection be serving multiple clients, or would a new "eternal" connection be made for each client?
Apache starts a new thread for each new concurrent http connection, up to a point, and re-uses these threads as old clients finish and new clients start. With mod_python, each thread gets its own python interpreter, which continues to run indefinitely; any modules loaded stay loaded until web-server shutdown.

If it is the former, would that not be risking performance hits, as the single connection would most likely have to complete a task for one client before it could start one for a second client. If the server became busy, would it not risk forming huge queues of pending queries?
As there's one for each web-server thread, and these are re-used for new connections, only slightly so - queuing shouldn't occur much except under heavy load. At this point i'd have to start thinking about farming the basic components off onto separate, possibly clustered servers for dynamic web, static web and db functions. Whereas this is just an exercise in learning, I want to code it so it *could* cope with, or at least be scalable...

If it is the latter, would it not still be hurting performance in the long run, as each "eternal" connection would be occupying a connection to the server, even tho the user may be long gone?

In the long run, simply opening a connection on each request and closing it when the request has been served sounds to me a more stable method.

If that is causing problems, a mid-level database connection management layer may be a good idea.
I think what I will do is put in a timer or counter from which I will determine if I should open a new connection or not, within a marshalling method wrapping the "connect" function. That way I can avoid the perils of stale connections and also those of thrashing the db server with "meelions" of short-lived connections.


Some software to manage the connections, re-use connections for multiple requests and open new once if the load becomes to heavy.

... which is probably just about the same thing you said... o well :P
Thanx for helping me make up my mind ;-)

regards,
-cybervegan
Jul 23 '08 #3

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

Similar topics

8
100228
by: Tim Clacy | last post by:
How is a 64 bit type defined in strict C++? It seems C has support for 'long long' since C99, but not so for C++? Looking through one compiler vendor's standard library headers has clouded the...
7
3581
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
5
4784
by: Mark Shelor | last post by:
Problem: find a portable way to determine whether a compiler supports the "long long" type of C99. I thought I had this one solved with the following code: #include <limits.h> #ifdef...
24
1836
by: Michael B Allen | last post by:
I use long longs occasionally. I know there are some limitations regarding the standards such as not using long long constants but what's the big deal? Why is long long not used so much? Mike
9
3921
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
21
2780
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be...
12
13372
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
2
7253
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a...
10
3797
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
15
2717
by: Oliver Graeser | last post by:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte = 64 bit right? but when I try to assign a value with more than 32 bit, it fails. To illustrate: for (i=0; i<64; i++){...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.