1 from threading
import Condition
3 from .
import __version__
4 from .steady_db
import connect
8 """General PooledDB error."""
12 """Database connection is invalid."""
16 """DB-API module not supported by PooledDB."""
20 """Too many database connections were opened."""
24 """Pool for DB-API 2 connections.
26 After you have created the connection pool, you can use
27 connection() to get pooled, steady DB-API 2 connections.
33 self, creator, mincached=0, maxcached=0,
34 maxshared=0, maxconnections=0, blocking=False,
35 maxusage=None, setsession=None, reset=True,
36 failures=None, ping=1,
42 creator:返回新的DB-API 2的任意函数
52 maxconnections:通常允许的最大连接数
55 (如果将其设置为true,请阻止并等待,直到
61 setsession:可用于准备的SQL命令的可选列表
62 会话,例如[“将日期样式设置为...”,“将时区设置为...”]
64 (对于以begin()开始的回滚事务,为False或None,
66 failures:可选的异常类或异常类的元组
68 如果默认值(OperationalError,InternalError)不足够
69 ping:确定何时应使用ping()检查连接
70 (0 =无=永不,1 =默认=每当从池中获取时,
73 args,kwargs:应传递给创建者的参数
80 :param maxconnections:默认即可
85 :param setsession:默认即可
93 :param POOL:使用自定义的PooledDB,不建议
96 threadsafety = creator.threadsafety
97 except AttributeError:
99 if not callable(creator.connect):
101 except AttributeError:
115 if mincached
is None:
117 if maxcached
is None:
119 if maxconnections
is None:
122 if maxcached < mincached:
123 maxcached = mincached
127 if threadsafety > 1
and maxshared:
133 if maxconnections < maxcached:
134 maxconnections = maxcached
135 if maxconnections < maxshared:
136 maxconnections = maxshared
141 self.
_lock = Condition()
149 """获得稳定的,未池化的DB-API 2连接."""
155 """从连接池中获得稳定的缓存的DB-API 2连接。
157 :param shareable:允许共享连接
178 while con.con._transaction:
184 con.con._ping_check()
212 """Alias for connection(shareable=False)."""
216 """Decrease the share of a connection in the shared cache."""
232 """Put a dedicated connection back into the idle cache."""
236 con._reset(force=self.
_reset)
247 """Close all connections in the pool."""
264 self.
_lock.notifyAll()
269 """Delete the pool."""
276 """Wait until notified or report an error."""
278 raise TooManyConnections
285 """Auxiliary proxy class for pooled dedicated connections."""
288 """Create a pooled dedicated connection.
290 pool: the corresponding PooledDB instance
291 con: the underlying SteadyDB connection
296 if not con.threadsafety():
302 """Close the pooled dedicated connection."""
310 """Proxy all members of the class."""
312 return getattr(self.
_con, name)
314 raise InvalidConnection
317 """Delete the pooled connection."""
325 """Auxiliary class for shared connections."""
328 """Create a shared connection.
330 con: the underlying SteadyDB connection
336 if self.
con._transaction == other.con._transaction:
337 return self.
shared < other.shared
339 return not self.
con._transaction
342 if self.
con._transaction == other.con._transaction:
343 return self.
shared <= other.shared
345 return not self.
con._transaction
348 return (self.
con._transaction == other.con._transaction
349 and self.
shared == other.shared)
352 return not self.
__eq__(other)
355 return other.__lt__(self)
358 return other.__le__(self)
361 """Increase the share of this connection."""
365 """Decrease the share of this connection."""
370 """Auxiliary proxy class for pooled shared connections."""
373 """Create a pooled shared connection.
375 pool: the corresponding PooledDB instance
376 con: the underlying SharedDBConnection
382 if not con.threadsafety() > 1:
389 """Close the pooled shared connection."""
397 """Proxy all members of the class."""
399 return getattr(self.
_con, name)
401 raise InvalidConnection
404 """Delete the pooled connection."""