Discussion:
[bottlepy] connecting to MySQL using a hook
Jim Gregory
2016-12-28 16:05:41 UTC
Permalink
I've written a module to manage a connection to a MySQL database. I would
like to make a connection to the database on each request and disconnect
after the request. I've tried doing this:

from modules.database import myMySQL

db = myMySQL('bikesatw_accounting')

@hook('before_request')
def _connect_db():
db = myMySQL('bikesatw_accounting')

@hook('after_request')
def _close_db():
if db.conn.open:
db.close()



This will open the database on the first request, but on subsequent
requests I receive the following error:

File
"/home/me/Projects/local/lib/python2.7/site-packages/pymysql/cursors.py",
line 71, in _get_db
raise err.ProgrammingError("Cursor closed")
ProgrammingError: Cursor closed

I can do this using hooks? I'm aware of the Bottle-MySQL plugin, but I'd
prefer to use my own module.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marcel Hellkamp
2016-12-29 14:13:30 UTC
Permalink
Hi there,

in _connect_db() you are assigning a new local variable instead of
re-assigning the global one. The global 'db' variable never changes.

But thee is another problem: Most web applications run in a threaded web
server, which means that your handler functions may be executed multiple
times in differed threads at the same time. That does not mix well with
global variables. Have a look at threading.local from the standard
library, or invest the time and write a small plugin (instead of using
hooks) to create a separate db connection for each request.

Here is an example plugin you could adapt:

https://bottlepy.org/docs/dev/plugindev.html#plugin-example-sqliteplugin
Post by Jim Gregory
I've written a module to manage a connection to a MySQL database. I
would like to make a connection to the database on each request and
|
frommodules.database importmyMySQL
db =myMySQL('bikesatw_accounting')
@hook('before_request')
db =myMySQL('bikesatw_accounting')
@hook('after_request')
db.close()
|
This will open the database on the first request, but on subsequent
|
File"/home/me/Projects/local/lib/python2.7/site-packages/pymysql/cursors.py",line
71,in_get_db
raiseerr.ProgrammingError("Cursor closed")
ProgrammingError:Cursorclosed
|
I can do this using hooks? I'm aware of the Bottle-MySQL plugin, but
I'd prefer to use my own module.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...