<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Choosing a Connection</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="ECPG - Embedded SQL in C" HREF="ecpg.html"><LINK REL="PREVIOUS" TITLE="Running SQL Commands" HREF="ecpg-commands.html"><LINK REL="NEXT" TITLE="Using Host Variables" HREF="ecpg-variables.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2007-02-02T03:57:22"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-commands.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 29. <SPAN CLASS="APPLICATION" >ECPG</SPAN > - Embedded <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > in C</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="ecpg.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="ecpg-variables.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="ECPG-SET-CONNECTION" >29.5. Choosing a Connection</A ></H1 ><P > The SQL statements shown in the previous section are executed on the current connection, that is, the most recently opened one. If an application needs to manage multiple connections, then there are two ways to handle this. </P ><P > The first option is to explicitly choose a connection for each SQL statement, for example </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL AT <TT CLASS="REPLACEABLE" ><I >connection-name</I ></TT > SELECT ...;</PRE ><P> This option is particularly suitable if the application needs to use several connections in mixed order. </P ><P > If your application uses multiple threads of execution, they cannot share a connection concurrently. You must either explicitly control access to the connection (using mutexes) or use a connection for each thread. If each thread uses its own connection, you will need to use the AT clause to specify which connection the thread will use. </P ><P > The second option is to execute a statement to switch the current connection. That statement is: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL SET CONNECTION <TT CLASS="REPLACEABLE" ><I >connection-name</I ></TT >;</PRE ><P> This option is particularly convenient if many statements are to be executed on the same connection. It is not thread-aware. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-commands.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="ecpg-variables.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Running SQL Commands</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ecpg.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Using Host Variables</TD ></TR ></TABLE ></DIV ></BODY ></HTML >