Ensure create_engine() retries the initial connection test
Before return create_engine() tests the connectivity to a DB. At this moment a DB might be unavailable for some reason, in which case create_engine() retries a few times. When used with MySQL, we didn't actually retry, but instead failed on the first connectivity error when trying to get the value of sql_mode session variable. Closes-Bug: #1376211 Co-Authored-By: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: I14e25cfe1ed51b0d51a94e491b7267f26e42d34e
This commit is contained in:
parent
26ec2fcaa2
commit
01a54cc5cc
@ -466,19 +466,27 @@ def _init_events(engine, mysql_sql_mode=None, **kw):
|
|||||||
cursor = dbapi_con.cursor()
|
cursor = dbapi_con.cursor()
|
||||||
cursor.execute("SET SESSION sql_mode = %s", [mysql_sql_mode])
|
cursor.execute("SET SESSION sql_mode = %s", [mysql_sql_mode])
|
||||||
|
|
||||||
realmode = engine.execute("SHOW VARIABLES LIKE 'sql_mode'").fetchone()
|
@sqlalchemy.event.listens_for(engine, "first_connect")
|
||||||
if realmode is None:
|
def _check_effective_sql_mode(dbapi_con, connection_rec):
|
||||||
LOG.warning(_LW('Unable to detect effective SQL mode'))
|
if mysql_sql_mode is not None:
|
||||||
else:
|
_set_session_sql_mode(dbapi_con, connection_rec)
|
||||||
realmode = realmode[1]
|
|
||||||
LOG.debug('MySQL server mode set to %s', realmode)
|
cursor = dbapi_con.cursor()
|
||||||
if 'TRADITIONAL' not in realmode.upper() and \
|
cursor.execute("SHOW VARIABLES LIKE 'sql_mode'")
|
||||||
'STRICT_ALL_TABLES' not in realmode.upper():
|
realmode = cursor.fetchone()
|
||||||
LOG.warning(
|
|
||||||
_LW(
|
if realmode is None:
|
||||||
"MySQL SQL mode is '%s', "
|
LOG.warning(_LW('Unable to detect effective SQL mode'))
|
||||||
"consider enabling TRADITIONAL or STRICT_ALL_TABLES"),
|
else:
|
||||||
realmode)
|
realmode = realmode[1]
|
||||||
|
LOG.debug('MySQL server mode set to %s', realmode)
|
||||||
|
if 'TRADITIONAL' not in realmode.upper() and \
|
||||||
|
'STRICT_ALL_TABLES' not in realmode.upper():
|
||||||
|
LOG.warning(
|
||||||
|
_LW(
|
||||||
|
"MySQL SQL mode is '%s', "
|
||||||
|
"consider enabling TRADITIONAL or STRICT_ALL_TABLES"),
|
||||||
|
realmode)
|
||||||
|
|
||||||
|
|
||||||
@_init_events.dispatch_for("sqlite")
|
@_init_events.dispatch_for("sqlite")
|
||||||
|
@ -453,19 +453,36 @@ class MysqlConnectTest(test_base.MySQLOpportunisticTestCase):
|
|||||||
|
|
||||||
log = self.useFixture(fixtures.FakeLogger(level=logging.WARN))
|
log = self.useFixture(fixtures.FakeLogger(level=logging.WARN))
|
||||||
|
|
||||||
engine = self._fixture(sql_mode=None)
|
mysql_conn = self.engine.raw_connection()
|
||||||
|
self.addCleanup(mysql_conn.close)
|
||||||
|
mysql_conn.detach()
|
||||||
|
mysql_cursor = mysql_conn.cursor()
|
||||||
|
|
||||||
@sqlalchemy.event.listens_for(
|
def execute(statement, parameters=()):
|
||||||
engine, "before_cursor_execute", retval=True)
|
|
||||||
def replace_stmt(
|
|
||||||
conn, cursor, statement, parameters,
|
|
||||||
context, executemany):
|
|
||||||
if "SHOW VARIABLES LIKE 'sql_mode'" in statement:
|
if "SHOW VARIABLES LIKE 'sql_mode'" in statement:
|
||||||
statement = "SHOW VARIABLES LIKE 'i_dont_exist'"
|
statement = "SHOW VARIABLES LIKE 'i_dont_exist'"
|
||||||
return statement, parameters
|
return mysql_cursor.execute(statement, parameters)
|
||||||
|
|
||||||
session._init_events.dispatch_on_drivername("mysql")(engine)
|
test_engine = sqlalchemy.create_engine(self.engine.url,
|
||||||
|
_initialize=False)
|
||||||
|
|
||||||
|
with mock.patch.object(
|
||||||
|
test_engine.pool, '_creator',
|
||||||
|
mock.Mock(
|
||||||
|
return_value=mock.Mock(
|
||||||
|
cursor=mock.Mock(
|
||||||
|
return_value=mock.Mock(
|
||||||
|
execute=execute,
|
||||||
|
fetchone=mysql_cursor.fetchone,
|
||||||
|
fetchall=mysql_cursor.fetchall
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
):
|
||||||
|
session._init_events.dispatch_on_drivername("mysql")(test_engine)
|
||||||
|
|
||||||
|
test_engine.raw_connection()
|
||||||
self.assertIn('Unable to detect effective SQL mode',
|
self.assertIn('Unable to detect effective SQL mode',
|
||||||
log.output)
|
log.output)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user