Michael Widenius wrote:
>
> >>>>> "Christian" == Christian Mack <Mack@stripped> writes:
>
> Christian> Shane Wegner wrote:
> >>
> >> Hi,
> >>
> >> I wrote an application which uses threads and I am planning to add Mysql
> >> support. My question is one relating to design and that is when a
> >> multithreaded program uses Mysql using the mysqlclient library, is it best
> >> to have each thread connect to the Mysql server seperately or is it safe
> >> to use a shared handle. The MYSQL_RES and MYSQL_ROW would be local to the
> >> thread of corse but can the handle be global? If it can, what will happen
> >> when two threads submit a query? Will the second wait until the first
> >> completes or should this construct just be avoided?
> >>
> >> --
> >> Shane Wegner: shane@stripped
>
> Christian> Hi Shane
>
> Christian> You have to give each thread its own connection.
> Christian> Mysql connections can't handle two concurrent queries.
> Christian> If you want to reduce the amount of open connections, you can create a
> connection pool.
> Christian> This obviously only will spare some connections as long as not all
> threads are querying at the same time.
>
> Christian> Tschau
> Christian> Christian
>
> Hi!
>
> You can use a global handle as long as you ensure that no other thread
> calls access the handle until you have called mysql_store_result().
>
> Regards,
> Monty
>
So does than mean that you can do something like this:
MYSQL dbh;
mysql_connect(&dbh, "localhost", "scott", "tiger");
scream_if_error(&dbh);
mysql_select_db("test");
switch(fork())
{
case -1: scream_bloody_murder_and_die("Cannot fork");
case 0: mysql_query(&dbh, "insert into test values
(1,'hello1'); break;
default: mysql_query(&dbh, "insert into test values
(2,'hello2'); break;
}
--
Sasha Pachev
http://www.sashanet.com/ (home)
http://www.direct1.com/ (work)