我正在尝试使用 GO lang (1.18) 对 MySQL (5.7) 进行第一次 CRUD。 CREATE & READ SQL 不返回错误,但在 UPDATE & DELETE 中返回错误。我正在搜索,看起来像是连接配置错误或错误......但我不知道如何跟踪它。
MySQL 驱动程序:github.com/go-sql-driver/mysql v1.6.0
我的仓库:https://github.com/ramadoiranedar/GO_REST_API
错误是:
[mysql] 2022/09/22 00:04:44 connection.go:299: invalid connection
driver: bad connection
我的 SQL 函数看起来像这样:
func NewDB() *sql.DB {
db, err := sql.Open("mysql", "MY_USERNAME:MY_PASSWORD@tcp(localhost:3306)/go_restful_api")
helper.PanicIfError(err)
db.SetMaxIdleConns(5)
db.SetMaxIdleConns(20)
db.SetConnMaxLifetime(60 * time.Minute)
db.SetConnMaxIdleTime(10 * time.Minute)
return db
}
func (repository *CategoryRepositoryImpl) Update(ctx context.Context, tx *sql.Tx, category domain.Category) domain.Category {
SQL := "update category set name = ? where id = ?"
_, err := tx.ExecContext(ctx, SQL, category.Name, category.Id)
helper.PanicIfError(err)
return category
}
func (repository *CategoryRepositoryImpl) Delete(ctx context.Context, tx *sql.Tx, category domain.Category) {
SQL := "delete from category where id = ?"
_, err := tx.ExecContext(ctx, SQL, category.Name, category.Id)
helper.PanicIfError(err)
}
func (repository *CategoryRepositoryImpl) Create(ctx context.Context, tx *sql.Tx, category domain.Category) domain.Category {
SQL := "insert into category (name) values (?)"
result, err := tx.ExecContext(ctx, SQL, category.Name)
helper.PanicIfError(err)
id, err := result.LastInsertId()
if err != nil {
panic(err)
}
category.Id = int(id)
return category
}
func (repository *CategoryRepositoryImpl) FindAll(ctx context.Context, tx *sql.Tx) []domain.Category {
SQL := "select id, name from category"
rows, err := tx.QueryContext(ctx, SQL)
helper.PanicIfError(err)
var categories []domain.Category
for rows.Next() {
category := domain.Category{}
err := rows.Scan(
&category.Id,
&category.Name,
)
helper.PanicIfError(err)
categories = append(categories, category)
}
return categories
}
func (repository *CategoryRepositoryImpl) FindById(ctx context.Context, tx *sql.Tx, categoryId int) (domain.Category, error) {
SQL := "select id, name from category where id = ?"
rows, err := tx.QueryContext(ctx, SQL, categoryId)
helper.PanicIfError(err)
category := domain.Category{}
if rows.Next() {
err := rows.Scan(&category.Id, &category.Name)
helper.PanicIfError(err)
return category, nil
} else {
return category, errors.New("CATEGORY IS NOT FOUND")
}
}
长话短说:您正在不应该使用数据库事务。
将它们保留用于一组必须成功的写入操作。
我建议您重新定义您的存储库及其实现。分层的整个想法是独立于存储库实现,并且能够从 mysql 切换到 mongo,再切换到 postgres,或者 w/e。
//category_controller.go
type CategoryRepository interface {
Create(ctx context.Context, category domain.Category) domain.Category
Update(ctx context.Context, category domain.Category) domain.Category
Delete(ctx context.Context, category domain.Category)
FindById(ctx context.Context, categoryId int) (domain.Category, error)
FindAll(ctx context.Context) []domain.Category
}
//category_repository_impl.go
type CategoryRepositoryImpl struct {
db *sql.DB
}
func NewCategoryRepository(db *sql.DB) CategoryRepository {
return &CategoryRepositoryImpl{db: db}
}
请参阅以下要点以及代码文件的修改版本: https://gist.github.com/audrenbdb/94660f707a206d385c42f64ceb93a4aa
如果出现无效连接错误,则意味着存在一个连接尚未关闭的选择查询。尝试将 FindById 和 FindAll 方法部分更改为:
func (repository *CategoryRepositoryImpl) FindById(ctx context.Context, tx *sql.Tx, categoryId int) (domain.Category, error) {
SQL := "select id, name from category where id = ?"
rows, err := tx.QueryContext(ctx, SQL, categoryId)
helper.PanicIfError(err)
defer rows.Close()
category := domain.Category{}
if rows.Next() {
err := rows.Scan(&category.Id, &category.Name)
helper.PanicIfError(err)
return category, nil
} else {
return category, errors.New("CATEGORY IS NOT FOUND")
}}
并在其他读取方法中也添加 Close() 以在使用后关闭连接