Use the wait callback during connection if set.

This commit is contained in:
Daniele Varrazzo 2010-04-02 02:00:48 +01:00
parent 04d66a6c82
commit 389ad08965
2 changed files with 46 additions and 12 deletions

View File

@ -33,6 +33,7 @@
#include "psycopg/connection.h" #include "psycopg/connection.h"
#include "psycopg/cursor.h" #include "psycopg/cursor.h"
#include "psycopg/pqpath.h" #include "psycopg/pqpath.h"
#include "psycopg/green.h"
/* conn_notice_callback - process notices */ /* conn_notice_callback - process notices */
@ -319,12 +320,24 @@ int
conn_sync_connect(connectionObject *self) conn_sync_connect(connectionObject *self)
{ {
PGconn *pgconn; PGconn *pgconn;
PyObject *wait_rv;
int green;
Py_BEGIN_ALLOW_THREADS; /* store this value to prevent inconsistencies due to a change
self->pgconn = pgconn = PQconnectdb(self->dsn); * in the middle of the function. */
Py_END_ALLOW_THREADS; green = psyco_green();
if (!green) {
Dprintf("conn_connect: new postgresql connection at %p", pgconn); Py_BEGIN_ALLOW_THREADS;
self->pgconn = pgconn = PQconnectdb(self->dsn);
Py_END_ALLOW_THREADS;
Dprintf("conn_connect: new postgresql connection at %p", pgconn);
}
else {
Py_BEGIN_ALLOW_THREADS;
self->pgconn = pgconn = PQconnectStart(self->dsn);
Py_END_ALLOW_THREADS;
Dprintf("conn_connect: new green postgresql connection at %p", pgconn);
}
if (pgconn == NULL) if (pgconn == NULL)
{ {
@ -341,18 +354,39 @@ conn_sync_connect(connectionObject *self)
PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)self); PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)self);
if (conn_setup(self, pgconn) == -1) #ifdef HAVE_PQPROTOCOL3
self->protocol = PQprotocolVersion(pgconn);
#else
self->protocol = 2;
#endif
Dprintf("conn_connect: using protocol %d", self->protocol);
self->server_version = (int)PQserverVersion(pgconn);
/* if the connection is green, wait to finish connection */
if (green) {
wait_rv = psyco_wait((PyObject *)self, Py_None);
if (wait_rv) {
Py_DECREF(wait_rv);
} else {
return -1;
}
}
/* From here the connection is considered ready: with the new status,
* poll() will use PQisBusy instead of PQconnectPoll.
*/
self->status = CONN_STATUS_READY;
if (conn_setup(self, self->pgconn) == -1) {
return -1; return -1;
}
if (pq_set_non_blocking(self, 1, 1) != 0) { if (pq_set_non_blocking(self, 1, 1) != 0) {
return -1; return -1;
} }
self->protocol = conn_get_protocol_version(pgconn);
Dprintf("conn_connect: using protocol %d", self->protocol);
self->server_version = (int)PQserverVersion(pgconn);
return 0; return 0;
} }

View File

@ -702,7 +702,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
self->notifies = PyList_New(0); self->notifies = PyList_New(0);
self->closed = 0; self->closed = 0;
self->async = async; self->async = async;
self->status = async ? CONN_STATUS_SETUP : CONN_STATUS_READY; self->status = CONN_STATUS_SETUP;
self->critical = NULL; self->critical = NULL;
self->async_cursor = NULL; self->async_cursor = NULL;
self->async_status = ASYNC_DONE; self->async_status = ASYNC_DONE;