Add a db-statement-timeout option for postgresql sources

Use that new option to set the statement_timeout option when creating connections, except for maintenance operations (shell and upgrade).

Closes #2547026

authorJulien Cristau <julien.cristau@logilab.fr>
changesete45bf9baa7b7
branchdefault
phasepublic
hiddenno
parent revision#927d035b6921 [formfields] Handle 'placeholder' attribute for text and textarea
child revision#cbb49851e00f [server/test] Add a test for db-statement-timeout option, #2614362be11e [view] Delete dead(?) code from ReloadableMixIn
files modified by this revision
server/sources/native.py
server/sqlutils.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1391522785 -3600
# Tue Feb 04 15:06:25 2014 +0100
# Node ID e45bf9baa7b73f8eb783da40caf79ac06ee3faef
# Parent 927d035b6921cab54b397f3556ce9a337a5546d7
Add a db-statement-timeout option for postgresql sources

Use that new option to set the statement_timeout option when creating
connections, except for maintenance operations (shell and upgrade).

Closes #2547026

diff --git a/server/sources/native.py b/server/sources/native.py
@@ -321,14 +321,20 @@
1            'default': '',
2            'help': 'set to "Trusted_Connection" if you are using SQLServer and '
3                    'want trusted authentication for the database connection',
4            'group': 'native-source', 'level': 2,
5            }),
6 +        ('db-statement-timeout',
7 +         {'type': 'int',
8 +          'default': 0,
9 +          'help': 'sql statement timeout, in milliseconds (postgres only)',
10 +          'group': 'native-source', 'level': 2,
11 +          }),
12      )
13 
14      def __init__(self, repo, source_config, *args, **kwargs):
15 -        SQLAdapterMixIn.__init__(self, source_config)
16 +        SQLAdapterMixIn.__init__(self, source_config, repairing=repo.config.repairing)
17          self.authentifiers = [LoginPasswordAuthentifier(self)]
18          if repo.config['allow-email-login']:
19              self.authentifiers.insert(0, EmailPasswordAuthentifier(self))
20          AbstractSource.__init__(self, repo, source_config, *args, **kwargs)
21          # sql generator
diff --git a/server/sqlutils.py b/server/sqlutils.py
@@ -297,11 +297,11 @@
22      """Mixin for SQL data sources, getting a connection from a configuration
23      dictionary and handling connection locking
24      """
25      cnx_wrap = ConnectionWrapper
26 
27 -    def __init__(self, source_config):
28 +    def __init__(self, source_config, repairing=False):
29          try:
30              self.dbdriver = source_config['db-driver'].lower()
31              dbname = source_config['db-name']
32          except KeyError:
33              raise ConfigurationError('missing some expected entries in sources file')
@@ -326,10 +326,18 @@
34          self._process_value = dbapi_module.process_value
35          self._dbencoding = dbencoding
36          if self.dbdriver == 'sqlite':
37              self.cnx_wrap = SqliteConnectionWrapper
38              self.dbhelper.dbname = abspath(self.dbhelper.dbname)
39 +        if not repairing:
40 +            statement_timeout = int(source_config.get('db-statement-timeout', 0))
41 +            if statement_timeout > 0:
42 +                def set_postgres_timeout(cnx):
43 +                    cnx.cursor().execute('SET statement_timeout to %d' % statement_timeout)
44 +                    cnx.commit()
45 +                postgres_hooks = SQL_CONNECT_HOOKS['postgres']
46 +                postgres_hooks.append(set_postgres_timeout)
47 
48      def wrapped_connection(self):
49          """open and return a connection to the database, wrapped into a class
50          handling reconnection and all
51          """