~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/session.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""Session Handling for SQLAlchemy backend."""
20
20
 
 
21
import re
21
22
import time
22
23
 
23
24
from sqlalchemy.exc import DisconnectionError, OperationalError
51
52
    return session
52
53
 
53
54
 
54
 
class SynchronousSwitchListener(sqlalchemy.interfaces.PoolListener):
55
 
 
 
55
def synchronous_switch_listener(dbapi_conn, connection_rec):
56
56
    """Switch sqlite connections to non-synchronous mode"""
57
 
 
58
 
    def connect(self, dbapi_con, con_record):
59
 
        dbapi_con.execute("PRAGMA synchronous = OFF")
60
 
 
61
 
 
62
 
class MySQLPingListener(object):
63
 
 
 
57
    dbapi_conn.execute("PRAGMA synchronous = OFF")
 
58
 
 
59
 
 
60
def ping_listener(dbapi_conn, connection_rec, connection_proxy):
64
61
    """
65
62
    Ensures that MySQL connections checked out of the
66
63
    pool are alive.
68
65
    Borrowed from:
69
66
    http://groups.google.com/group/sqlalchemy/msg/a4ce563d802c929f
70
67
    """
71
 
 
72
 
    def checkout(self, dbapi_con, con_record, con_proxy):
73
 
        try:
74
 
            dbapi_con.cursor().execute('select 1')
75
 
        except dbapi_con.OperationalError, ex:
76
 
            if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
77
 
                LOG.warn('Got mysql server has gone away: %s', ex)
78
 
                raise DisconnectionError("Database server went away")
79
 
            else:
80
 
                raise
 
68
    try:
 
69
        dbapi_conn.cursor().execute('select 1')
 
70
    except dbapi_conn.OperationalError, ex:
 
71
        if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
 
72
            LOG.warn('Got mysql server has gone away: %s', ex)
 
73
            raise DisconnectionError("Database server went away")
 
74
        else:
 
75
            raise
81
76
 
82
77
 
83
78
def is_db_connection_error(args):
91
86
    return False
92
87
 
93
88
 
 
89
def regexp(expr, item):
 
90
    reg = re.compile(expr)
 
91
    return reg.search(unicode(item)) is not None
 
92
 
 
93
 
 
94
class AddRegexFactory(sqlalchemy.interfaces.PoolListener):
 
95
    def connect(delf, dbapi_con, con_record):
 
96
        dbapi_con.create_function('REGEXP', 2, regexp)
 
97
 
 
98
 
94
99
def get_engine():
95
100
    """Return a SQLAlchemy engine."""
96
101
    global _ENGINE
115
120
            if FLAGS.sql_connection == "sqlite://":
116
121
                engine_args["poolclass"] = StaticPool
117
122
                engine_args["connect_args"] = {'check_same_thread': False}
118
 
 
 
123
                engine_args['listeners'] = [AddRegexFactory()]
 
124
 
 
125
        _ENGINE = sqlalchemy.create_engine(FLAGS.sql_connection, **engine_args)
 
126
 
 
127
        if 'mysql' in connection_dict.drivername:
 
128
            sqlalchemy.event.listen(_ENGINE, 'checkout', ping_listener)
 
129
        elif "sqlite" in connection_dict.drivername:
119
130
            if not FLAGS.sqlite_synchronous:
120
 
                engine_args["listeners"] = [SynchronousSwitchListener()]
121
 
 
122
 
        if 'mysql' in connection_dict.drivername:
123
 
            engine_args['listeners'] = [MySQLPingListener()]
124
 
 
125
 
        _ENGINE = sqlalchemy.create_engine(FLAGS.sql_connection, **engine_args)
 
131
                sqlalchemy.event.listen(_ENGINE, 'connect',
 
132
                                        synchronous_switch_listener)
126
133
 
127
134
        if (FLAGS.sql_connection_trace and
128
135
                _ENGINE.dialect.dbapi.__name__ == 'MySQLdb'):