Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how many connection handles?

Posted on 2006-11-08
4
Medium Priority
?
203 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 46

Expert Comment

by:Kent Olsen
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 46

Accepted Solution

by:
Kent Olsen earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.
Suggested Courses

885 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