summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-04-10 19:38:22 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-04-10 19:38:22 -0400
commit5280f26d45f6d79013d2da3bd5e0fe4a88117b20 (patch)
treeb58a4623060d7caf0ac509d0655fe1a09661e44c /lib
parent3269b73ff7a12303aadcaed0246d401ec649fa94 (diff)
downloadsqlalchemy-5280f26d45f6d79013d2da3bd5e0fe4a88117b20.tar.gz
- [feature] Added new connection event
dbapi_error(). Is called for all DBAPI-level errors passing the original DBAPI exception before SQLAlchemy modifies the state of the cursor.
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/engine/base.py9
-rw-r--r--lib/sqlalchemy/events.py30
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index 1f28501f7..110ac4e8a 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -1806,10 +1806,19 @@ class Connection(Connectable):
(statement is not None and context is None)
if should_wrap and context:
+ if self._has_events:
+ self.engine.dispatch.dbapi_error(self,
+ cursor,
+ statement,
+ parameters,
+ context,
+ e)
context.handle_dbapi_exception(e)
is_disconnect = isinstance(e, self.dialect.dbapi.Error) and \
self.dialect.is_disconnect(e, self.__connection, cursor)
+
+
if is_disconnect:
self.invalidate(e)
self.engine.dispose()
diff --git a/lib/sqlalchemy/events.py b/lib/sqlalchemy/events.py
index 504dfe150..18888be03 100644
--- a/lib/sqlalchemy/events.py
+++ b/lib/sqlalchemy/events.py
@@ -401,6 +401,36 @@ class ConnectionEvents(event.Events):
parameters, context, executemany):
"""Intercept low-level cursor execute() events."""
+ def dbapi_error(self, conn, cursor, statement, parameters,
+ context, exception):
+ """Intercept a raw DBAPI error.
+
+ This event is called with the DBAPI exception instance
+ received from the DBAPI itself, *before* SQLAlchemy wraps the
+ exception with it's own exception wrappers, and before any
+ other operations are performed on the DBAPI cursor; the
+ existing transaction remains in effect as well as any state
+ on the cursor.
+
+ The use case here is to inject low-level exception handling
+ into an :class:`.Engine`, typically for logging and
+ debugging purposes. In general, user code should **not** modify
+ any state or throw any exceptions here as this will
+ interfere with SQLAlchemy's cleanup and error handling
+ routines.
+
+ Subsequent to this hook, SQLAlchemy may attempt any
+ number of operations on the connection/cursor, including
+ closing the cursor, rolling back of the transaction in the
+ case of connectionless execution, and disposing of the entire
+ connection pool if a "disconnect" was detected. The
+ exception is then wrapped in a SQLAlchemy DBAPI exception
+ wrapper and re-thrown.
+
+ New in 0.7.7.
+
+ """
+
def begin(self, conn):
"""Intercept begin() events."""