Re: [cx-oracle-users] How to transparently re-connect using cx_Oracle?
Brought to you by:
atuining
From: Amaury F. d'A. <ama...@gm...> - 2006-10-04 05:52:59
|
2006/9/28, Laurelin of Middle Earth wrote: > Hello, > > Is it possible for a cx_Oracle.Connection to be told to "reconnect"? > .... > > But the situation I'm facing now is: client establishes their > cx_Oracle.Connection instance (and the assocated cx_Oracle.Cursor). > Some time later, the database is "bounced", and that connection is no > longer good. The RetryHandler recognizes the "not connected to > Oracle" exception, but what to do with it? > > The RetryHandler has been given a cursor object (with its connection), > and cannot "replace" that cursor/connection with a different instance. > But it would be nice to tell that cursor/connection to "try to connect > yourself again from scratch, as if you were new, but within the existing > instance at the existing address". > > Is this possible? Or am I reaching for the moon? A solution could be to wrap both classes (connection and cursor) inside "RetryingConnection", "RetryingCursor" classes. They delegate all calls to their base objects, but the RetryingConnection.cursor() function returns a wrapped RetryingCursor. RetryingCursor contains a reference to its parent RetryingConnection, in addition to its base cursor object. When a method of RetryingCursor detects an error, it asks the RetryingConnection to connect again, then recreates its cursor member. Note that some functions may be difficult to restart: if fetchone() fails in the middle of a long resultset, you have no choice but trying to fetch again the previous lines. And it's better if Variables are not involved... At least, this method should work for the plain cursor.execute() function. I hope I was clear. I tried to write some sample code, but I could not figure how your RetryHandler works. It is really an object, or a simple function? -- Amaury Forgeot d'Arc |