473,748 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

application level database connection

I have a PHP5 application that accepts external messages, and inserts
them into a database. It's rather high traffic.. the server could be
receiving thousands at a time, and recently mysql has been locking up
because it's exceeding the max_connections limit. I raised it for now,
but that's only a temporary fix.

My question is is there a way to make a connection to mysql that
multiple instances of an object will use together? I'm under the
impression that something like a singleton would only live on a per
instance basis, or am I incorrect?

Nov 16 '06 #1
10 1728
"rich" <rb*****@gmail. comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
My question is is there a way to make a connection to mysql that
multiple instances of an object will use together? I'm under the
impression that something like a singleton would only live on a per
instance basis, or am I incorrect?
We'd all need to know a little bit more about your application, and whether
the updates have to be done right away.

The obvious solution that comes to mind is just to have each PHP web process
append to a file, and have a separate daemon that flushes the file to the
database. Unix file semantics guarantee that there is a way to do this with
guaranteed results (for example, the daemon mv's the file then operates on
it ... this guarantees that any append operations either occur before or
after the mv but won't collide with it).

You could have the daemon just look at the file from time to time (once a
second?), or you could use a more fancy triggering mechanism.

They key design factor seems to be whether the database updates have to
complete by the time the PHP web script ends ...

Dave.

Nov 16 '06 #2

David T. Ashley wrote:
"rich" <rb*****@gmail. comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
My question is is there a way to make a connection to mysql that
multiple instances of an object will use together? I'm under the
impression that something like a singleton would only live on a per
instance basis, or am I incorrect?

We'd all need to know a little bit more about your application, and whether
the updates have to be done right away.

The obvious solution that comes to mind is just to have each PHP web process
append to a file, and have a separate daemon that flushes the file to the
database. Unix file semantics guarantee that there is a way to do this with
guaranteed results (for example, the daemon mv's the file then operates on
it ... this guarantees that any append operations either occur before or
after the mv but won't collide with it).

You could have the daemon just look at the file from time to time (once a
second?), or you could use a more fancy triggering mechanism.

They key design factor seems to be whether the database updates have to
complete by the time the PHP web script ends ...

Dave.
No, it doesn't have to be instant, and I definitely thought about this
method. At this point though, it seems like if the application can
hold a connection, it would be a heck of a lot bigger than batching
these inserts. I just don't know if it can. Perhaps an import would
be better anyway for performance reasons...

Nov 16 '06 #3
rich wrote:
I have a PHP5 application that accepts external messages, and inserts
them into a database. It's rather high traffic.. the server could be
receiving thousands at a time, and recently mysql has been locking up
because it's exceeding the max_connections limit. I raised it for now,
but that's only a temporary fix.

My question is is there a way to make a connection to mysql that
multiple instances of an object will use together? I'm under the
impression that something like a singleton would only live on a per
instance basis, or am I incorrect?
Hi,

I understood that if:
- the same login-credentials (username/password)
- from the same host
are used in your scripts, the connection will be recycled if you use
mysql_pconnect( ).

Are you using pconnect?

Also have a look at your databasestructu re: Is every insert causing some
triggers to run (like Primary Key, Constraints, Indexes)??
Especially Indexes can slow your inserts down. For each insert the Index
must be updated.

If your table IS using Indexes, it might help to:
- do your inserts in a very plain table (ISAM) for storage
- move them one by one to the real (tables) later, when demand on your
server is lower.

Regards,
Erwin Moller
Nov 16 '06 #4

Erwin Moller wrote:
Hi,

I understood that if:
- the same login-credentials (username/password)
- from the same host
are used in your scripts, the connection will be recycled if you use
mysql_pconnect( ).

Are you using pconnect?
No, I'm not. I think my biggest problem here that I don't fully
understand is how can I (or is it possible to) use a resource for
multiple instances of an object. I understand that something like a
static object will live throughout an object and it's children. But if
5 different processes open 5 different instances of the same object, is
it even possible in PHP to give them the same resource if they never
know about each other? I suppose if what you say about pconnect is
true, then you just answered my question.

Thanks for the other tips on indexing too.. that'll definitely help.

Nov 16 '06 #5
rich wrote:
>
Erwin Moller wrote:
>Hi,

I understood that if:
- the same login-credentials (username/password)
- from the same host
are used in your scripts, the connection will be recycled if you use
mysql_pconnect ().

Are you using pconnect?

No, I'm not. I think my biggest problem here that I don't fully
understand is how can I (or is it possible to) use a resource for
multiple instances of an object.

Hi,

I think you ONLY have to use mysql_pconnect instead of mysql_connect. PHP
will make sure the same connection isn't used at the same time.
I am not sure how it is implemented, but I expect PHP flags a connection
idle/finished when the script ends, but leaves the connection open, so next
time you need it you won't have to go through the authorisation again.

I understand that something like a
static object will live throughout an object and it's children. But if
5 different processes open 5 different instances of the same object, is
it even possible in PHP to give them the same resource if they never
know about each other?
Yes, that is possible. PHP will handle that, you don't have to worry about
it. PHP must use some connectionpooli ngmechanism behind the scenes, but I
do not know HOW.
But from a coders point of view: When PHP needs a connection, it checks if
it has one already (for the same user/password/host):
- If No, it creates a fresh one
- If yes (AND it is free), it returns that connection.
I suppose if what you say about pconnect is
true, then you just answered my question.

Thanks for the other tips on indexing too.. that'll definitely help.
First check the pconnect, if that doesn't help enough, redesign your
database since that will be a lot more work than adding a 'p' to your
script :-).
Long tables with indexes get very slow for inserts/updates.
Indexes only speed things up when querying (SELECT), but slow down
update/insert instructions.

Good luck!

Regards,
Erwin Moller
Nov 17 '06 #6
Erwin Moller wrote:
rich wrote:

>>Erwin Moller wrote:
>>>Hi,

I understood that if:
- the same login-credentials (username/password)
- from the same host
are used in your scripts, the connection will be recycled if you use
mysql_pconne ct().

Are you using pconnect?

No, I'm not. I think my biggest problem here that I don't fully
understand is how can I (or is it possible to) use a resource for
multiple instances of an object.

Hi,

I think you ONLY have to use mysql_pconnect instead of mysql_connect. PHP
will make sure the same connection isn't used at the same time.
I am not sure how it is implemented, but I expect PHP flags a connection
idle/finished when the script ends, but leaves the connection open, so next
time you need it you won't have to go through the authorisation again.

I understand that something like a
>>static object will live throughout an object and it's children. But if
5 different processes open 5 different instances of the same object, is
it even possible in PHP to give them the same resource if they never
know about each other?


Yes, that is possible. PHP will handle that, you don't have to worry about
it. PHP must use some connectionpooli ngmechanism behind the scenes, but I
do not know HOW.
But from a coders point of view: When PHP needs a connection, it checks if
it has one already (for the same user/password/host):
- If No, it creates a fresh one
- If yes (AND it is free), it returns that connection.
I suppose if what you say about pconnect is
>>true, then you just answered my question.

Thanks for the other tips on indexing too.. that'll definitely help.


First check the pconnect, if that doesn't help enough, redesign your
database since that will be a lot more work than adding a 'p' to your
script :-).
Long tables with indexes get very slow for inserts/updates.
Indexes only speed things up when querying (SELECT), but slow down
update/insert instructions.

