remove mutex from sql tx

Summary: We were releasing tx lock before consuming rows data which could place the connection in a bad state. Instead of changing lock placement we chose to remove it as mutations were changed to being serial which is expected when working with transactions.

Reviewed By: a8m

Differential Revision: D16449629

fbshipit-source-id: 9f2e7435036f35c407f0640e3dd1071a17b3c368
This commit is contained in:
Alex Snast
2019-07-23 20:12:21 -07:00
committed by Facebook Github Bot
parent 1e47de5300
commit ff6403b4ae
8 changed files with 62 additions and 79 deletions

File diff suppressed because one or more lines are too long

View File

@@ -53,13 +53,10 @@ func (tx *Tx) Client() *Client {
// applies a query, for example: {{ $first.Name }}.QueryXXX(), the query will be executed
// through the driver which created this transaction.
//
// Note that this driver is safe for concurrent usage, however, it executes only one query
// at the time.
// Note that txDriver is not goroutine safe.
type txDriver struct {
// the driver we started the transaction from.
drv dialect.Driver
// protects the tx below from concurrent execution.
mu sync.Mutex
// tx is the underlying transaction.
tx dialect.Tx
}
@@ -93,15 +90,11 @@ func (*txDriver) Rollback() error { return nil }
// Exec calls tx.Exec.
func (tx *txDriver) Exec(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Exec(ctx, query, args, v)
}
// Query calls tx.Query.
func (tx *txDriver) Query(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Query(ctx, query, args, v)
}

View File

@@ -3,7 +3,7 @@ version: "3.7"
services:
mysql:
image: mysql:5.7.23
image: mysql:5.7.26
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: pass

View File

@@ -4,7 +4,6 @@ package ent
import (
"context"
"sync"
"fbc/ent/dialect"
"fbc/ent/entc/integration/ent/migrate"
@@ -70,13 +69,10 @@ func (tx *Tx) Client() *Client {
// applies a query, for example: Card.QueryXXX(), the query will be executed
// through the driver which created this transaction.
//
// Note that this driver is safe for concurrent usage, however, it executes only one query
// at the time.
// Note that txDriver is not goroutine safe.
type txDriver struct {
// the driver we started the transaction from.
drv dialect.Driver
// protects the tx below from concurrent execution.
mu sync.Mutex
// tx is the underlying transaction.
tx dialect.Tx
}
@@ -110,15 +106,11 @@ func (*txDriver) Rollback() error { return nil }
// Exec calls tx.Exec.
func (tx *txDriver) Exec(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Exec(ctx, query, args, v)
}
// Query calls tx.Query.
func (tx *txDriver) Query(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Query(ctx, query, args, v)
}

View File

@@ -4,7 +4,6 @@ package entv1
import (
"context"
"sync"
"fbc/ent/dialect"
"fbc/ent/entc/integration/migrate/entv1/migrate"
@@ -46,13 +45,10 @@ func (tx *Tx) Client() *Client {
// applies a query, for example: User.QueryXXX(), the query will be executed
// through the driver which created this transaction.
//
// Note that this driver is safe for concurrent usage, however, it executes only one query
// at the time.
// Note that txDriver is not goroutine safe.
type txDriver struct {
// the driver we started the transaction from.
drv dialect.Driver
// protects the tx below from concurrent execution.
mu sync.Mutex
// tx is the underlying transaction.
tx dialect.Tx
}
@@ -86,15 +82,11 @@ func (*txDriver) Rollback() error { return nil }
// Exec calls tx.Exec.
func (tx *txDriver) Exec(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Exec(ctx, query, args, v)
}
// Query calls tx.Query.
func (tx *txDriver) Query(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Query(ctx, query, args, v)
}

View File

@@ -4,7 +4,6 @@ package entv2
import (
"context"
"sync"
"fbc/ent/dialect"
"fbc/ent/entc/integration/migrate/entv2/migrate"
@@ -52,13 +51,10 @@ func (tx *Tx) Client() *Client {
// applies a query, for example: Group.QueryXXX(), the query will be executed
// through the driver which created this transaction.
//
// Note that this driver is safe for concurrent usage, however, it executes only one query
// at the time.
// Note that txDriver is not goroutine safe.
type txDriver struct {
// the driver we started the transaction from.
drv dialect.Driver
// protects the tx below from concurrent execution.
mu sync.Mutex
// tx is the underlying transaction.
tx dialect.Tx
}
@@ -92,15 +88,11 @@ func (*txDriver) Rollback() error { return nil }
// Exec calls tx.Exec.
func (tx *txDriver) Exec(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Exec(ctx, query, args, v)
}
// Query calls tx.Query.
func (tx *txDriver) Query(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Query(ctx, query, args, v)
}

View File

@@ -4,7 +4,6 @@ package ent
import (
"context"
"sync"
"fbc/ent/dialect"
"fbc/ent/entc/integration/plugin/ent/migrate"
@@ -46,13 +45,10 @@ func (tx *Tx) Client() *Client {
// applies a query, for example: Boring.QueryXXX(), the query will be executed
// through the driver which created this transaction.
//
// Note that this driver is safe for concurrent usage, however, it executes only one query
// at the time.
// Note that txDriver is not goroutine safe.
type txDriver struct {
// the driver we started the transaction from.
drv dialect.Driver
// protects the tx below from concurrent execution.
mu sync.Mutex
// tx is the underlying transaction.
tx dialect.Tx
}
@@ -86,15 +82,11 @@ func (*txDriver) Rollback() error { return nil }
// Exec calls tx.Exec.
func (tx *txDriver) Exec(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Exec(ctx, query, args, v)
}
// Query calls tx.Query.
func (tx *txDriver) Query(ctx context.Context, query string, args interface{}, v interface{}) error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Query(ctx, query, args, v)
}

View File

@@ -1,4 +1,4 @@
// Code generated by go-bindata. (@generated) DO NOT EDIT.
// Package build Code generated by go-bindata. (@generated) DO NOT EDIT.
// sources:
// template/build.tmpl
package build
@@ -47,21 +47,32 @@ type bindataFileInfo struct {
modTime time.Time
}
// Name return file name
func (fi bindataFileInfo) Name() string {
return fi.name
}
// Size return file size
func (fi bindataFileInfo) Size() int64 {
return fi.size
}
// Mode return file mode
func (fi bindataFileInfo) Mode() os.FileMode {
return fi.mode
}
// Mode return file modify time
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}
// IsDir return file whether a directory
func (fi bindataFileInfo) IsDir() bool {
return false
return fi.mode&os.ModeDir != 0
}
// Sys return file is sys mode
func (fi bindataFileInfo) Sys() interface{} {
return nil
}
@@ -81,7 +92,7 @@ func templateBuildTmpl() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "template/build.tmpl", size: 364, mode: os.FileMode(420), modTime: time.Unix(1562857148, 0)}
info := bindataFileInfo{name: "template/build.tmpl", size: 364, mode: os.FileMode(420), modTime: time.Unix(1560858190, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}