Skip to content

Commit

Permalink
Attach SQLAlchemy listeners to objects, not classes
Browse files Browse the repository at this point in the history
Otherwse listeners get duplicated when running tests.

Fixes #2267.
  • Loading branch information
djmitche committed Apr 1, 2012
1 parent dd4db71 commit dd5201c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 10 additions & 6 deletions master/buildbot/db/enginestrategy.py
Expand Up @@ -27,7 +27,7 @@
import sqlalchemy as sa
from twisted.python import log
from sqlalchemy.engine import strategies, url
from sqlalchemy.pool import NullPool, Pool
from sqlalchemy.pool import NullPool
from buildbot.util import sautils

# from http://www.mail-archive.com/sqlalchemy@googlegroups.com/msg15079.html
Expand Down Expand Up @@ -91,7 +91,7 @@ class CheckpointFullfsyncDisabler(object):
disabler.connect = connect_listener
engine.pool.add_listener(disabler)
else:
sa.event.listen(Pool, 'connect', connect_listener)
sa.event.listen(engine.pool, 'connect', connect_listener)

log.msg("setting database journal mode to 'wal'")
try:
Expand Down Expand Up @@ -127,6 +127,10 @@ def special_case_mysql(self, u, kwargs):
else:
u.query['charset'] = 'utf8'

return u, kwargs, None

def set_up_mysql_engine(self, u, engine):
"""Special setup for mysql engines"""
# add the reconnecting PoolListener that will detect a
# disconnected connection and automatically start a new
# one. This provides a measure of additional safety over
Expand All @@ -149,11 +153,9 @@ class ReconnectingListener(object):
pass
rcl = ReconnectingListener()
rcl.checkout = checkout_listener
kwargs['listeners'] = [ rcl ]
engine.pool.add_listener(rcl)
else:
sa.event.listen(Pool, 'checkout', checkout_listener)

return u, kwargs, None
sa.event.listen(engine.pool, 'checkout', checkout_listener)

def create(self, name_or_url, **kwargs):
if 'basedir' not in kwargs:
Expand Down Expand Up @@ -188,6 +190,8 @@ def create(self, name_or_url, **kwargs):

if u.drivername.startswith('sqlite'):
self.set_up_sqlite_engine(u, engine)
elif u.drivername.startswith('mysql'):
self.set_up_mysql_engine(u, engine)

return engine

Expand Down
3 changes: 0 additions & 3 deletions master/buildbot/test/unit/test_db_enginestrategy.py
Expand Up @@ -17,7 +17,6 @@
from twisted.python import runtime
from sqlalchemy.engine import url
from sqlalchemy.pool import NullPool
import sqlalchemy as sa
from buildbot.db import enginestrategy

class BuildbotEngineStrategy_special_cases(unittest.TestCase):
Expand All @@ -27,8 +26,6 @@ class BuildbotEngineStrategy_special_cases(unittest.TestCase):
mysql_kwargs = dict(basedir='my-base-dir',
connect_args=dict(init_command='SET storage_engine=MyISAM'),
pool_recycle=3600)
if hasattr(sa, '__version__') and sa.__version__.startswith('0.6'):
mysql_kwargs['listeners'] = [ 'ReconnectingListener' ]
sqlite_kwargs = dict(basedir='/my-base-dir', poolclass=NullPool)

def setUp(self):
Expand Down

0 comments on commit dd5201c

Please sign in to comment.