Solved

how many connection handles?

Posted on 2006-11-08
4
197 Views
Last Modified: 2010-04-15
Hi Experts,
I'm trying to connect mysql database through C.
I have around 20 tables in database and corresponding to those tables 20 structs in my API. All the tables can be simultaniously update/delete/insert through the code.
Here, my confusion is that can I use the same connection handle(pointer to connection) for all these updates/deletes simultaneously.
I'm confused because in Java, in this case of using the same connection object we do it 'shrychronized'.  
0
Comment
Question by:rrahulgupta
  • 2
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 17905836

The MySQL default is to allow 100 simultaneous connections so if you're going to allow multiple connections, you'll need to make sure that the middleware, cgi, etc are all thread safe.  That is, they'll need their own connection.

If your code connects to the database and executes a fork() you're fine.  You'll have passed the connection to the new task.  If the code opens the connection from the new task you're still fine.  Either way, the new task should cleanly close the connection when it terminates.


I would suggest that you don't use the same connection handle if you want simultaneous access by different processes.  It's much easier to manage the multi-tasking if you don't try to force everything through a single connection.


Good Luck,
Kent

0
 

Author Comment

by:rrahulgupta
ID: 17906282
Hi! Kent,
Thanks for the pretty informative comment.

I want to make it clear for me:
>I would suggest that you don't use the same connection handle if you want simultaneous access by different processes.
You have suggested to do not use same connection handle simaltaneously by different process. Here, do you mean to say that if process requires connection, I should create a new connection handle and return to that process.

BTW, I'm using the following code to return the connection handle:
===========================================
MYSQL *myconn(connection_info *conn_info)
{
    MYSQL *connect;      
    connect=mysql_init(NULL);
   if(connect==NULL)
   {
       printf("FAILED\n[-] init mysql failed:%s\n",mysql_error(connect));
       return NULL;
   }
   //#ifdef HAVE_OPENSSL
   int in = mysql_ssl_set(connect,"certs/client-key.pem",
    "certs/client-cert.pem",
    "certs/ca-cert.pem", 0, 0);
     // printf("%d\n", in);
   //#endif
   
   /* Connect to database */
   if(mysql_real_connect(connect,conn_info->server,conn_info->user,conn_info->password,conn_info->database,conn_info->port,NULL,0)==NULL)
   {
          printf("FAILED\n[-] Error: %s\n",mysql_error(connect));          
          return NULL;
    }
    return connect;
}
==============================================

Do you mean to say, whenever process requires a connection, function should return a new connection handle?

>It's much easier to manage the multi-tasking if you don't try to force everything through a single connection.
How can I achive this multi-tasking through my code? I'll apreiciate, if you send some code snippet for the same.

Thanks,
Rahul
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 17906332

Think of the connection as a file handle.  While you might (big if here) want to allow multiple processes to write to a file (such as a log file) it wouldn't do to have several processes read from a file using the same handle.

So when your program needs to access the database, just a few simple rules need to be followed:

1)  Does a connection exist from this process/task?  If so skip to 3
2)  Create a connection to the database
3)  Send query to MySQL
4)  Receive the results.


While the task is in these steps, it is effectively blocked from handling other SQL.  Since you want to multi task, you should fork other tasks to perform other queries.  They will all follow the same logic.


Kent
0
 

Author Comment

by:rrahulgupta
ID: 17906488
Thanks, Kent. I got it.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question