Solved

how many connection handles?

Posted on 2006-11-08
4
196 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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 and use conditional statements in the C programming language.

911 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

21 Experts available now in Live!

Get 1:1 Help Now