Closed AntmanTmp closed 4 years ago
It's been a long time and I'm not sure there was ever a lot of thought put into the error handling here, but based on the snippets you've quoted, I think it's a bug in the mysql connector that an incorrect type raises OperationalError instead of ProgrammingError.
so if I put some sql in a transaction, when catch OperationalError, the connection is closed, mysql auto rollback, the rollback by myself has no effect.
Why does this matter? It shouldn't be a problem to roll back twice.
Fortunately, I have extended tornndb's support for transactions in my code, but I also don`t know the reason of close connection when catch OperationalError
My understanding (which could be wrong) is that in general, after you get an OperationalError, the connection is in a broken state and you can't do anything with it but to close it and open another.
My understanding (which could be wrong) is that in general, after you get an OperationalError, the connection is in a broken state and you can't do anything with it but to close it and open another.
Yes, if the conn is closed by OperationalError, which raised by some sql execute, mysql server will auto rollback, so I can do nothing.
def __enter__(self):
if not self.conn.has_transaction():
self._begin()
self.conn.assign_transaction(self)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
try:
if exc_type:
self.rollback()
elif self.conn.has_transaction():
try:
self.commit()
except:
self.rollback()
raise
finally:
self.conn.del_transaction()
the rollback effect mysql will automatic completion, not self.rollback() manual completion
from the docstring, we know torndb is a wrapper of mysqldb, and support some easy used DML sql method. the most important method is _execute and _cursor,you know
but why when catch OperationalError, will close the connection?
from the mysql doc we know OperationalError just some exception not related to sql execute, but from pep249 , this error is related to the database's operation, so maybe some sql execute can cause the error
mysql doc: https://dev.mysql.com/doc/connector-python/en/connector-python-api-errors-operationalerror.html
mysql doc: https://dev.mysql.com/doc/connector-python/en/connector-python-api-errors.html
pep249: https://www.python.org/dev/peps/pep-0249/
for example: when I execute a sql ,which a field type is not match table field schema, it also raise OperationalError
in my example, this field schema of table is integer,but I put a string in sql
so if I put some sql in a transaction, when catch OperationalError, the connection is closed, mysql auto rollback, the rollback by myself has no effect.
Fortunately, I have extended tornndb's support for transactions in my code, but I also don`t know the reason of close connection when catch OperationalError
Even more confusing is according to mysql doc, 1366 will be DatabaseError, but in fact it raise OperationalError
server-error-reference
you see HY000
it will be DatabaseError when you check the table here connector-python-api-errors