zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
U
M±d ,ã@stdZddlmZddlmZz eefZWne    k
r@eZYnXGdd„de
ƒZ Gdd„de ƒZ Gd    d
„d
ƒZ d S) a.
SteadyPg - hardened classic PyGreSQL connections.
 
Implements steady connections to a PostgreSQL database
using the classic (not DB-API 2 compliant) PyGreSQL API.
 
The connections are transparently reopened when they are
closed or the database connection has been lost or when
they are used more often than an optional usage limit.
Only connections which have been marked as being in a database
transaction with a begin() call will not be silently replaced.
 
A typical situation where database connections are lost
is when the database server or an intervening firewall is
shutdown and restarted for maintenance reasons.  In such a
case, all database connections would become unusable, even
though the database service may be already available again.
 
The "hardened" connections provided by this module will
make the database connections immediately available again.
 
This results in a steady PostgreSQL connection that can be used
by PooledPg or PersistentPg to create pooled or persistent
connections to a PostgreSQL database in a threaded environment
such as the application server of "Webware for Python."
Note, however, that the connections themselves are not thread-safe.
 
For more information on PostgreSQL, see:
    https://www.postgresql.org/
For more information on PyGreSQL, see:
    http://www.pygresql.org
For more information on Webware for Python, see:
    https://webwareforpython.github.io/w4py/
 
 
Usage:
 
You can use the class SteadyPgConnection in the same way as you
would use the class DB from the classic PyGreSQL API module db.
The only difference is that you may specify a usage limit as the
first parameter when you open a connection (set it to None
if you prefer unlimited usage), and an optional list of commands
that may serve to prepare the session as the second parameter,
and you can specify whether is is allowed to close the connection
(by default this is true).  When the connection to the PostgreSQL
database is lost or has been used too often, it will be automatically
reset, without further notice.
 
    from dbutils.steady_pg import SteadyPgConnection
    db = SteadyPgConnection(10000, ["set datestyle to german"],
        host=..., dbname=..., user=..., ...)
    ...
    result = db.query('...')
    ...
    db.close()
 
 
Ideas for improvement:
 
* Alternatively to the maximum number of uses,
  implement a maximum time to live for connections.
* Optionally log usage and loss of connection.
 
 
Copyright, credits and license:
 
* Contributed as supplement for Webware for Python and PyGreSQL
  by Christoph Zwerschke in September 2005
 
Licensed under the MIT license.
é)ÚDBé)Ú __version__c@seZdZdZdS)Ú SteadyPgErrorzGeneral SteadyPg error.N©Ú__name__Ú
__module__Ú __qualname__Ú__doc__©r r úHd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\dbutils/steady_pg.pyrRsrc@seZdZdZdS)ÚInvalidConnectionzDatabase connection is invalid.Nrr r r r r Vsr c@s–eZdZdZeZd"dd„Zdd„Zdd    „Zd
d „Z    d d „Z
dd„Z dd„Z dd„Z d#dd„Zd$dd„Zd%dd„Zd&dd„Zdd„Zdd„Zd d!„ZdS)'ÚSteadyPgConnectionaûClass representing steady connections to a PostgreSQL database.
 
    Underlying the connection is a classic PyGreSQL pg API database
    connection which is reset if the connection is lost or used too often.
    Thus the resulting connection is steadier ("tough and self-healing").
 
    If you want the connection to be persistent in a threaded environment,
    then you should not deal with this class directly, but use either the
    PooledPg module or the PersistentPg module to get the connections.
    NTcOsfd|_d|_|dkrd}t|tƒs*tdƒ‚||_||_||_t||Ž|_d|_    d|_| 
¡d|_ dS)aÅCreate a "tough" PostgreSQL connection.
 
        maxusage: maximum usage limit for the underlying PyGreSQL connection
            (number of uses, 0 or None means unlimited usage)
            When this limit is reached, the connection is automatically reset.
        setsession: optional list of SQL commands that may serve to prepare
            the session, e.g. ["set datestyle to ...", "set time zone ..."]
        closeable: if this is set to false, then closing the connection will
            be silently ignored, but by default the connection can be closed
        args, kwargs: the parameters that shall be used to establish
            the PostgreSQL connections with PyGreSQL using pg.DB()
        NTrz$'maxusage' must be an integer value.F) Ú_conÚ_closedÚ
isinstanceÚbaseintÚ    TypeErrorÚ    _maxusageÚ_setsession_sqlÚ
_closeableÚ PgConnectionÚ _transactionÚ _setsessionÚ_usage)ÚselfZmaxusageZ
setsessionZ    closeableÚargsÚkwargsr r r Ú__init__hs
 zSteadyPgConnection.__init__cCs | ¡|S)z9Enter the runtime context. This will start a transaction.)Úbegin©rr r r Ú    __enter__ˆszSteadyPgConnection.__enter__cGs:|ddkr.|ddkr.|ddkr.| ¡n| ¡dS)z8Exit the runtime context. This will end the transaction.rNré)ÚcommitÚrollback)rÚexcr r r Ú__exit__s$
