1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is it OK to Connect To Multiple MySql Databases?

Discussion in 'MySQL' started by SeoVeteran33, Jun 25, 2008.

  1. #1
    I'm currently running 2 different scripts for one site. A video script and a link dump script. When a page loads for the first time (and only the first time), I do the following:
    SEMrush
    1.) I close out the connection to the video DB
    2.) I connect to the link dump DB and record "hits in" if appropriate
    3.) I then close out the connection to the link dump DB
    4.) I reconnect to the video DB

    Is this ok? I don't want to hit a concurrent connections limit or anything. Also, do you think it's overkill to close out the video DB and reconnect each time? Any help would be greatly appreciated.
     
    SeoVeteran33, Jun 25, 2008 IP
    SEMrush
  2. mwasif

    mwasif Active Member

    Messages:
    818
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    If your page does not take much time to load, then it is ok to keep the connections open.
     
    mwasif, Jun 25, 2008 IP
    SeoVeteran33 likes this.
  3. jestep

    jestep Prominent Member Premium Member

    Messages:
    3,661
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Unless the connection took a long time, I would not close out the first connection.

    I would do this:
    Open 1 and 2;
    //do stuff
    Close 2;
    Close 1;
     
    jestep, Jun 25, 2008 IP
    SeoVeteran33 likes this.
  4. Social.Network

    Social.Network Member

    Messages:
    519
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #4
    I agree with step, but want to add a note. Acquire your resources late, consume as needed, and close resource. If you need to connect to multiple MySQL databases that is fine, no problem.
     
    Social.Network, Jun 26, 2008 IP
    SeoVeteran33 likes this.
  5. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #5
    If the page load is still fast leave them open.

    When the document ends and the page is shown the connections are closed anyways.

    Only reopened when a new page is opened calling the connection again.

    php automatically closes the DB at the end of a document.

    TJ
     
    lowridertj, Jun 27, 2008 IP
    SeoVeteran33 likes this.
  6. Social.Network

    Social.Network Member

    Messages:
    519
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #6
    I explicitly close connections. Leaving connections open and relying on the end of script to close your database connections is bad practice. Never leave connections open or objects hanging around consuming resources. You can pool database connections as needed to minimize the overhead of opening a new connection.
     
    Social.Network, Jun 27, 2008 IP
  7. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #7
    I was making ref. to small scripts that do not take much time to load.

    yes its in good practice to close all connections at the end of running a script. However by default they are closed upon ending the script execution.

    TJ
     
    lowridertj, Jun 27, 2008 IP
  8. SeoVeteran33

    SeoVeteran33 Well-Known Member

    Messages:
    390
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    108
    #8
    Thanks guys for your advice on this. The site is up and everything is running smoothly.
     
    SeoVeteran33, Jul 2, 2008 IP