禁用事物

This commit is contained in:
ziming 2025-03-26 13:50:06 +08:00
parent b2f6021d3d
commit 0d4a5e3528
2 changed files with 27 additions and 54 deletions

View File

@ -28,7 +28,7 @@ func db(data *conf.Data_Database) (*gorm.DB, func()) {
mysql.Open(data.Source), mysql.Open(data.Source),
&gorm.Config{ &gorm.Config{
Logger: logger.Default.LogMode(logger.Info), Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: false, SkipDefaultTransaction: true,
}, },
) )

View File

@ -25,7 +25,7 @@ func Test_gorm_db_create(t *testing.T) {
mysql.Open(source), mysql.Open(source),
&gorm.Config{ &gorm.Config{
Logger: logger.Default.LogMode(logger.Info), Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: false, SkipDefaultTransaction: true,
}, },
) )
if err != nil { if err != nil {
@ -37,14 +37,25 @@ func Test_gorm_db_create(t *testing.T) {
t.Fatal("failed to gormDB " + err.Error()) t.Fatal("failed to gormDB " + err.Error())
} }
sqlDB.SetMaxIdleConns(1000) sqlDB.SetMaxIdleConns(20)
sqlDB.SetMaxOpenConns(1000) sqlDB.SetMaxOpenConns(100)
start2 := time.Now() start2 := time.Now()
concurrency := 1000 // 调整并发数 concurrency := 1000 // 调整并发数
errCount := 0 errCount := 0
stats := sqlDB.Stats()
fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)
go func() {
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
fmt.Printf("监控: 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)
}
}()
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(concurrency) wg.Add(concurrency)
@ -52,14 +63,13 @@ func Test_gorm_db_create(t *testing.T) {
go func(i int) { go func(i int) {
defer wg.Done() defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx := context.Background()
defer cancel()
now := time.Now() now := time.Now()
info := &model.Order{ info := &model.Order{
OrderNo: fmt.Sprintf("test_create_1_%d", i), OrderNo: fmt.Sprintf("test_create_10_%d", i),
OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e2_%d", i), OutBizNo: fmt.Sprintf("374252390990605ngywYrSAGE8310_%d", i),
ProductNo: "001", ProductNo: "001",
BatchNo: "001", BatchNo: "001",
Account: "oO3vO5K2nE131-9uMoeYymLhlbYk", Account: "oO3vO5K2nE131-9uMoeYymLhlbYk",
@ -80,14 +90,21 @@ func Test_gorm_db_create(t *testing.T) {
info.Status = vo.OrderStatusSuccess.GetValue() info.Status = vo.OrderStatusSuccess.GetValue()
} }
tx := gormDB.WithContext(ctx).Model(model.Order{}).Create(info) ddd := gormDB.WithContext(ctx).Model(model.Order{})
tx := ddd.Create(info)
if tx.Error != nil { if tx.Error != nil {
fmt.Printf("写入错误: %v\n", tx.Error) fmt.Printf("写入错误: %v\n", tx.Error)
errCount += 1 errCount += 1
} }
sqlDBx, _ := ddd.DB()
fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", sqlDBx.Stats().OpenConnections, sqlDBx.Stats().Idle)
}(i) }(i)
} }
fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)
wg.Wait() wg.Wait()
t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount) t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
} }
@ -102,6 +119,7 @@ func Test_gorm_db(t *testing.T) {
// MaxLifetime: durationpb.New(60), // 5分钟 // MaxLifetime: durationpb.New(60), // 5分钟
// IsDebug: false, // IsDebug: false,
//} //}
// 测试
//data := &conf.Data_Database{ //data := &conf.Data_Database{
// Driver: "mysql", // Driver: "mysql",
// Source: "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local", // Source: "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
@ -110,7 +128,6 @@ func Test_gorm_db(t *testing.T) {
// MaxLifetime: durationpb.New(60), // MaxLifetime: durationpb.New(60),
// IsDebug: false, // IsDebug: false,
//} //}
data := &conf.Data_Database{ data := &conf.Data_Database{
Driver: "mysql", Driver: "mysql",
Source: "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local", Source: "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local",
@ -199,47 +216,3 @@ func Test_db(t *testing.T) {
t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount) t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
} }
func Test_orderCreate(t *testing.T) {
//maxLifetime := durationpb.New(300) // 5分钟
//data := &conf.Data_Database{
// Driver: "mysql",
// Source: "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
// MaxIdle: 2, // 空闲连接池
// MaxOpen: 10, // 最大连接数
// MaxLifetime: maxLifetime,
// IsDebug: false,
//}
//gormDb, cleanup := db(data)
//defer cleanup()
//
//now := time.Now()
//
//info := &model.Order{
// OrderNo: req.OrderNo,
// OutBizNo: req.OutBizNo,
// ProductNo: req.ProductNo,
// BatchNo: req.BatchNo,
// Account: req.Account,
// AccountType: req.AccountType.GetValue(),
// Status: req.Status.GetValue(),
// Type: req.Type.GetValue(),
// AppID: req.AppID,
// MerchantNo: req.MerchantNo,
// Channel: req.Channel.GetValue(),
// NotifyUrl: req.NotifyUrl,
// Attach: req.Attach,
// CreateTime: &now,
// UpdateTime: &now,
//}
//
//if req.ProductNo == "001" {
// info.VoucherNo = req.OrderNo
// info.Status = vo.OrderStatusSuccess.GetValue()
//}
//
//tx := p.DB(ctx).Create(info)
//if tx.Error != nil {
// return nil, fmt.Errorf("create db fail %w", tx.Error)
//}
}