当前位置:网站首页>commands out of sync. did you run multiple statements at once
commands out of sync. did you run multiple statements at once
2022-07-02 08:41:00 【Tutu is a cat】
problem
The database connection error is caused after several insert operations , Throw an error
commands out of sync. did you run multiple statements at once
reason
- First, the normal insertion operation can eliminate the problem of connection parameters
- The check found that the database was inserted many times, resulting in open Too many database connections .

solve
Define global database connections , That is, the same connection is used multiple times
Define a public variable
var (
DB *gorm.DB
)
Define a function to establish a connection
func GetDB() (*gorm.DB,error) {
dsn := "root:[email protected](127.0.0.1:3306)/trs_hycloud_igi?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
})
if err != nil {
return nil, err
}
return db, err
}
Call the connected function in the main function and assign values to the defined public variables
func main() {
service.DB,_= service.GetDB()
}
Other database operations can directly use public variable operations
db := DB
db.Create(&reply)
边栏推荐
猜你喜欢

使用wireshark抓取Tcp三次握手

Solid principle: explanation and examples

sqli-labs第2关

Minecraft空岛服开服

Development of digital collection trading website development of metauniverse digital collection

Linux安装Oracle Database 19c

整理秒杀系统的面试必备!!!

sqli-labs第8关(布尔盲注)

HCIA—應用層

Use the kaggle training model and download your own training model
随机推荐
kubernetes部署loki日志系统
Openshift build image
Realize bidirectional linked list (with puppet node)
Googlenet network explanation and model building
Web security -- core defense mechanism
sqli-labs第8关(布尔盲注)
D interface and domain problems
Comparable,Comparator,Clonable 接口使用剖析
Minecraft群组服开服
Installation and use of simple packaging tools
[blackmail virus data recovery] suffix Hydra blackmail virus
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
KubeSphere 虚拟化 KSV 安装体验
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Makefile基本原理
Learning C
Sentinel 简单使用
STM32 new project (refer to punctual atom)
Zipkin is easy to use
Matlab other