pgthreadlock_t pg_g_threadlock = default_threadlock;
+/*
+ * pqDropConnection
+ *
+ * Close any physical connection to the server, and reset associated
+ * state inside the connection object. We don't release state that
+ * would be needed to reconnect, though.
+ */
+void
+pqDropConnection(PGconn *conn)
+{
+ /* Drop any SSL state */
+ pqsecure_close(conn);
+ /* Close the socket itself */
+ if (conn->sock >= 0)
+ closesocket(conn->sock);
+ conn->sock = -1;
+ /* Discard any unread/unsent data */
+ conn->inStart = conn->inCursor = conn->inEnd = 0;
+ conn->outCount = 0;
+}
+
+
/*
* Connecting to a Database
*
return 1;
connect_errReturn:
- if (conn->sock >= 0)
- {
- pqsecure_close(conn);
- closesocket(conn->sock);
- conn->sock = -1;
- }
+ pqDropConnection(conn);
conn->status = CONNECTION_BAD;
return 0;
}
{
if (!connectNoDelay(conn))
{
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->addr_cur = addr_cur->ai_next;
continue;
}
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to non-blocking mode: %s\n"),
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->addr_cur = addr_cur->ai_next;
continue;
}
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to close-on-exec mode: %s\n"),
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->addr_cur = addr_cur->ai_next;
continue;
}
if (err)
{
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->addr_cur = addr_cur->ai_next;
continue;
}
* failure and keep going if there are more addresses.
*/
connectFailureMessage(conn, SOCK_ERRNO);
- if (conn->sock >= 0)
- {
- closesocket(conn->sock);
- conn->sock = -1;
- }
+ pqDropConnection(conn);
/*
* Try the next address, if any.
* error message.
*/
connectFailureMessage(conn, optval);
+ pqDropConnection(conn);
/*
* If more addresses remain, keep trying, just as in the
*/
if (conn->addr_cur->ai_next != NULL)
{
- if (conn->sock >= 0)
- {
- closesocket(conn->sock);
- conn->sock = -1;
- }
conn->addr_cur = conn->addr_cur->ai_next;
conn->status = CONNECTION_NEEDED;
goto keep_going;
/* only retry once */
conn->allow_ssl_try = false;
/* Must drop the old connection */
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_NEEDED;
- /* Discard any unread/unsent data */
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
goto keep_going;
}
}
{
conn->pversion = PG_PROTOCOL(2, 0);
/* Must drop the old connection */
- pqsecure_close(conn);
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_NEEDED;
- /* Discard any unread/unsent data */
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
goto keep_going;
}
/* only retry once */
conn->wait_ssl_try = false;
/* Must drop the old connection */
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_NEEDED;
- /* Discard any unread/unsent data */
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
goto keep_going;
}
/* only retry once */
conn->allow_ssl_try = false;
/* Must drop the old connection */
- pqsecure_close(conn);
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_NEEDED;
- /* Discard any unread/unsent data */
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
goto keep_going;
}
#endif
PQclear(res);
conn->send_appname = false;
/* Must drop the old connection */
- pqsecure_close(conn);
- closesocket(conn->sock);
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_NEEDED;
- /* Discard any unread/unsent data */
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
goto keep_going;
}
}
/*
* Close the connection, reset all transient state, flush I/O buffers.
*/
- if (conn->sock >= 0)
- {
- pqsecure_close(conn);
- closesocket(conn->sock);
- }
- conn->sock = -1;
+ pqDropConnection(conn);
conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just
* absent */
conn->asyncStatus = PGASYNC_IDLE;
if (conn->lobjfuncs)
free(conn->lobjfuncs);
conn->lobjfuncs = NULL;
- conn->inStart = conn->inCursor = conn->inEnd = 0;
- conn->outCount = 0;
#ifdef ENABLE_GSS
{
OM_uint32 min_s;