Solved

how many connection handles?

Posted on 2006-11-08
4
195 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
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
Thanks, Kent. I got it.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now