I am doing some unit testing and noticed that if I click (or have multiple peopl
ID: 659354 • Letter: I
Question
I am doing some unit testing and noticed that if I click (or have multiple people clicking) the same filter very very fast (very precise I know;) eventually I get a "The connection is already open." error. I know that one way to solve this is to open new individual connections for each query each time it's called but I wonder if doing so opens my app up to a security risk -perhaps an easier time of a DoS/DDos attack?
Also, the connection is closed once completed.
If this is a risk, how can I (how do others) solve this problem?
Explanation / Answer
It sounds to me like you're running into thread-safety issues. The fix for this, as you've discovered, is to not re-use connections.
To your question specifically, no, there is no security risk in opening new connections each time you need to connect to the database. You don't say what technology you're using (besides MySQL) but typically if you're following good coding practices such as only returning the data you need, and closing connections as soon as you're through with them, there should not be any significant increase in the risk of DoS attacks than when you re-use connections, and there may even be less by eliminating the threading issues you're seeing now. Additionally, if whatever data provider technology you're using offers connection pooling, this can eliminate the performance impacts of creating new database connections almost entirely, making perf a non-issue.
So, yes, you should be spinning up a new database connection each time you need to access the database. It's the safest option you have.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.