We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Connection Management Structure in C language

rrahulgupta
rrahulgupta asked
on
Medium Priority
312 Views
Last Modified: 2013-12-03
I have to design and code the persistence(database) api to insert/update/delete the records in table in C language, there are number of tables in the database and so I want to create a common connection managenet entity so it will be reusable for across the api. To doing this should be my approach?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

Commented:
> I want to create a common connection managenet entity so it will be reusable for across the api.
You mean capability to access all table using a single connection? If yes, then connection would remain open until you explicitly close it. Keep passing the handle around and things would work fine. This applies to server side too. you can use multiple threads in master/slave fashion where one thread assigns tasks to other threads to process. Number of tables does not influence your connection.

Author

Commented:
Thanks sunny.
My questioned are inlined:

>You mean capability to access all table using a single connection?
Actually, i was thinking that this will be right approach if i can do it. but i was not sure because i don't have so much experience in C, however, i have pretty good idea about OOPs and java.

> If yes, then connection would remain open until you explicitly close it.
I say, yes. However, i would like to know that there some other option to do the same?  
 
>This applies to server side too. you can use multiple threads in master/slave fashion where one thread assigns tasks to other threads to process. Number of tables does not influence your connection.
I didn't got that. can u plz explain it a bit more, please.

CERTIFIED EXPERT
Top Expert 2006
Commented:
>> If yes, then connection would remain open until you explicitly close it.
>I say, yes. However, i would like to know that there some other option to do the same?
There are several ways to accomplish this kind of communcation but which one suits best depends on the application. Simplest possible solution would be to have server listen for incoming connections. Client connects, server accepts and services requests from the client - one at a time. After a client is finished, connection is closed and next client connection is accepted and served.

An obvious improvement to this would be to have a multi-threaded server capable of serving multiple clients. While this would make database more usable, it would also make it necessary to ensure proper synchronization between different threads.

Another variation would be to use UDP to post requests to server. This is usable if operations are idempotent and non-critical.

Yet another option is build a distributed computing environment with client using RPC calls to invoke service routines e.g. as used in CORBA.

>>This applies to server side too. you can use multiple threads in master/slave fashion where one thread assigns tasks
>>to other threads to process. Number of tables does not influence your connection.
>I didn't got that. can u plz explain it a bit more, please.

Client
1- Connect to server
2- Post request
3- parse response
4- If there are more requests, go to step 2
5- close connection

Server Main thread
1- Listen for client connections
2- Accept client connection
3- Spawn a worker thread and pass client socket to new worker thread to service this client
4- Go to step 1

Server worker thread
1- Accept client request
2- if client sought to close connection then close connection and exit
3- process client request
4- send response
5- Go to 1

There can again be several variations of this - e.g. master thread can maintain a pool of worker threads. Rather than spawning a thread for each request and letting it die after processing one client, same thread can be reused to serve other clients in future.

Yet another improvement would be to keep a pool of min and max threads and spawn/kill threads depending on server load as done in Apache web server.

lastly, the grading history in your profile might influence the kind of help you receive
Last 10 Grades Given B C
http://www.experts-exchange.com/help.jsp#hi73

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.