Skip to content
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

sqlite: implement driver.SessionResetter and driver.Validator #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raggi
Copy link
Member

@raggi raggi commented May 26, 2023

These implementations enable us to maintain connections in database/sql pools.

Fixes #70

These implementations enable us to maintain connections in database/sql
pools.

Fixes #70
if c.stmts != nil {
for _, s := range c.stmts {
if err := s.resetAndClear(); err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this return ErrBadConn?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above case we could and probably should. In this particular case, that's unclear because:

ErrBadConn should be returned by a driver to signal to the sql package that a driver.Conn is in a bad state (such as the server having earlier closed the connection) and the sql package should retry on a new connection.

To prevent duplicate operations, ErrBadConn should NOT be returned if there's a possibility that the database server might have performed the operation. Even if the server sends back an error, you shouldn't return ErrBadConn.

Errors will be checked using errors.Is. An error may wrap ErrBadConn or implement the Is(error) bool method.

If we have potentially completed a tx we can not disambiguate between these conditions so we should not match BadConn.

Copy link
Member

@crawshaw crawshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we generate the sort of error that gets database/sql to call ResetSession? if so, can you write a unit test that exercises this code?

}

// ensure that all prepared statements are reset and unbound
if c.stmts != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil check is redundant because a range over a nil map does nothing


// ensure that all prepared statements are reset and unbound
if c.stmts != nil {
for _, s := range c.stmts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is almost right. in practice it would basically work. but the problem are statements created with persist=false, which don't get put into c.stmts.

@raggi
Copy link
Member Author

raggi commented May 27, 2023

I can reverse it. It's absent from the docs. We do do have stats showing that we're not retaining cons. Either way yeah, I'll find a way to make some tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement driver.SessionResetter and driver.Validator so that conns are retained in database/sql pools
3 participants