当前位置:网站首页>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 06:30:00 【图图是只猫】
问题
数据库在执行几次插入操作后导致数据库连接错误,抛出错误
commands out of sync. did you run multiple statements at once
原因
- 首先正常插入操作可以排除连接参数问题
- 检查发现数据库由于多次插入操作导致此处open了过多的数据库连接。
解决
定义全局的数据库连接,即同一个连接多次使用
定义一个公共的变量
var (
DB *gorm.DB
)
定义一个建立连接的函数
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
}
在主函数中调用建立连接的函数并对定义的公共的变量赋值
func main() {
service.DB,_= service.GetDB()
}
其他数据库操作可直接使用公共变量操作
db := DB
db.Create(&reply)
边栏推荐
猜你喜欢
KubeSphere 虚拟化 KSV 安装体验
Service de groupe minecraft
ARP and ARP Spoofing
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Installation and use of simple packaging tools
Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
Chrome debugging
Sqli labs level 12
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
旋转链表(图解说明)
随机推荐
Web安全--核心防御机制
HCIA - application layer
[blackmail virus data recovery] suffix Crylock blackmail virus
Realize bidirectional linked list (with puppet node)
zipkin 简单使用
[blackmail virus data recovery] suffix Rook3 blackmail virus
Service de groupe minecraft
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
IP protocol and IP address
HCIA—應用層
STM32 new project (refer to punctual atom)
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
程序猿学英语-指令式编程
What is SQL injection
Carsim 学习心得-粗略翻译1
STL quick reference manual
File upload and download performance test based on the locust framework
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
Summary of one question per day: String article (continuously updated)
Benefits of ufcs of D