Changeset 844 for trunk


Ignore:
Timestamp:
Feb 8, 2016, 6:15:53 PM (4 years ago)
Author:
cito
Message:

New connection attribute "closed" in pgdb

This can be useful when implementing connection pools, e.g. in SQLAlchemy.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/pgdb/connection.rst

    r814 r844  
    7070    The following attributes are not part of the DB-API 2 standard.
    7171
     72.. attribute:: Connection.closed
     73
     74    This is *True* if the connection has been closed or has become invalid
     75
    7276.. attribute:: Connection.cursor_type
    7377
  • trunk/pgdb.py

    r843 r844  
    13421342            raise _op_error("Connection has been closed")
    13431343
     1344    @property
     1345    def closed(self):
     1346        """Check whether the connection has been closed or is broken."""
     1347        try:
     1348            return not self._cnx or self._cnx.status != 1
     1349        except TypeError:
     1350            return True
     1351
    13441352    def commit(self):
    13451353        """Commit any pending transaction to the database."""
  • trunk/pgmodule.c

    r824 r844  
    14421442check_cnx_obj(connObject *self)
    14431443{
    1444         if (!self->valid)
     1444        if (!self || !self->valid || !self->cnx)
    14451445        {
    14461446                set_error_msg(OperationalError, "Connection has been closed");
     
    36303630                return NULL;
    36313631
    3632         /* make sure that the connection object is valid */
    3633         if (!self->pgcnx->cnx)
    3634                 return NULL;
    3635 
    36363632        encoding = PQclientEncoding(self->pgcnx->cnx);
    36373633
     
    37723768
    37733769        /* checks validity */
    3774         if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
     3770        if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL | CHECK_CNX))
    37753771                return NULL;
    37763772
     
    39813977        /* checks validity */
    39823978        if (!check_source_obj(self, CHECK_CNX | CHECK_RESULT) ||
    3983                         !self->pgcnx->cnx ||
    39843979                        PQresultStatus(self->result) != PGRES_COPY_IN)
    39853980        {
     
    40694064        /* checks validity */
    40704065        if (!check_source_obj(self, CHECK_CNX | CHECK_RESULT) ||
    4071                         !self->pgcnx->cnx ||
    40724066                        PQresultStatus(self->result) != PGRES_COPY_OUT)
    40734067        {
  • trunk/tests/test_dbapi20.py

    r843 r844  
    419419        cur.execute("select 1 union select 2 union select 3")
    420420        self.assertEqual([r[0] for r in cur], [1, 2, 3])
     421
     422    def test_cursor_invalidation(self):
     423        con = self._connect()
     424        cur = con.cursor()
     425        cur.execute("select 1 union select 2")
     426        self.assertEqual(cur.fetchone(), (1,))
     427        self.assertFalse(con.closed)
     428        con.close()
     429        self.assertTrue(con.closed)
     430        self.assertRaises(pgdb.OperationalError, cur.fetchone)
    421431
    422432    def test_fetch_2_rows(self):
Note: See TracChangeset for help on using the changeset viewer.