Synchronization

Discussion in 'PHP' started by r_honey, Jun 23, 2007.

  1. #1
    My Hosting provider puts a limit on the maximum no. of simultanoeus DB connections. So, I am providing a wrapper to my DB layer through which all modules request new DB connections to keep a check on the permissible maximum...

    I need to make sure that only one script, page or thread (whatever you call it) is executing inside the Request connection method of the wrapper.

    So, is there any built-in support for synchronization in PHP like there is synchronized construct in Java...
     
    r_honey, Jun 23, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    nope: php doesn't support threading, and therefore no synchronization.

    By default when you run a php script that connects to a database, and you connect several times to the database (using mysql_connect), only 1 connection will be made, and it will reuse the connection over and over again when you reconnect.

    The "persistent" flag of the mysql_connect function determines if php should create a new connection, or re-use the existing one. By Default this flag is set to false, and the existing connection will be re-used.
     
    UnrealEd, Jun 23, 2007 IP
  3. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's strange...
    Do you mean that when I open a connection in a script, then close it & then again connect to the DB, PHP will not close the connection, but it will close it only when the script terminates.
    That means, disconnecting inside a script will have no effect if we reconnect later.

    So, if I change the connection parameters later, say DB, will PHP perform a reconnection, or it will still use the existing connection, performing only select_DB()???

    I believed that when persistent flag if false, the connection will terminate. Only when it is set to true will the connection be reused. You are telling me exactly the opposite. Are you sure???
     
    r_honey, Jun 23, 2007 IP
  4. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not sure 100% if you explicitly close the connection with mysql_close, but if you don't php will always attempt to use the same connection. This is what i found on php.net:
    so only non-persistent connections will be closed.

    if you only change the db name, you will only have to call mysql_select_db, and therefore not reconnect to the server. So yes, it will use the existing connection. If you change the host, username or password, then it will have to reconnect of course.

    You can use mysql_pconnect though, which will always attempt to return the exisiting connection.
     
    UnrealEd, Jun 24, 2007 IP
  5. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have also gone through all above on php.net.

    Finally,
    obviously, if an opened link with same parameters is available, it will be reused. But my intuition says, that if the previous identical link has already been closed, then it will not be recycled...
     
    r_honey, Jun 24, 2007 IP