Good luck!

Regards,
Erwin Moller
This will only make the situation worse.

Right now he has a problem with the maximum number of connections being
occasionally exceeded. mysql_pconnect( ) requires the maximum number of
connections to be allocated ALL the time.

I have yet to see where mysql_pconnect SOLVES a problem. Normally it
creates MORE. Yes, it's theoretically possible for it to help on very
busy systems where connections are used for a very short time. But
that's not the case here.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 17 '06 #7
Jerry Stuckle wrote:
Erwin Moller wrote:
>rich wrote:

>>>Erwin Moller wrote:

Hi,

I understood that if:
- the same login-credentials (username/password)
- from the same host
are used in your scripts, the connection will be recycled if you use
mysql_pconn ect().

Are you using pconnect?
No, I'm not. I think my biggest problem here that I don't fully
understand is how can I (or is it possible to) use a resource for
multiple instances of an object.

Hi,

I think you ONLY have to use mysql_pconnect instead of mysql_connect. PHP
will make sure the same connection isn't used at the same time.
I am not sure how it is implemented, but I expect PHP flags a connection
idle/finished when the script ends, but leaves the connection open, so
next time you need it you won't have to go through the authorisation
again.

I understand that something like a
>>>static object will live throughout an object and it's children. But if
5 different processes open 5 different instances of the same object, is
it even possible in PHP to give them the same resource if they never
know about each other?


Yes, that is possible. PHP will handle that, you don't have to worry
about it. PHP must use some connectionpooli ngmechanism behind the scenes,
but I do not know HOW.
But from a coders point of view: When PHP needs a connection, it checks
if it has one already (for the same user/password/host):
- If No, it creates a fresh one
- If yes (AND it is free), it returns that connection.
I suppose if what you say about pconnect is
>>>true, then you just answered my question.

