-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add TxOptions to txn.Begin #3327
Conversation
// TODO: accept sql.TxOptions when we remove gorm | ||
func (d *DB) Begin(ctx context.Context) (*Transaction, error) { | ||
tx := d.DB.WithContext(ctx).Begin() | ||
func (d *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Transaction, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could consider using variadic arguments to allow the opts to be optional.
func (d *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Transaction, error) { | |
func (d *DB) Begin(ctx context.Context, opts ...sql.TxOptions) (*Transaction, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see gorm
uses this pattern, but I consider this dangerous and I don't think it should ever be used.
varargs are great if you are going to use all the args, but using varargs for a single optional argument and ignoring the rest is going to cause confusion for anyone reading the code. Adding a nil
for the parameter isn't really a big inconvenience. We could also follow the stdlib and have two functions for Begin
, but at this time it didn't seem necessary since it was easy enough to add the nils.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's just a suggestion. The nil argument is a bit ugly, especially when that's the default case. From the project I've seen, this is a really common pattern and familiar to many Go devs.
6ead237
to
c66c012
Compare
80bb5f3
to
fe66be6
Compare
28d353f
to
c63f4c4
Compare
fe66be6
to
71ec757
Compare
c63f4c4
to
8420901
Compare
This will allow us to start read only transactions, or transactions with different isolation levels.
71ec757
to
f45166a
Compare
This will allow us to start read only transactions, or transactions with different isolation levels.
We're not using that yet, but after #3302 merges we'll be able to use it for handlers. An example of that can be seen here #3328.
We will also need this for long-polling. We need to use an isolation level that ensures that multiple queries in the same transaction all read the same snapshot (repeatable read).