Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 80263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:16:30+00:00 2026-05-10T21:16:30+00:00

I use lazy connection to connect to my DB within my DB object. This

  • 0

I use lazy connection to connect to my DB within my DB object. This basically means that it doesn’t call mysql_connect() until the first query is handed to it, and it subsequently skips reconnecting from then on after.

Now I have a method in my DB class called disconnectFromDB() which pretty much calls mysql_close() and sets $_connected = FALSE (so the query() method will know to connect to the DB again). Should this be called after every query (as a private function) or externally via the object… because I was thinking something like (code is an example only)

$students = $db->query('SELECT id FROM students');  $teachers = $db->query('SELECT id FROM teachers'); 

Now if it was closing after every query, would this slow it down a lot as opposed to me just adding this line to the end

$db->disconnectFromDB(); 

Or should I just include that line above at the very end of the page?

What advantages/disadvantages do either have? What has worked best in your situation? Is there anything really wrong with forgetting to close the mySQL connection, besides a small loss of performance?

Appreciate taking your time to answer.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:16:31+00:00Added an answer on May 10, 2026 at 9:16 pm

    As far as I know, unless you are using persistent connections, your MySQL connection will be closed at the end of the page execution.

    Therefore, you calling disconnect will add nothing and because you do the lazy connection, may cause a second connection to be created if you or another developer makes a mistake and disconnects at the wrong time.

    Given that, I would just allow my connection to close automatically for me. Your pages should be executing quickly, therefore holding the connection for that small amount of time shouldn’t cause any problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this tool called Lazy C++ which breaks a single C++ .lzz file
I want to lazy load of @Lob properties. First ,i use javassist to instrument
i have an spring web application (on glassfish server) that use JDBC connection pool.
In JavaScript, you can use Lazy Function Definitions to optimize the 2nd - Nth
In my APP. Need to use Lazy Loading, so I have to handler the
In our .Net application, some of our business objects use lazy loading to access
I use Fedor's lazy load for gallery show several pictures with URL. The code
What is Lazy Loading? [Edit after reading a few answers] Why do people use
I have a mod_perl2 based web app that requires a connection to a mysql
Before reading please note that I've googled this and read a ton of articles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.