On Dec 6, 2010, at 10:50 AM, M3nt0r3 wrote:

> HI,
> i am trying to handle the error c on connection when db is stopped or
> unrechable.
> 
>    File "/usr/lib/python2.6/dist-packages/sqlalchemy/pool.py", line
> 213, in __init__
>    self.connection = self.__connect()
> 
>    File "/usr/lib/python2.6/dist-packages/sqlalchemy/pool.py", line
> 279, in __connect
>    connection = self.__pool._creator()
> 
>    File "/home/vete/pg2_work/trunk/core/promogest/Environment.py",
> line 210, in connect
>    password=password, database=database)
> 
>  OperationalError: impossibile connettersi al server: Connessione
> rifiutata
>       Controllare che il server all'indirizzo "localhost" sia in funzione
>       e che accetti connessioni TCP/IP sulla porta 5432?

psycopg2 doesnt' give us a specific error for "disconnect", so we have no 
choice but to parse the text of the exception:

        if isinstance(e, self.dbapi.OperationalError):
            return 'closed the connection' in str(e) or 'connection not open' 
in str(e)
        elif isinstance(e, self.dbapi.InterfaceError):
            return 'connection already closed' in str(e) or 'cursor already 
closed' in str(e)
        elif isinstance(e, self.dbapi.ProgrammingError):
            # yes, it really says "losed", not "closed"
            return "losed the connection unexpectedly" in str(e)
        else:
            return False

unfortunately your PG database is using a different language for error 
messages.   You'd have to get your PG to produce error messages with the US 
locale for SQLA's current disconnect functionality to work.

Alternatively, if you're using ConnectionProxy, you can perhaps detect the 
disconnect messages in the desired language, then call 
connection.invalidate(exception), connection.engine.dispose(), then re-raise 
the exception.  This is basically what SQLA's own reconnect logic does.




> 
> That was the error
> 
> after some test with PoolListener and ConnectionProxy that are good to
> catch all other errors
> i found this solution but a really don't like to import psycopg2 ad
> this point.
> 
> def connect():
>    import psycopg2
>    a=None
>    try:
>        a = psycopg2.connect(user=user, host=host, port=port,
>                            password=password, database=database)
>    except Exception, e:
>        a= "CONNESSIONE AL DB NON RIUSCITA.\n DETTAGLIO ERRORE: [%s]"
> % ( e,)
>        messageInfo(msg=a)
>        exit( )
>    if a:
>        return a
> 
> def _psycopg2new():
>    try:
>        engine = create_engine('postgresql://', creator=connect,
>                convert_unicode=True,
> #                listeners=[MyListener()],
>                proxy=MyProxy())
> #        engine = create_engine('postgresql:'+'//'
> #                    +user+':'
> #                    + password+ '@'
> #                    + host + ':'
> #                    + port + '/'
> #                    + database,
> #                    encoding='utf-8',pool_size=30,
> #
> convert_unicode=True,listeners=[MyListener()],proxy=MyProxy() )
>        return engine
>    except:
>        return False
> 
> It works but my question is id there is another way to solve this
> problem.
> 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to