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 6928009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:07:42+00:00 2026-05-27T11:07:42+00:00

I have gone similar threads like Django persistent database connection and other stuff on

  • 0

I have gone similar threads like Django persistent database connection and other stuff on same topic.
However Django doesn’t officially support persistent connections to MySQL and Mongo(to my limited knowledge).So i tried avoiding a lot of stuff and tried to make it simple.So what i did was in my views.py made global connection variables for both MongoDB and MySQL,something like:

from pymongo import Connection
import MySQLdb
global mongo_connection,mongo_db,collection,mysql_connection,mysql_cursor
mysql_connection = MySQLdb.connect (host = "localhost",
                       user = "root",
                       passwd = "password",
                       db = "demo")
mysql_cursor = mysql_connection.cursor ()
mongo_connection = Connection()
mongo_db = mongo_connection.test_database
collection = mongo_db.test_collection

So after this when the required view is called as per URL requested,i dump the data in the two databases. Like:

mysql_cursor.execute('''INSERT INTO           
table_name(l,n_n,n_id,s_n,s_id,u,r) VALUES
(%s,%s,%s,%s,%s,%s,%s)''',
(l,n_n,n_id,s_name,s_id,u,re)
)   

And similarly i did for saving to MongoDB.

Obviously there’s this flaw in this approach that i am not closing the connection anywhere.But this approach does seem to work and work well.

Why is this sort of approach not used?

How can i measure the performance improvements i get by using this approach v/s letting Django create a new connection to DB on each call.

Also a batch insert is supposed to make things even better,by reducing calls to DB.How can such a concept be implemented within view definition?

Here is how my application behaved before i had used my method of trying to make a persistent connection and had let Django take care of it

mysql> show status like '%onn%';
+--------------------------+--------+
| Variable_name            | Value  |
+--------------------------+--------+
| Aborted_connects         | 0      |
| Connections              | 164359 |
| Max_used_connections     | 3      |
| Ssl_client_connects      | 0      |
| Ssl_connect_renegotiates | 0      |
| Ssl_finished_connects    | 0      |
| Threads_connected        | 1      |
+--------------------------+--------+
7 rows in set (0.00 sec)

After a few seconds when i ran the same query i got:

mysql> show status like '%onn%';
+--------------------------+--------+  
| Variable_name            | Value  |
+--------------------------+--------+
| Aborted_connects         | 0      |
| Connections              | 175047 |
| Max_used_connections     | 3      |
| Ssl_client_connects      | 0      |
| Ssl_connect_renegotiates | 0      |
| Ssl_finished_connects    | 0      |
| Threads_connected        | 1      |
+--------------------------+--------+
7 rows in set (0.00 sec)

http://dev.mysql.com/doc/refman/5.5/en/server-status-variables.html#statvar_Connections states that Connection:The number of connection attempts (successful or not) to the MySQL server.So is it due to some problem in which i handle saving a row using Django ORM for MySQL or is this sort of number of connections expected?

However after using my approach the number of connections didn’t increase.

SOLVED

Got confused by reading the definition about Connections.So finally figured it out by doing a test.
Inserted 19 records into DB,Connections increased by the same number.So i believe it means the number of times DB has been contacted.So in that case using Django’s inbuilt stuff is the best way to go.

http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html tells that maximum number of connections is 151,so anyways it was a misinterpretation on my part.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T11:07:43+00:00Added an answer on May 27, 2026 at 11:07 am

    If you want to get into such a technical topic, have a look at the django.db package and review how Django’s database backends and QuerySet instances interact with each other before you make any fallacious assumptions, suh as that Django opens a new database connection every time it interacts with a database. You’ll see that the database layer performs everything you’ve mentioned here, without allowing the developer to improperly manage database resources, like obtaining a cursor within a loop and forgetting to close it and creating memory leaks.

    So it’s not that the approach isn’t present, it’s that it’s managed and abstracted from the work you perform, as it should be, for reasons mentioned above, with the added benefit that you don’t need to, say, directly incorporate database management within a view.

    Having said that, for unsupported databases, such as MongoDB and any other non-relational database, you’ll have to assume that you do not have Django’s database layer managing your database resources and how you interact with them and diligently asses what are your obligations as the developer under the conditions of interfacing with their database APIs. When you see obvious patterns emerging you will come up with a solution suitable to be applied within the context of view components.

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

Sidebar

Related Questions

http://jsfiddle.net/cwUcM/ I've gone through all of the other threads that seems to have a
I have gone through all the msdn stuff for getting started with the SDK
I have gone through most similar JQuery posts relating to hiding parent containers but
I have gone through similar questions on Stackoverflow but still can't get a good
First let me say, I have gone thoroughly through all other solutions to this
Have gone through hibernate api specification on FlushMode but didn't get the exact difference.
I have gone through their developer guide but haven't been able to find a
I have gone through different questions/articles on Message Brokers and ESBs(Even on stackoverflow). Still
I have gone through following question. Convert NSString to NSDictionary It is something different
I have gone through this and this , but the question I am asking

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.