当前位置:网站首页>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)
边栏推荐
- ICMP Protocol
- Sqli labs level 12
- IP protocol and IP address
- 程序猿学英语-Learning C
- Solid principle: explanation and examples
- Matlab-其它
- File upload and download performance test based on the locust framework
- In depth understanding of prototype drawings
- C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
- PCL calculates the intersection of three mutually nonparallel planes
猜你喜欢
Web security -- core defense mechanism
Openshift deployment application
zipkin 简单使用
Openfeign is easy to use
Web安全--核心防御机制
Nacos download, start and configure MySQL database
Minecraft空岛服开服
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
Data asset management function
Minecraft群組服開服
随机推荐
Installation and use of simple packaging tools
OpenShift构建镜像
c语言将字符串中的空格替换成%20
HCIA—数据链路层
Rotating linked list (illustration)
Force deduction method summary: find classes
Analysis of the use of comparable, comparator and clonable interfaces
How to build the alliance chain? How much is the development of the alliance chain
Pointer initialization
Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share
ICMP Protocol
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
Sentinel easy to use
顺序表基本功能函数的实现
Qunhui NAS configuring iSCSI storage
Flex layout
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
Use C language to receive JSON strings
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)