~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to psycopg/connection_int.c

  • Committer: Federico Di Gregorio
  • Date: 2004-12-10 10:34:57 UTC
  • Revision ID: fog-1b2e9ebeaa5d76bd7e4a9dc5eb779afb4076f236
async fixes and better connection/cursor management.

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
void
132
132
conn_close(connectionObject *self)
133
133
{
134
 
    int len, i;
135
 
    PyObject *t = NULL;
136
 
 
137
134
    /* sets this connection as closed even for other threads; also note that
138
135
       we need to check the value of pgconn, because we get called even when
139
136
       the connection fails! */
143
140
    self->closed = 1;
144
141
 
145
142
    /* execute a forced rollback on the connection (but don't check the
146
 
       result, we're going to close the pq connection anyway */
147
 
    if (self->pgconn) pq_abort(self);
148
 
    
149
 
    /* orphans all the children cursors but do NOT destroy them (note that we
150
 
       need to lock the connection before orphaning a cursor: we don't want to
151
 
       remove a connection from a cursor executing a DB operation */
152
 
    pthread_mutex_unlock(&self->lock);
153
 
    Py_END_ALLOW_THREADS;
154
 
 
155
 
    pthread_mutex_lock(&self->lock);
156
 
    len = PyList_Size(self->cursors);
157
 
    Dprintf("conn_close: ophaning %d cursors", len);
158
 
    for (i = len-1; i >= 0; i--) {
159
 
        t = PySequence_GetItem(self->cursors, i);
160
 
        Dprintf("conn close:     cursor at %p: refcnt = %d", t, t->ob_refcnt);
161
 
        PySequence_DelItem(self->cursors, i);
162
 
        ((cursorObject *)t)->conn = NULL; /* orphaned */
163
 
        Dprintf("conn_close:     -> new refcnt = %d", t->ob_refcnt);
164
 
    }
165
 
    pthread_mutex_unlock(&self->lock);
166
 
    
167
 
    /* now that all cursors have been orphaned (they can't operate on the
168
 
       database anymore) we can shut down the connection */
 
143
       result, we're going to close the pq connection anyway */    
169
144
    if (self->pgconn) {
 
145
        pq_abort(self);
170
146
        PQfinish(self->pgconn);
171
147
        Dprintf("conn_close: PQfinish called");
172
148
        self->pgconn = NULL;
173
149
    }
 
150
 
 
151
    pthread_mutex_unlock(&self->lock);
 
152
    Py_END_ALLOW_THREADS;
 
153
 
174
154
}
175
155
 
176
156
/* conn_commit - commit on a connection */