当前位置:网站首页>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)
边栏推荐
- c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
- [dynamic planning] p4170: coloring (interval DP)
- Matlab-其它
- C language replaces spaces in strings with%20
- Viewing JS array through V8
- ICMP Protocol
- Don't know mock test yet? An article to familiarize you with mock
- The best blog to explain the basics of compilation (share)
- Jz-061-serialized binary tree
- OpenShift 部署应用
猜你喜欢

16: 00 interview, came out at 16:08, the question is really too

Routing foundation - dynamic routing

Minecraft空岛服开服

方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]

CarSim learning experience - rough translation 1

St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases

Getting started with k8s: building MySQL with Helm

Openfeign facile à utiliser

Qunhui NAS configuring iSCSI storage
随机推荐
Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
Openfeign is easy to use
Matlab - autres
Kubedm deploys kubernetes v1.23.5 cluster
sqli-labs第2关
Sqli labs (post type injection)
Getting started with k8s: building MySQL with Helm
Sqli labs level 12
类和对象(类和类的实例化,this,static关键字,封装)
The best blog to explain the basics of compilation (share)
[dynamic planning] p4170: coloring (interval DP)
Sqli labs level 8 (Boolean blind note)
ARP and ARP Spoofing
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Minecraft安装资源包
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
Web security -- core defense mechanism
Vs code configuration problem
Minecraft插件服开服
Web安全--核心防御机制