当前位置:网站首页>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)
边栏推荐
- 整理秒杀系统的面试必备!!!
- TCP/IP—传输层
- Linux二进制安装Oracle Database 19c
- Openfeign is easy to use
- When a custom exception encounters reflection
- Minecraft安装资源包
- Installation and use of simple packaging tools
- IP protocol and IP address
- The best blog to explain the basics of compilation (share)
- C language replaces spaces in strings with%20
猜你喜欢
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
HCIA - data link layer
sqli-labs第12关
ICMP Protocol
File upload and download performance test based on the locust framework
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Use C language to receive JSON strings
KubeSphere 虚拟化 KSV 安装体验
kubernetes部署loki日志系统
Realization of basic function of sequence table
随机推荐
ARP and ARP Spoofing
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
群辉 NAS 配置 iSCSI 存储
Minecraft空岛服开服
Linux安装Oracle Database 19c
[blackmail virus data recovery] suffix Rook3 blackmail virus
Sqli labs (post type injection)
整理秒杀系统的面试必备!!!
Use C language to receive JSON strings
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
程序猿学英语-Learning C
Tcp/ip - transport layer
HackTheBox-Gunship
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
Rotating linked list (illustration)
Call Stack
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
DWORD ptr[]
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it