There's a Java paradigm for database access implemented within the Java DataSource. This object produce a helpful abstraction around the development of database connections. The DataSource object keeps database configuration, and can only create database connections on request. This really is enables you to definitely keep all database configuration and initialization code in one location, and causes it to be simple to change database implementation, or make use of a mock database for testing.

I presently focusing on a Python project which utilizes cx_Oracle. In cx_Oracle, one will get an association from the module:

import cx_Oracle as dbapi
connection = dbapi.connect(connection_string)
# At this point I am assuming that a real connection has been made to the database.
# Is this true?

I'm looking for a parallel towards the DataSource in cx_Oracle. I'm able to easily create this by developing a new class and wrapping cx_Oracle, but I'm wondering if this sounds like the proper way to get it done in Python.

You will find relevant information of methods to gain access to databases in Python by searching at PEP-249: Python Database API Specification v2.0. cx_Oracle adjusts for this specs, just like many database motorists for Python.

Within this specs a Connection object signifies a database connection, but there's no built-in pooling. Tools for example SQLAlchemy do provide pooling facilities, and although SQLAlchemy is frequently charged being an ORM, it doesn't need to be used as a result while offering nice abstractions to be used on the top of SQL engines.

Should you choose wish to accomplish object-relational-mapping, then SQLAlchemy does the company, and you will consider either its very own declarative syntax or any other layer for example Elixir which sits on the top of SQLAlchemy and offers elevated simplicity of use for additional common use cases.

I do not think there's a "right" method of doing this in Python, except maybe to visit a step further and employ another layer between yourself and also the database.

With respect to the reason behind attempting to make use of the DataSource concept (which I have only ever run into in Java), SQLAlchemy (or something like that similar) might solve the issues for you personally, without you needing to write something on your own.

In the event that does not suit you perfectly, writing your personal wrapper seems like an acceptable solution.

Yes, Python includes a similar abstraction.

This really is from your local build regression test, where we ensure that we are able to speak with our databases once we develop a new python.

if database == SYBASE:
    import Sybase
    conn = Sybase.connect('sybasetestdb','mh','secret')
elif database == POSTRESQL:
    import pgdb
    conn = pgdb.connect('pgtestdb:mh:secret')
elif database == ORACLE:
    import cx_Oracle
    conn = cx_Oracle.connect("mh/secret@oracletestdb")

curs.execute('select a,b from testtable')
for row in curs.fetchall():
    print row

(note, this is actually the simple version, within our multidb-aware code there exists a dbconnection class which has this logic inside.)

I simply drawn up and authored my very own. It permitted me to include such things as abstracting the database (Oracle/MySQL/Access/etc), adding logging, error handling with transaction rollbacks, etc.