db fail 当前打开连接数

This commit is contained in:
李子铭 2025-03-20 17:10:21 +08:00
parent 70dbaf72c0
commit 2c131e30c8
3 changed files with 15 additions and 3 deletions

View File

@ -52,7 +52,9 @@ func db(data *conf.Data_Database) (*gorm.DB, func()) {
// SetMaxOpenConns sets the maximum number of openapi connections to the database.
sqlDB.SetMaxOpenConns(int(data.MaxOpen))
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
sqlDB.SetConnMaxLifetime(1 * time.Hour)
//sqlDB.SetConnMaxLifetime(1 * time.Hour)
sqlDB.SetConnMaxLifetime(7200 * time.Second)
sqlDB.SetConnMaxIdleTime(15 * time.Minute)
return gormDB, func() {
if mysqlConn != nil {

View File

@ -3,6 +3,7 @@ package repoimpl
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"gorm.io/gorm"
"time"
"unicode/utf8"
@ -84,9 +85,13 @@ func (p *OrderRepoImpl) Create(ctx context.Context, req *bo.OrderBo) (*bo.OrderB
func (p *OrderRepoImpl) GetByOutBizNo(ctx context.Context, t vo.OrderType, outBizNo string) (*bo.OrderBo, error) {
info := &model.Order{}
tx := p.DB(ctx).Where(model.Order{Type: t.GetValue(), OutBizNo: outBizNo}).Find(&info)
db := p.DB(ctx)
tx := db.Where(model.Order{Type: t.GetValue(), OutBizNo: outBizNo}).Find(&info)
if tx.Error != nil {
sqlDB, _ := db.DB()
log.Warnf("order 当前打开连接数: %d ,空闲连接数: ", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)
return nil, fmt.Errorf("order db fail %w", tx.Error)
}

View File

@ -3,6 +3,7 @@ package repoimpl
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"gorm.io/gorm"
err2 "voucher/api/err"
"voucher/internal/biz/bo"
@ -29,9 +30,13 @@ func (p *ProductRepoImpl) DB(ctx context.Context) *gorm.DB {
func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.ProductBo, error) {
var item model.Product
tx := r.DB(ctx).Where(model.Product{ProductNo: PNO}).Find(&item)
db := r.DB(ctx)
tx := db.Where(model.Product{ProductNo: PNO}).Find(&item)
if tx.Error != nil {
sqlDB, _ := db.DB()
log.Warnf("当前打开连接数: %d ,空闲连接数: ", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)
return nil, fmt.Errorf("product db fail %w", tx.Error)
}