Python Forum
Connect and disconnect to database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect and disconnect to database
#1
I can't seem to find straight answer in the books I've got or from my DDG searches...

When should I connect and disconnect to my database?

Is it OK/acceptable to connect at the beginning of a method and disconnect at the end or should I connect and disconnect each time I want to do something with the database?

I'm just wondering what the norm is!

Thank you
Reply
#2
Connection is much more expensive than other DB operations. Unless you are somehow processing jobs from different DB entities (like switching users for some reason), I'd expect to connect at the beginning of the program and use that connection throughout.

Connections are the time when you process the DB server, the specific database, the DB user. If none of those things are changing, I wouldn't bother with a different connection.
Reply
#3
Oh wow... that certainly makes things easier! It will probably remove a good number of lines from my code too!

Thank you
Reply
#4
And it will be faster. One thing to be careful about - make sure all errors and exceptions are handled and that you close the database connection before the program exits.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Database Connect JohnnyCoffee 1 1,648 Apr-02-2020, 05:25 PM
Last Post: ndc85430
  Connect to database OVH Serferi 0 2,036 Apr-26-2019, 07:34 AM
Last Post: Serferi
  connect to remote database via python script sunstar20 5 5,364 Apr-23-2018, 11:05 AM
Last Post: Gribouillis
  Unable to connect oracle database using python code mayuresh 2 17,107 Jan-27-2018, 11:57 AM
Last Post: mayuresh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020