package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func main(){
db, err := gorm.Open(mysql.New(mysql.Config{
DSN: "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local", // data source name
DefaultStringSize: 256, // default size for string fields
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
}), &gorm.Config{})
if err != nil{
panic("failed to connect database")
}
db.AutoMigrate(&Product{})
//db.Create(&Product{Code:"D42",Price:100})
/*var product Productdb.First(&product,1)
db.First(&product,"code = ?","D42")
db.Model(&product).Update("Price",200)
db.Model(&product).Updates(Product{Price:200,Code: "F42"})
db.Model(&product).Updates(map[string]interface{}{"Price":200,"Code":"F42"})*/
intChan := make(chan int)
errChan := make(chan error)
//go printMsg(errChan,intChan);
for i:=0;i<200;i++{
go test1(errChan,intChan,db)
go test2(errChan,intChan,db)
}
var name string
fmt.Scanf("1:%s 2:%d 3:%t", &name)
fmt.Println(name)
}
func printMsg( eC chan error,c chan int){
for{
fmt.Println(<- eC,<- c)
}
}
func test1( eC chan error,c chan int,db *gorm.DB){
tx:=db.Begin()
product := &Product{}
if err := tx.Model(product).Where("id = ?",1).Update("price",gorm.Expr("price + ?",1)).Error;err != nil{
tx.Rollback();
return
}
tx.Commit()
}
func test2( eC chan error,c chan int,db *gorm.DB){
tx:=db.Begin()
product := &Product{}
if err := tx.Model(product).Where("id = ?",1).Update("price",gorm.Expr("price + ?",1)).Error;err != nil{
//tx.Rollback();
return
}
tx.Commit()
}
error:
`** F:\go\mysql>go run main.go panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x1 addr=0x18 pc=0x50874e]
goroutine 240 [running]: database/sql.(Tx).grabConn(0x0, 0x844d80, 0xc0000a0068, 0x0, 0x0, 0x0, 0x0) C:/Go/src/database/sql/sql.go:2045 +0x5e database/sql.(Tx).ExecContext(0x0, 0x844d80, 0xc0000a0068, 0xc000a8a0c0, 0x43, 0xc000a84280, 0x3, 0x4, 0xc000559d98, 0x5a41b5, ...) C:/Go/src/database/sql/sql.go:2271 +0x53 gorm.io/driver/mysql.Update(0xc000a96600) C:/Users/gaopeng/go/pkg/mod/gorm.io/driver/mysql@v1.0.3/update_with_order_by_limit.go:34 +0x3cd gorm.io/gorm.(processor).Execute(0xc0000db540, 0xc000a96600) C:/Users/gaopeng/go/pkg/mod/gorm.io/gorm@v1.20.9/callbacks.go:105 +0x22b gorm.io/gorm.(DB).Update(0xc000a96600, 0x7c2dd9, 0x5, 0x796940, 0xc000a966f0, 0x1) C:/Users/gaopeng/go/pkg/mod/gorm.io/gorm@v1.20.9/finisher_api.go:310 +0x10f main.test2(0xc000086360, 0xc000086300, 0xc00015db60) F:/go/mysql/main.go:76 +0x20d created by main.main F:/go/mysql/main.go:43 +0x235 exit status 2
**`
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: listen-lavender
so where is the solution?