How many connections will open in mongodb in python? - python

I am working with mongodb and python, for accessing the database I am doing this:
#pymongo connection settings
from pymongo import Connection
connection = Connection('localhost', 27017)
db = connection['MyDB']
I am inserting documents like this:
db_data = db.mycollection.insert({'name': 'Mulagala', 'age': 24})
and finding like this:
db_data = db.mycollection.find()
When I am creating multiple users or getting mycollection details for multiple times how many conections will open in mongodb. Do I need to close any open connection before returning result?

No matter how many db.coll.find({...}) and db.coll.insert({...}) will you do, you will still have only one connection. You do not need to close open connection (at the end of the script it will be closed automatically)
P.S. MongoClient is the preferred method to connect to mongo (Connection is deprecated)

Related

Is there a limit on the number of connections opened from a client's side to a SQL Server database?

I'm working on a Python application with an SQL Server database using pyodbc, and I need to open multiple connections from the application's side to the database.
I learnt that the max number of connections allowed on an instance of the SQL Server database is 32,767. My understanding is this is the max that the DB instance "can handle", i.e. all simultaneous users combined.
Is there a limit on how many connections one client can open towards the same database instance, is it also 32,767? If yes, where / how is this limit configured?
Taking an educated guess here that there is no connection count limit on the client side towards the same DB instance, there is a limit of 32,767 on the server side, but the client would be more likely to run out of other resources way before it gets close to this figure.
I was using one connection, one cursor, and threading to insert multiple records, but kept getting a "connection is busy" error, this is resolved by adding "MARS_Connection=yes" in the pyodbc database connection string, thanks to this MS documentation.
Related:
How costly is opening and closing of a DB connection?
Can I use multiple cursors on one connection with pyodbc and MS SQL Server?

How to recycle mysql connection while using mysql.connector in python

I am working on some flask based api.And it is hosted in python anywhere free version[1]: http://Pythonanywhere.com.
I am using mysql.connector for establishing connection between mysql database and the code.The problem is the mysql database connection will be closed after 5 minutes. Then it will show a MYSQL connection not available error.how to get rid of this error?
I am establishing the connection like this
import mysql.connector
db_config={"host":"","user":"","password":"","database":"","raise_on_warnings":True}# "connection_timeout":86400}
mydb=mysql.connector.connect(**db_config)
In Pythonanywhere free version there will be a limited daily quota for Mysql connection. It seems like you are not closing your connection after use.So after some number of requests your quota will be exceeded. That is why it is showing mysql connection is not available. You should close the connection after use. Then you can reuse mysql connection within allowed quota. If you want more number of connection at a time you can consider upgrading pythonanywherepackage
For more information refer the below link,
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-close.html

Create cx_Oracle Python timeout on connect

I'm trying to get the Oracle inbuilt timeout attribute to work in Python.
Some relevant docs are here:
https://cx-oracle.readthedocs.io/en/latest/connection.html
import cx_Oracle
connection = cx_Oracle.connect("user/pass#thedb")
connection.callTimeout = 2000
cursor = connection.cursor()
The trouble is that the initial connection command is the one that is taking an excessive period to timeout (several minutes).
My question is, is there a way to somehow apply the callTimeout before the connection is made? or is there another way to do what I want?
I'm aware of this help:
Set database connection timeout in Python
It seems excessive to use threads for this.

should i open db connection for every thread?

I have developing kind of chat app.
There are python&postgresql in server side, and xcode, android(java) side are client side(Web will be next phase).
Server program is always runing on ubuntu linux. and I create thread for every client connection in server(server program developed by python). I didnt decide how should be db operations?.
Should i create general DB connection and i should use this
connection for every client's DB
operation(Insert,update,delete..etc). In that case If i create
general connection, I guess i got some lock issue in future. (When i try to get chat message list while other user inserting)
IF I create DB connection when each client connected to my server. In that case, Is there too many connection. and it gaves me performance issue in future.
If i create DB connection on before each db operation, then there is so much db connection open and close operation.
Whats your opinion? Whats the best way?
The best way would be to maintain a pool of database connections in the server side.
For each request, use the available connection from the pool to do database operations and release it back to the pool once you're done.
This way you will not be creating new db connections for each request, which would be a costly operation.

MySQLdb not working while trying to upload Raspberry Pi weather data to SQL database

I recently have been programming a program that uploads data from my Raspberry Pi weather station, with Python, to a PHPMyAdmin database. My website will then read the data in the database with PHP. I have the PHP section of the code working (will read data from the database), however my issue is my Python part of the program. I am using the MySQLdb plugin for Python. However, when I attempt to connect with the same details as PHP, it does seem to establish as a connection, as there is no error, however the rest of the code does not run.
How would I go about fixing this?
Here is my code:
import MySQLdb
db = MySQLdb.connect(
host = 'server169.web-hosting.com',
user = '***********',
passwd = '**************',
db = '************',
port = 3306 # should be same as in /etc/mysql/my.cnf on server.
)
cursor = db.cursor()
cursor.execute('SELECT VERSION()')
data = cursor.fetchone()
print str(data)
database.close()
host = 'server169.web-hosting.com',
This is a real host I think ? By default, MySQL (the DB you use) is disable on anything other than a localhost incoming connexion : you can't connect to the db from "outside" the server.
You PHP script works because it is run on the server so it can connect locally to the database.
Three ways to solve this :
connecting to you server with a SSH tunnelling raspberrypi:3306 to server169.web-hosting.com
allowing mysql to listen on server169.web-hosting.com:3306 (you'll need root access to do this, +change the firewall rules)
using an interface on the server that'll add the data for you (like a php script you call from you RaspberryPi with an HTTP POST request and a JSON object).
The last one is probably the best way for you, as you don't have to go too deep in configuration / need specials access on the server.

Categories