当前位置:网站首页>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)
边栏推荐
- OpenFeign 简单使用
- OpenShift构建镜像
- Makefile基本原理
- Sentinel easy to use
- Installation and use of simple packaging tools
- Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share
- Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
- 2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
- Shortcut key to comment code and cancel code in idea
- ICMP Protocol
猜你喜欢
随机推荐
Short video with goods source code, double-click to zoom in when watching the video
Kubedm deploys kubernetes v1.23.5 cluster
Introduction to anti interception technology of wechat domain name
Intelligent manufacturing solutions digital twin smart factory
Don't know mock test yet? An article to familiarize you with mock
Detailed explanation of NIN network
Minecraft空岛服开服
Mutex
STM32-新建工程(参考正点原子)
c语言将字符串中的空格替换成%20
【无标题】
Openfeign facile à utiliser
Valin cable: BI application promotes enterprise digital transformation
DWORD ptr[]
Pointer initialization
Causes of laptop jam
OpenShift构建镜像
OpenFeign 簡單使用
ICMP协议
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it









![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)