tinaheidinger
tinaheidinger

Reputation: 954

Python dataset in web application

I wrote a simple REST API with pybottle as web framework and dataset as database handler. I wonder about how dataset handles (opens and closes) connections to the database and how far I would need to take care about this.

Consider the following:

db = dataset.connect('sqlite:///database.db')
table = db['table']

def some_function():
    # do some manipulation (eg. insert) in 'table'

what happens when I call some_function() from the web application (which is in another file)? Does dataset open and close the connection to the database properly after the execution of the function? Or do I have a new open connection with every function call?

Upvotes: 1

Views: 187

Answers (1)

tuned
tuned

Reputation: 1125

Every SQL driver in Python follows the Python DB API PEP0249. As far as I see you are using a driver for SQLite so it should follow the same rules.

SQLAlchemy has a built-in way to establish a long lasting connection and avoid connection drop. You can create a method get_connection() that returns a Database object or restablishes connection if there is no active one.

Check the connect documentation and source code to see how the library wraps the communication with the database.

Upvotes: 1

Related Questions