Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sharing a DBI::Mysql database connection with your children

Reply
Thread Tools

Sharing a DBI::Mysql database connection with your children

 
 
Andrew DeFaria
Guest
Posts: n/a
 
      03-31-2008
I have a process I was thinking of making into a multithreaded daemon
that deals with a MySQL database. The thought is that the daemon would
open the database once, then listen for clients. As clients connected
the daemon would fork off a copy of itself and handle the requests. This
would make the process faster because I wouldn't need to open the
database every time a new client wanted service.

However, I've found that once I fork the database handle (obtained
through DBI) is no longer valid. Reading around a little bit I noticed
people saying to reopen or reconnect to the database in the child. Well
that's the very time consuming thing I was trying to avoid!

Does anybody know how to open a MySQL database such that that database
connection can be used in forked children?
--
Andrew DeFaria <http://defaria.com>
Instead of talking to your plants, if you yelled a them would they still
grow, only to be troubled and insecure?

 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-31-2008
On 2008-03-31 03:45, Andrew DeFaria <> wrote:
> I have a process I was thinking of making into a multithreaded daemon

^^^^^^^^^^^^^
> that deals with a MySQL database. The thought is that the daemon would
> open the database once, then listen for clients. As clients connected
> the daemon would fork off a copy of itself and handle the requests. This

^^^^
Do you want to use threads or fork?

> would make the process faster because I wouldn't need to open the
> database every time a new client wanted service.


This cannot be done. Not only will the database server get a mixture of
requests from different database clients on the same connection (this
could be solved), but it also has to send back all replies via the same
connection: Which client will receive the response? There is no way to
determine that.

(There is at least one RDBMS where the client automatically opens a new
connection when it detects a pid change - presumably the new connection
will be pre-authenticated and faster to establish).

Your best bet is probably to use a pre-forked approach like some web
servers. Run a number of your your daemons in parallel, all listening on
the same port. A client connecting to that port will get any of them.
If all are busy, the client has to wait, or a controlling process can
start more worker processes.

hp

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-31-2008
Andrew DeFaria <> wrote:
>
> I have a process I was thinking of making into a multithreaded daemon
> that deals with a MySQL database. The thought is that the daemon would
> open the database once, then listen for clients. As clients connected
> the daemon would fork off a copy of itself and handle the requests. This
> would make the process faster because I wouldn't need to open the
> database every time a new client wanted service.


On my system it takes less than one millisecond to open a connection
to a MySQL server (located on a different system.) Do you find that
connection time problematic? This is about the same amount of time
it takes to fork in the first place.


> However, I've found that once I fork the database handle (obtained
> through DBI) is no longer valid. Reading around a little bit I noticed
> people saying to reopen or reconnect to the database in the child.


Yes, true.

> Well
> that's the very time consuming thing


I find that hard to believe.

> I was trying to avoid!
>
> Does anybody know how to open a MySQL database such that that database
> connection can be used in forked children?


I don't. I rather doubt other people do, either. Maybe your server
should handle requests internally rather than forking.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-31-2008
Andrew DeFaria schreef:

> Does anybody know how to open a MySQL database such that that database
> connection can be used in forked children?


See connect_cached and Ima:BI.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
szr
Guest
Posts: n/a
 
      03-31-2008
wrote:
> Andrew DeFaria <> wrote:
>>
>> I have a process I was thinking of making into a multithreaded daemon
>> that deals with a MySQL database. The thought is that the daemon
>> would open the database once, then listen for clients. As clients
>> connected the daemon would fork off a copy of itself and handle the
>> requests. This would make the process faster because I wouldn't need
>> to open the database every time a new client wanted service.

>
> On my system it takes less than one millisecond to open a connection
> to a MySQL server (located on a different system.) Do you find that
> connection time problematic? This is about the same amount of time
> it takes to fork in the first place.


Normally this would be true, if the MySQL server is on the same network,
but if it's not, connection time depends entirely on the latency between
the OP's running system and the MySQL server, such is the case if it is
across the internet; it will surely take longer than a millisecond. But
then again, that would hardly be an ideal setup, but it's not unheard
of.

--
szr


 
Reply With Quote
 
Andrew DeFaria
Guest
Posts: n/a
 
      04-01-2008
wrote:
> On my system it takes less than one millisecond to open a connection
> to a MySQL server (located on a different system.) Do you find that
> connection time problematic? This is about the same amount of time it
> takes to fork in the first place.

I'm sorry. I didn't officially measure it. I remember another DBMS being
extremely slow so I assume it. Will you ever forgive me?
--
Andrew DeFaria <http://defaria.com>
Disk Full - Press F1 to belch.

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-01-2008
Andrew DeFaria <> wrote in
news:47f1c1d6$0$89385$:

> I'm sorry. I didn't officially measure it. I remember another DBMS
> being extremely slow so I assume it. Will you ever forgive me?
> --
> Andrew DeFaria <http://defaria.com>
> Disk Full - Press F1 to belch.
>
> Attachment decoded: untitled-2.txt
> --------------060102070906040004010008
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>


I may consider it if you stop posting HTML attachments.

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      04-01-2008
A. Sinan Unur <> wrote:
> Andrew DeFaria <> wrote in
> news:47f1c1d6$0$89385$:
>
>> I'm sorry. I didn't officially measure it. I remember another DBMS
>> being extremely slow so I assume it. Will you ever forgive me?
>> --
>> Andrew DeFaria <http://defaria.com>
>> Disk Full - Press F1 to belch.
>>
>> Attachment decoded: untitled-2.txt
>> --------------060102070906040004010008
>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> <html>
>> <head>

>
> I may consider it if you stop posting HTML attachments.



Please don't tell him how to post.

http://groups.google.com/group/comp....331dca40c4d90a


Nobody else here ignores that netiquette, simply spend your
time answering their questions instead.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Sharing database connection from C to Python dgiagio@gmail.com Python 1 02-01-2006 09:16 PM
Parsing an XML file for a particular element, reading the children, and getting database values Luis Esteban Valencia ASP .Net 0 06-20-2005 12:54 PM
Database connection sharing Atif Jalal ASP .Net 1 08-08-2003 09:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57