Permalink
Browse files

Significant duh - remember the threads! Sharing one db conn between m…

…any threads leads to melt down. For now create new connection on each request and try, before looking at connection pooling
  • Loading branch information...
1 parent 1dbed31 commit 15872323f3b4f4db4a7db20f7b3aed46785fe610 @harryf committed Feb 25, 2009
Showing with 63 additions and 28 deletions.
  1. +55 −22 dammit/db_mysql.py
  2. +8 −6 dammit/uri.py
View
@@ -63,10 +63,19 @@ class MySQL(object):
>>> None == m.load(u2.id)
True
"""
- def __init__(self, config = None, dropfirst = False):
+ def __init__(self, config = None, dropfirst = False, bootstrap = True):
self.config = self._default_config(config)
- self.db = self._connect()
- self.bootstrap(dropfirst)
+ if bootstrap:
+ self.db = self._connect()
+ self.bootstrap(dropfirst)
+ else:
+ self.db = self._connect(usedb = True)
+
+ def fresh_connection(self):
+ """
+ Make sure we have a fresh connection for each request
+ """
+ return MySQL(config = self.config, bootstrap = False)
@db_cache.load
@reconnect
@@ -263,25 +272,49 @@ def _default_config(self, config):
return config
- def _connect(self):
- try:
- # This will fail on MySQL < 4.1
- db = MySQLdb.connect(
- host = self.config['db_host'],
- user = self.config['db_user'],
- passwd = self.config['db_pass'],
- use_unicode=1,
- connect_timeout = 5,
- init_command="set names utf8"
- )
- except MySQLdb.OperationalError:
- db = MySQLdb.connect(
- host = self.config['db_host'],
- user = self.config['db_user'],
- passwd = self.config['db_pass'],
- connect_timeout = 5,
- use_unicode=1
- )
+ def _connect(self, usedb = False):
+ if usedb:
+ try:
+ # This will fail on MySQL < 4.1
+ db = MySQLdb.connect(
+ host = self.config['db_host'],
+ user = self.config['db_user'],
+ passwd = self.config['db_pass'],
+ db = self.config['db_name'],
+ use_unicode=1,
+ connect_timeout = 5,
+ init_command="set names utf8"
+ )
+ except MySQLdb.OperationalError:
+ db = MySQLdb.connect(
+ host = self.config['db_host'],
+ user = self.config['db_user'],
+ passwd = self.config['db_pass'],
+ db = self.config['db_name'],
+ connect_timeout = 5,
+ use_unicode=1
+ )
+ else:
+ # When bootstrapping, we may not yet have a
+ # DB to use...
+ try:
+ # This will fail on MySQL < 4.1
+ db = MySQLdb.connect(
+ host = self.config['db_host'],
+ user = self.config['db_user'],
+ passwd = self.config['db_pass'],
+ use_unicode=1,
+ connect_timeout = 5,
+ init_command="set names utf8"
+ )
+ except MySQLdb.OperationalError:
+ db = MySQLdb.connect(
+ host = self.config['db_host'],
+ user = self.config['db_user'],
+ passwd = self.config['db_pass'],
+ connect_timeout = 5,
+ use_unicode=1
+ )
db.charset = 'utf8'
return db
View
@@ -604,7 +604,7 @@ def load(self, id):
>>> u2.uri == "http://local.ch/test.html"
True
"""
- return self.db.load(id)
+ return self.db.fresh_connection().load(id)
def register(self, uri, status = 200, **kwargs):
"""
@@ -667,8 +667,9 @@ def register(self, uri, status = 200, **kwargs):
True
"""
id = URI.hash(uri)
+ db = self.db.fresh_connection()
- u = self.db.load(id)
+ u = db.load(id)
newrecord = True
if u:
@@ -690,7 +691,8 @@ def apply_attribute(uri, key, value):
setattr(uri, key, value)
updated = True
except Exception, e:
- logging.error("Error applying key '%s' for '%s': %s", key, uri.uri, e)
+ if not status == 200 and key == 'location':
+ logging.error("Error applying key '%s' for '%s': %s", key, uri.uri, e)
return updated
@@ -714,11 +716,11 @@ def apply_attribute(uri, key, value):
if newrecord:
u.created = now
u.updated = now
- self.db.insert(u)
+ db.insert(u)
else:
if updated:
u.updated = now
- self.db.update(u)
+ db.update(u)
return u
@@ -734,7 +736,7 @@ def delete(self, id):
>>> print um.load(u.id)
None
"""
- self.db.delete(id)
+ self.db.fresh_connection().delete(id)
def _test():
import doctest

0 comments on commit 1587232

Please sign in to comment.