Thanks for the other tips on indexing too.. that'll definitely help.


First check the pconnect, if that doesn't help enough, redesign your
database since that will be a lot more work than adding a 'p' to your
script :-).
Long tables with indexes get very slow for inserts/updates.
Indexes only speed things up when querying (SELECT), but slow down
update/insert instructions.

Good luck!

Regards,
Erwin Moller

This will only make the situation worse.
Will it?
You sound very sure.
>
Right now he has a problem with the maximum number of connections being
occasionally exceeded. mysql_pconnect( ) requires the maximum number of
connections to be allocated ALL the time.
That depends.
If the time it takes to run 1 request is short, then a relatively large
amount of time is spend on creating a connection.
If the script runs long, this is relatively short.

In the first case it might help to use pconnect().

But you knew that because you describe it yourself.
So why say it will make the situation worse?

The OP doesn't have to redesign his whole app or something.
It is easily checked with minimum effort.
I think it it definitely worth a try to add the 'p' and see if things run
smoother.

Regards,
Erwin Moller
>
I have yet to see where mysql_pconnect SOLVES a problem. Normally it
creates MORE. Yes, it's theoretically possible for it to help on very
busy systems where connections are used for a very short time. But
that's not the case here.
Nov 17 '06 #8
rich wrote:
I have a PHP5 application that accepts external messages, and inserts
them into a database. It's rather high traffic.. the server could be
receiving thousands at a time, and recently mysql has been locking up
because it's exceeding the max_connections limit. I raised it for now,
but that's only a temporary fix.

My question is is there a way to make a connection to mysql that
multiple instances of an object will use together? I'm under the
impression that something like a singleton would only live on a per
instance basis, or am I incorrect?
That's a lot of messages to be handling. How are they getting to your
server - via web pages, a socket, or some other means? That will make a
difference.

From the MySQL end, you should start by ensuring you're using good
programming practices. Things like closing the database connection as
soon as you're through with it, rather than waiting until the end of the
script (or even worse, depending on the garbage collector to do it for you.

If you have SELECTs running concurrently, especially with INNODB tables,
you may be running into locking issues. This will delay requests,
resulting in a larger number of concurrent connections.

Also, the more indexes you have on your table(s), the longer INSERT
operations will take. Check out your indexes and get rid of any you
don't need.

The recommendation of spooling to the file system can be awkward at
best. You have to ensure you lock the file before writing to it by
anyone or you can end up with a corrupted file.

Personally, I like writing data to a "scratch" table in MySQL. This
would be a MyISAM table with no indexes. It may only have two columns -
an id and unparsed data, or, if the data are always the same, it could
have several columns.

The idea is to have a table that the application can insert data into as
quickly as possible.

Then I have a batch script fetch the unparsed info from this table and
inserts it into the appropriate tables. Depending on how busy the
system is, how much data is being spooled, how long it takes to parse
the data, etc., I might fetch one row at a time - or I might fetch a
dozen, read them all into an array then process them.

The only think you need to watch out for is that you don't exceed the
processing time limit for the PHP script.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 17 '06 #9
Erwin Moller wrote:
Jerry Stuckle wrote:

>>Erwin Moller wrote:
>>>rich wrote:

Erwin Moller wrote:
>Hi,
>
>I understood that if:
>- the same login-credentials (username/password)
>- from the same host
>are used in your scripts, the connection will be recycled if you use
>mysql_pcon nect().
>
>Are you using pconnect?
>

No, I'm not. I think my biggest problem here that I don't fully
understan d is how can I (or is it possible to) use a resource for
multiple instances of an object.

Hi,

I think you ONLY have to use mysql_pconnect instead of mysql_connect. PHP
will make sure the same connection isn't used at the same time.
I am not sure how it is implemented, but I expect PHP flags a connection
idle/finished when the script ends, but leaves the connection open, so
next time you need it you won't have to go through the authorisation
again.

I understand that something like a
static object will live throughout an object and it's children. But if
5 different processes open 5 different instances of the same object, is
it even possible in PHP to give them the same resource if they never
know about each other?
Yes, that is possible. PHP will handle that, you don't have to worry
about it. PHP must use some connectionpooli ngmechanism behind the scenes,
but I do not know HOW.
But from a coders point of view: When PHP needs a connection, it checks
if it has one already (for the same user/password/host):
- If No, it creates a fresh one
- If yes (AND it is free), it returns that connection.
I suppose if what you say about pconnect is
true, then you just answered my question.

Thanks for the other tips on indexing too.. that'll definitely help.
First check the pconnect, if that doesn't help enough, redesign your
database since that will be a lot more work than adding a 'p' to your
script :-).
Long tables with indexes get very slow for inserts/updates.
Indexes only speed things up when querying (SELECT), but slow down
update/insert instructions.

