[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

how many connection handles?

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
rrahulgupta
Asked:
rrahulgupta
  • 2
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
rrahulguptaAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
rrahulguptaAuthor Commented:
Thanks, Kent. I got it.
0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now