pgtools/pgmanager.py
changeset 2 efee419b7a2d
parent 0 eaae9539e910
child 3 1fe6c30f7700
equal deleted inserted replaced
0:eaae9539e910 2:efee419b7a2d
   149             self.destroy_conn(conn)
   149             self.destroy_conn(conn)
   150 
   150 
   151     def create_conn(self, name='default', isolation_level=None, dsn=None, **kw):
   151     def create_conn(self, name='default', isolation_level=None, dsn=None, **kw):
   152         '''Create named connection.'''
   152         '''Create named connection.'''
   153         if name in self.conn_known:
   153         if name in self.conn_known:
   154             raise PgManagerError('Connection name "%s" already registered.', name)
   154             raise PgManagerError('Connection name "%s" already registered.' % name)
   155 
   155 
   156         if dsn is None:
   156         if dsn is None:
   157             dsn = ' '.join([x[0]+'='+str(x[1]) for x in kw.items()])
   157             dsn = ' '.join([x[0]+'='+str(x[1]) for x in kw.items()])
   158 
   158 
   159         isolation_level = self._normalize_isolation_level(isolation_level)
   159         isolation_level = self._normalize_isolation_level(isolation_level)
   177         
   177         
   178         Counterpart of create_conn.
   178         Counterpart of create_conn.
   179         
   179         
   180         '''
   180         '''
   181         if not name in self.conn_known:
   181         if not name in self.conn_known:
   182             raise PgManagerError('Connection name "%s" not registered.', name)
   182             raise PgManagerError('Connection name "%s" not registered.' % name)
   183 
   183 
   184         self.close_conn(name)
   184         self.close_conn(name)
   185 
   185 
   186         del self.conn_known[name]
   186         del self.conn_known[name]
   187         del self.conn_pool[name]
   187         del self.conn_pool[name]
   189     def get_conn(self, name='default'):
   189     def get_conn(self, name='default'):
   190         '''Get connection of name 'name' from pool.'''
   190         '''Get connection of name 'name' from pool.'''
   191         self.lock.acquire()
   191         self.lock.acquire()
   192         try:
   192         try:
   193             if not name in self.conn_known:
   193             if not name in self.conn_known:
   194                 raise PgManagerError("Connection name '%s' not registered.", name)
   194                 raise PgManagerError("Connection name '%s' not registered." % name)
   195 
   195 
   196             conn = None
   196             conn = None
   197             while len(self.conn_pool[name]) and conn is None:
   197             while len(self.conn_pool[name]) and conn is None:
   198                 conn = self.conn_pool[name].pop()
   198                 conn = self.conn_pool[name].pop()
   199                 if conn.closed:
   199                 if conn.closed:
   220         
   220         
   221         '''
   221         '''
   222         self.lock.acquire()
   222         self.lock.acquire()
   223         try:
   223         try:
   224             if not name in self.conn_known:
   224             if not name in self.conn_known:
   225                 raise PgManagerError("Connection name '%s' not registered.", name)
   225                 raise PgManagerError("Connection name '%s' not registered." % name)
   226 
   226 
   227             if len(self.conn_pool[name]) >= self.conn_known[name].keep_open:
   227             if len(self.conn_pool[name]) >= self.conn_known[name].keep_open:
   228                 conn.close()
   228                 conn.close()
   229                 return
   229                 return
   230 
   230