zSteadyPgConnection.__exit__cCs"|jr|jD]}|j |¡q dS)z1Execute the SQL commands for session preparation.N)rrÚquery)rÚsqlr r r r”s
zSteadyPgConnection._setsessioncCs:|js6z|j ¡Wntk
r(YnXd|_d|_dS)z§Close the tough connection.
 
        You can always close a tough connection with this method
        and it will not complain if you close it more than once.
        FTN)rrÚcloseÚ    Exceptionrr r r r Ú_closešszSteadyPgConnection._closecCs"|jr| ¡n|jr| ¡dS)a]Close the tough connection.
 
        You are allowed to close a tough connection by default
        and it will not complain if you close it more than once.
 
        You can disallow closing connections by setting
        the closeable parameter to something false.  In this case,
        closing tough connections will be silently ignored.
        N)rr+rÚresetr r r r r)¨s
 
zSteadyPgConnection.closec Cstz|j ¡WnFtk
rT|jrPd|_z|j d¡Wntk
rNYnXYnXd|_d|_| ¡d|_dS)ziReopen the tough connection.
 
        It will not complain if the connection cannot be reopened.
        Fr$rN)    rÚreopenr*Z _transcationrr'rrrr r r r r-·s zSteadyPgConnection.reopencCs€z"|j ¡d|_| ¡d|_WnXtk
rzz | ¡Wn6tk
rtz | ¡Wntk
rnYnXYnXYnXdS)z«Reset the tough connection.
 
        If a reset is not possible, tries to reopen the connection.
        It will not complain if the connection is already closed.
        FrN)rr,rrrr*r-r$r r r r r,Ës
 
  zSteadyPgConnection.resetcCsPd|_z |jj}Wn$tk
r6|j |p.d¡YSX|rF||dS|ƒSdS)zBegin a transaction.Tr©r(N)rrrÚAttributeErrorr')rr(rr r r rßs 
zSteadyPgConnection.begincCsPd|_z |jj}Wn$tk
r6|j |p.d¡YSX|rF||dS|ƒSdS)úCommit the current transaction.FÚendr.N)rrr1r/r')rr(r1r r r r1ís 
zSteadyPgConnection.endcCsPd|_z |jj}Wn$tk
r6|j |p.d¡YSX|rF||dS|ƒSdS)r0Fr#r.N)rrr#r/r')rr(r#r r r r#ús 
zSteadyPgConnection.commitcCsPd|_z |jj}Wn$tk
r6|j |p.d¡YSX|rF||dS|ƒSdS)z!Rollback the current transaction.Fr$r.N)rrr$r/r')rr(r$r r r r$s 
zSteadyPgConnection.rollbackcs‡‡fdd„}|S)aReturn a "tough" version of a connection class method.
 
        The tough version checks whether the connection is bad (lost)
        and automatically and transparently tries to reset the connection
        if this is the case (for instance, the database has been restarted).
        cs°ˆj}|sLz$ˆjjjr(ˆjr,ˆjˆjkr,t‚Wntk
rJˆ ¡YnXzˆ||Ž}WnBtk
rœ|rxdˆ_‚n ˆjjjr†‚nˆ ¡ˆ||Ž}YnXˆjd7_|S)NFr)    rrÚdbÚstatusrrr/r*r,)rrZ transactionÚresult©Úmethodrr r Ú tough_methods.
ÿ
ÿ
z:SteadyPgConnection._get_tough_method.<locals>.tough_methodr )rr6r7r r5r Ú_get_tough_methodsz$SteadyPgConnection._get_tough_methodcCs:|jr2t|j|ƒ}|dks$| d¡r.| |¡}|St‚dS)z…Inherit the members of the standard connection class.
 
        Some methods are made "tougher" than in the standard version.
        )r'ÚgetÚinsertÚupdateÚdeleteÚget_N)rÚgetattrÚ
startswithr8r )rÚnameÚattrr r r Ú __getattr__5s ÿ
zSteadyPgConnection.__getattr__cCs&z | ¡Wntk
r YnXdS)zDelete the steady connection.N)r+r*r r r r Ú__del__Cs zSteadyPgConnection.__del__)NNT)N)N)N)N)rrr    r
rÚversionrr!r&rr+r)r-r,rr1r#r$r8rBrCr r r r rZs( ÿ
 
 
 
 
!rN)r
ZpgrrÚrÚintÚlongrÚ    NameErrorr*rr rr r r r Ú<module>sG