Good luck!

Regards,
Erwin Moller

This will only make the situation worse.


Will it?
You sound very sure.
Yes, I am.
>
>>Right now he has a problem with the maximum number of connections being
occasionall y exceeded. mysql_pconnect( ) requires the maximum number of
connections to be allocated ALL the time.


That depends.
If the time it takes to run 1 request is short, then a relatively large
amount of time is spend on creating a connection.
If the script runs long, this is relatively short.
Actually, the time spent making a connection is not all that long.
Sure, it's longer than reusing a permanent connection, but it's still
not that long.
In the first case it might help to use pconnect().

But you knew that because you describe it yourself.
So why say it will make the situation worse?
Because with permanent connections you have to have the maximum number
of permanent connections required available all the time. This uses up
a huge amount of server resources 24/7 just to handle an occasional peak
in requirements. The result is system and MySQL resources are tied up
unnecessarily almost all of the time.
The OP doesn't have to redesign his whole app or something.
It is easily checked with minimum effort.
I think it it definitely worth a try to add the 'p' and see if things run
smoother.
I repeat - I have YET to see where mysql_pconnect SOLVES a problem. It
it can create more. It can slow down the entire system during normal
operation while providing only a marginal increase in performance during
peak times.

Have you ever done performance tests to see how much time
mysql_pconnect( ) actually saves? It's not much.

He would be much better off restructuring his application to provide
better performance.
Regards,
Erwin Moller

>>I have yet to see where mysql_pconnect SOLVES a problem. Normally it
creates MORE. Yes, it's theoretically possible for it to help on very
busy systems where connections are used for a very short time. But
that's not the case here.


--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5758
by: Jason_Schaitel | last post by:
I have an application that segregates data into two different databases. Database A has stored procs that perform joins between tables in database A and database B. I am thinking that I have reached the limits of Application Roles, but correct me if I am wrong. My application creates a connection to database A as 'testuser' with read only access, then executes sp_setapprole to gain read write permissions. Even then the only way 'testuser'...
43
2849
by: Davey | last post by:
I am planning on developing an application which will involve skills that I have very little experience of - therefore I would appreciate comments on my initial design thoughts. Overview on system: I'm not going to divulge the exact aims of the application but the purpose of it is to allow multiple client applications to retrieve data from a database (on a db server) and feed this data into another Windows
9
23083
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated to a pool. If I assign only one application to a applicaton pool and have multiple worker processes assigned to that pool. Will my application be processed by many worker processes?
6
8578
by: B B | last post by:
Okay, here is what's happening: I have a reasonably fast laptop (1.4 GHz Mobile M, so comparable to 2.5GHz P4) doing .net development. Running Windows XP pro, SP2 IIS is installed and running fine All SQL Servers I am referring to share a small (10 computers or so) LAN with a 100MB Switch. No other computers on the LAN exhibit this problem.
9
2773
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
7
2837
by: Venkat Venkataramanan | last post by:
Hello: I have a UI layer that interacts with the business layer which interacts with the DB layer. I have developed the business layer as a class library that complies to a DLL that will be stored on the server. So, every client application that is launched will have to run an instance of the DLL that will run in the
20
6639
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are logging onto the web server using LDAP for authentication, do most people 1) have the web server connecting to the database using its own user account (possibly through ident), and controlling access to different database entities strictly through...
5
7864
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based setting is of course stored in the app.exe.config file located in the same directory as the exe. Upon closing the form, I save the user setting which then creates a user.config file in the appdata directory in my profile. This is all well and good....
3
1443
by: GaryDean | last post by:
I have just been through the docs on the Data Access Application blocks and it seems that they complicate things more than make things simple. To me it seems that there is nothing more simple and straight forward than writing simple stored procedures and executing them from .net code using easy to understand connection strings. I'm looking for opinions here from those that have used these tools. Am I missing something? --
2
1137
by: user | last post by:
Hello, I simply make some aspx web page to update data in a mdb access database..But when this database is open (not table, juste the mdb in access) with MS Access, i have an error with my asp.net application (that tell that the database is either open) Is there a way to make no-exclusive connection to this database ?? Thanks a lot !!!
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.