List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:April 12 1999 8:20am
Subject:Opening multiple MySQL connections in one program (with and without threads).
View as plain text  
Hi!

>>>>> "terry" == terry jones <terry@stripped> writes:

terry> I'm trying to open multiple connections to my MySQL server from the
terry> one executable, but I keep getting segmentation violations. Can
terry> someone take a look at the following code and tell me if it works for
terry> them, or what I am doing wrong? There must be something basic I am
terry> missing (I guess). I am running mysql-3.22.21 on a 400MHz Pentium II
terry> GNU/Linux Redhat 5.1 with 512MB of memory.

terry> I built my version of mysql via:

terry>   ./configure \
terry>     --prefix=/usr/local/mysql \
terry>     --with-unix-socket-path=/usr/local/mysql/.mysql-pipe

As you are using threaded clients, you should add the switch
--enable-thread-safe-client

Change also the following:

void *
conn(void *arg)
{
    int threaded = (int)arg;
    
    MYSQL *mysql = (MYSQL *) malloc(sizeof(MYSQL));
    
    if (!mysql){
        fprintf(stderr, "bad mysql malloc.\n");
        exit(1);
    }
    
    fprintf(stderr, "Attempting to connect (%sthreaded).\n", threaded ? "" : "un");

    mysql_init(mysql);
    

To:

void *
conn(void *arg)
{
    int threaded = (int)arg;
    
    MYSQL *mysql = mysql_init((MYSQL *) 0);
    
    if (!mysql){
        fprintf(stderr, "bad mysql init.\n");
        exit(1);
    }
    
    fprintf(stderr, "Attempting to connect (%sthreaded).\n", threaded ? "" : "un");

    if (!mysql_real_connect(mysql, "localhost", "USER", "PASSWORD", "UNIVERSE", 3306,
NULL, 0)){

...

(This should be a little bit safer :)

The nice things with the above is that mysql_close() will automaticly
free the mysql structure!

Regards,
Monty
Thread
Opening multiple MySQL connections in one program (with and without threads).terry jones12 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).Ed Carp12 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).terry jones12 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).Ed Carp12 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).Ed Carp12 Apr
  • Re: Opening multiple MySQL connections in one program (with andwithout threads).Aldrian Gintingsuka12 Apr
  • Opening multiple MySQL connections in one program (with and without threads).Michael Widenius12 Apr
  • Opening multiple MySQL connections in one program (with and without threads).terry jones12 Apr
    • Re: Opening multiple MySQL connections in one program (with andwithout threads).Ilya Obshadko12 Apr
    • Re: Opening multiple MySQL connections in one program (with andwithout threads).terry jones12 Apr
      • Re: Opening multiple MySQL connections in one program (with andwithout threads).Ilya Obshadko12 Apr
      • Re: Opening multiple MySQL connections in one program (with andwithout threads).terry jones13 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).Ed Carp13 Apr
  • Re: Opening multiple MySQL connections in one program (with and without threads).terry jones14 Apr