2


I am using postgres 9.0 database , where In my C program I connect the database only once and using fork I generate processes , where all my child program shares the connection Most of the time it works correctly , In some cases child A gets the query error of child B , and It also gets the query time out issues and all

My question is , Is there any wrong to share the connections ? per second it may create 1 to 5 processes at the max

Note: I never closes the connection at all

1 Answer 1

2

It's not a good idea for threads to share a single database connection because you'll run into the exact issue that you described in your question: one thread can get the output of another thread's request. Instead, you'll want each thread to connect separately. If you have a lot of threads, you might want to consider making a connection pool.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , but FYI I used fork , is your answer applicable for both fork and thread ? , kindly reply
Yes. If two processes (either started by threads or via fork) share the same database connection, then the database has no way of telling which process made a request or to which process it should send output.
This is covered in the documentation: postgresql.org/docs/9.1/static/libpq-connect.html (as a case of "don't do that")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.