New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
database/sql: No way to tell before use if sql.Stmt is closed #20993
Comments
/cc @kardianos |
That sounds reasonable. @ncapdevi Could you post your use case where it is difficult to determine if the stmt is closed or not? |
@kardianos Sure. The general situation is that we are doing a bunch of batched DB queries/exec. We have a helper I hope this is clear, if you need any more clarification, please let me know. |
That all sounds good. Out of curiosity, why would there by a stmt.Close() call at all? |
There shouldn't be, or at least I can't imagine that there would be, but it's a project with multiple people working on it and as you can imagine, things sneak in there sometimes. It's more precautionary than anything. My thoughts were that it should be reasonably quick to implement (would be happy to do a PR myself, but am not entirely familiar with the sql.go file so didn't want to make assumptions), so it'd be nice to have in there as a safe guard. |
I'm less thrilled with adding a "IsClosed()" type of call for a just-in-case use. But, we have pleanty of time before the go1.10 freeze to think about it. I'd be interested in hearing other use cases from you or other organizations too. in the mean time. |
This seems like a mistake. If you don't know whether the statement is closed, you also don't know whether someone else is going to close the statement a nanosecond after you confirm that it's open. We shouldn't encourage programs not to know fundamental details like "has someone closed this statement?" There's no equivalent for open files, for example. |
@rsc that's a fair point, but it greatly, greatly, reduces the likelihood of a problem, and even if it DOES get closed in that nanosecond in between, you'd get the same "sql: statement is closed" that is and always has existed. So you're taking care of 99.99% use cases and if not, falling back to what is already currently implemented. |
I'm going to decline this proposal. I think @rsc 's comment was appropriate. If this is a problem you can easily audit your code base for any instances of Stmt.Close with common tools. |
What version of Go are you using (
go version
)?1.8.3
What did you do?
Performing a query on a statement that may/may not have been previously
closed()
can result in sql.go line 1883-1885 being hit, returning error "sql: statement is closed" it would be much more efficient if there were some way to check stmt.IsClosed() which returned stmt.closed and if it was, the user could re-prepare the statement on their own. As it stands, you would need to perform a query, check the error coming back via a string compare, then re-perform the query.The text was updated successfully, but these errors were encountered: