1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>30.5.Choosing a Connection</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="ecpg.html" title="Chapter30.ECPG - Embedded SQL in C">
<link rel="prev" href="ecpg-commands.html" title="30.4.Running SQL Commands">
<link rel="next" href="ecpg-variables.html" title="30.6.Using Host Variables">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="ecpg-set-connection"></a>30.5.Choosing a Connection</h2></div></div></div>
<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 <em class="replaceable"><code>connection-name</code></em> 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 <em class="replaceable"><code>connection-name</code></em>;</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></body>
</html>
|