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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,32 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
// Raw is so ConnInitFunc can cast to SQLConn.
func (c *conn) Raw(fn func(any) error) error { return fn(c) }

// ResetSession is called prior to executing a query on the connection
// if the connection has been used before. If the driver returns ErrBadConn
// the connection is discarded.
func (c *conn) ResetSession(ctx context.Context) error {
if c.txState != txStateNone {
return errors.New("cannot ResetSession on a connection with an active transaction")
}

// 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

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.

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.

}
}
}

return nil
}

// IsValid is called prior to placing the connection into the
// connection pool. The connection will be discarded if false is returned.
func (c *conn) IsValid() bool {
return true
}

type readOnlyKey struct{}

// ReadOnly applies the query_only pragma to the connection.
Expand Down
7 changes: 7 additions & 0 deletions sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (
"github.com/tailscale/sqlite/sqliteh"
)

// SessionResetter and Validator are required in order for conns to be returned
// to database/sql connection pools.
var (
_ driver.SessionResetter = (*conn)(nil)
_ driver.Validator = (*conn)(nil)
)

func TestOpenDB(t *testing.T) {
db := openTestDB(t)
var journalMode string
Expand Down