DB-Connections Class as a Singleton in Python
Normally, you have some kind of object representing the thing that uses a database (e.g., an instance of MyWebServer ), and you make the database connection a member of that object.
If you instead have all your logic inside some kind of function, make the connection local to that function. (This isn’t too common in many other languages, but in Python, there are often good ways to wrap up multi-stage stateful work in a single generator function.)
If you have all the database stuff spread out all over the place, then just use a global variable instead of a singleton. Yes, globals are bad, but singletons are just as bad, and more complicated. There are a few cases where they’re useful, but very rare. (That’s not necessarily true for other languages, but it is for Python.) And the way to get rid of the global is to rethink you design. There’s a good chance you’re effectively using a module as a (singleton) object, and if you think it through, you can probably come up with a good class or function to wrap it up in.
Obviously just moving all of your globals into class attributes and @classmethod s is just giving you globals under a different namespace. But moving them into instance attributes and methods is a different story. That gives you an object you can pass around—and, if necessary, an object you can have 2 of (or maybe even 0 under some circumstances), attach a lock to, serialize, etc.
In many types of applications, you’re still going to end up with a single instance of something—every Qt GUI app has exactly one MyQApplication , nearly every web server has exactly one MyWebServer , etc. No matter what you call it, that’s effectively a singleton or global. And if you want to, you can just move everything into attributes of that god object.
But just because you can do so doesn’t mean you should. You’ve still got function parameters, local variables, globals in each module, other (non-megalithic) classes with their own instance attributes, etc., and you should use whatever is appropriate for each value.
For example, say your MyWebServer creates a new ClientConnection instance for each new client that connects to you. You could make the connections write MyWebServer.instance.db.execute whenever they want to execute a SQL query… but you could also just pass self.db to the ClientConnection constructor, and each connection then just does self.db.execute . So, which one is better? Well, if you do it the latter way, it makes your code a lot easier to extend and refactor. If you want to load-balance across 4 databases, you only need to change code in one place (where the MyWebServer initializes each ClientConnection ) instead of 100 (every time the ClientConnection accesses the database). If you want to convert your monolithic web app into a WSGI container, you don’t have to change any of the ClientConnection code except maybe the constructor. And so on.
If you’re using an object oriented approach, then abamet’s suggestion of attaching the database connection parameters as class attributes makes sense to me. The class can then establish a single database connection which all methods of the class refer to as self.db_connection , for example.
If you’re not using an object oriented approach, a separate database connection module can provide a functional-style equivalent. Devote a module to establishing a database connection, and simply import that module everywhere you want to use it. Your code can then refer to the connection as db.connection , for example. Since modules are effectively singletons, and the module code is only run on the first import, you will be re-using the same database connection each time.
Jeffrey Froman 5643
Related Query
- Python singleton class
- Python decorator class with function counter variable
- Slighly alter method by passing function to class constructor in Python
- Boost Python to_python_converter for user defined python class in extension module
- using python class variables to manage global lists (pools)
- cython cdef class c method: how to call it from another cython cdef class without python overhead?
- Instantiating a Class in Interactive Python Shell
- Calling a method from a parent class in Python
- Python change class functions
- Python 3.2, Tkinter class declarations, class undefined when subclassing
- Howto create Python Pyramid view class without need to specify ‘name’ for each method
- Is there a better way to explore the Python Standard Library with a proper Class Browser in PyDev/Eclipse?
- Python / C-Api: Add class to module
- Parallel Python — create objects into another class in parallel execution
- What would be the proper naming convention for a Python module, class and instance variable having the same base name?
- Boost Python wrapper for a c++ class that uses Opencv 2.3
- Count number of classes and class hierarchies in a Python package
- Python Calls logging. Getting the base class name
- Sqlalchemy: get python class from sqlalchemy.schema.MetaData
- How to tell python non-class objects from class objects
- Python — Are class methods multiprocess safe?
- Python properties also as class properties
- Jython: Access singleton Java class (static)
- Multiple database connections with Python + Pylons + SQLAlchemy
- Prevent instantiation of an abstract base class in Python
- Can a python class have multiple grandparents?
- Python recursively adding to a class variable gives different results
- Creating an automatic delegating wrapper class in Python 3
- mysql connector python fails with «3159 (HY000): Connections using insecure transport are prohibited while —require_secure_transport=ON.»
- Multiple different roles to a python class
- How to manage an object from a class defined in python using the C-Python API
- Unresolved reference to a class in Python
- Using Python decorators to gather information on available method and store that data in the class or instance
- How to properly implement helper functions in a class in python
- Python Web Scraping same class
- How do I mock a function with no parameters that sets a class attribute for Python unit tests?
- Define a class with another class object in Python
- Java class HiveDriver not found when conecting to database Hive with Anaconda and Python 3.8 64bits
- Python Multithreading in a class method
- Python add attribute of property to instance of class
- How to represent free functions in a Class Diagram Python
- How to click a button class using selenium in Python
- use of access modifier like underscore in python module control how variable defined or module, function, class imported should be accessed?
- Python — Init class reflectively
- Python super and setting parent class property from subclasses
- How to enforce that subclass of abstract class should define a particular inner class in Python
- Python Generic class of Matrix, I receive Cannot instantiate typing.TypeVar error
- python — how to reference Class declared inside a function
- Using merge sort/quick sort to sort attribute of class objects in Python
- Using Gtk.Window defined in Glade for a class derived from Gtk.Window with Python GTK
More Query from same tag
- re.findall failing for regex with grouping in Python
- Image processing and neural networks approach
- How to make a shape change color when I click on it in a Connect Four board?
- How to update json file with python -c flag?
- python: how to cast a list to c_byte array
- Step function analysis with python
- Python getting the part after div class
- CSS aware intelligent html parser for python
- Slice string till next char
- Using Idle with Imap_tools and Yahoo
- Get the pairs of values from a list according to a condition without elements repeating
- Pip3 — Python 3.8.9 — mac M1 — No import is working properly from my scripts
- Comparing Value of one list inside Gigantic Two Dimen list in python, Fastest way?
- Parsing xml with etree
- Python tkinter time.sleep()
- Scrapy — TypeError: ‘Request’ object is not iterable
- Mocking file reads using patch decorator and side_effect
- Python for loop with a tuple in it
- how do i get similar lines out of two files?
- How to grab first ip address from a string
- Modify border color of a Qgroupbox without modify borders of widgets inside it in PyQt5
- raindrop going right except going down
- change scaling for the schrodinger equation
- python : use optparse on given list rather than on system command line arguments
- pypdf not extracting tables from pdf
- Access variable declared in a function from another function
- Find out multiple definitions of a field in a class
- Is there any good python css inlining module for html in the wild?
- list comprehension using regex conditional
- How do I use «:» in XML element names using lxml?
- Matplotlib scatter 3d colors
- What does eval(dir()[0]) do in python
- How can I make a Tkinter GUI that auto fills certain entries
- Seaborn Reg Plots with the same Y Axis scale (Side by Side)
- Can you change the indexing in python?
How to do singleton database connection in python
Amazon DynamoDB(Beta) – is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.
And I use boto , its a Python package that provides interfaces to Amazon Web Services
class DbConnection create a singleton database connection and DynomoDbOperations does the basic dynamodb operations.
following is a code sample , to make a singleton connection with dynamodb
#make sure boto installed
from boto import connect_dynamodb
from boto.exception import BotoClientError,DynamoDBResponseError
class DbConnection(object):
_instance = None
# inner class
class Singleton:
def __init__(self):
# singleton variable
#configure boto or supply necessary parameters with connect_dynamodb
self.connection = connect_dynamodb()
# constructor of DbConnection
def __init__( self):
# check whether instace is already created or not
if DbConnection._instance is None:
DbConnection._instance = DbConnection.Singleton()
self._EventHandler_instance = DbConnection._instance
def __getattr__(self, aAttr):
return getattr(self._instance, aAttr)
def __setattr__(self, aAttr, aValue):
return setattr(self._instance, aAttr, aValue)
# Follwing class does basic dynamodb operations get,put,update,delete
class DynamoDbOperations():
TABLE_NAME = ‘tbltest’
# method getting connection with DynamoDb
def get_dynamodb_connection(self):
self.dynamodb_connection = DbConnection().connection
# printing the connection object for testing
print self.dynamodb_connection
return self.dynamodb_connection
except BotoClientError as exe:
def get_dynamodb_table(self):
return self.get_dynamodb_connection().get_table(self.TABLE_NAME)
except DynamoDBResponseError as exe:
# method put values to dynamodb
def put_dynanodb(self , attr_dict):
self.get_dynamodb_table().new_item(hash_key = None ,range_key = None,attrs = attr_dict ).put()
except BotoClientError as exe:
# method getting values from Dynamodb using the unique key
def get_dynamodb(self, key):
return self.get_dynamodb_table().get_item(key)
except BotoClientError as exe:
# method updating DynamoDb
def update_dynamodb(self , key , additional_items_dict):
item = self.get_dynamodb(key)
for key in additional_items_dict.iterkeys():
itemPython singleton database connection = additional_items_dictPython singleton database connection
item.put()
except BotoClientError as exe:
# metod delete from DynamoDb
def delete_from_dynamodb(self, key):
self.get_dynamodb(key).delete()
except BotoClientError as exe:
Test it by cerating two different objects of DynamoDbOperations and calling the method get_dynamodb, both object make use of the same database connection ,we can verify it by examining the objects printed in the method get_dynamodb_connection ,both points to the same address location.
print DynamoDbOperations().get_dynamodb(‘abc’)
print DynamoDbOperations().get_dynamodb(‘def’)