设置了连接池参数后,会导致连接用完无法释放 #1834

Closed
opened 2020-11-25 03:23:19 +00:00 by zibuyu28 · 8 comments

xorm版本 xorm.io/xorm v1.0.1
Engine 增加了设置

	Engine.SetMaxIdleConns(50)
	Engine.SetMaxOpenConns(60)
	Engine.SetLogLevel(log.LOG_DEBUG)
	Engine.SetLogger(log.NewSimpleLogger(f))
	Engine.ShowSQL(true)

一开始运行都是很正常的,但是在运行了大概4到5个小时之后会将连接用完,并且无法释放,打印出的db.state()是这样的,可以看到很多的请求处于等待中

"db state":{"MaxOpenConnections":60,"OpenConnections":60,"InUse":60,"Idle":0,"WaitCount":81803,"WaitDuration":7248022182901,"MaxIdleClosed":4826,"MaxLifetimeClosed":0}
xorm版本 xorm.io/xorm v1.0.1 Engine 增加了设置 ```golang Engine.SetMaxIdleConns(50) Engine.SetMaxOpenConns(60) Engine.SetLogLevel(log.LOG_DEBUG) Engine.SetLogger(log.NewSimpleLogger(f)) Engine.ShowSQL(true) ``` 一开始运行都是很正常的,但是在运行了大概4到5个小时之后会将连接用完,并且无法释放,打印出的db.state()是这样的,可以看到很多的请求处于等待中 ```text "db state":{"MaxOpenConnections":60,"OpenConnections":60,"InUse":60,"Idle":0,"WaitCount":81803,"WaitDuration":7248022182901,"MaxIdleClosed":4826,"MaxLifetimeClosed":0} ```
Owner

You can set MaxLifeTime so that the connection will be closed when idle.

You can set MaxLifeTime so that the connection will be closed when idle.
Author

You can set MaxLifeTime so that the connection will be closed when idle.

设置了没有用,而且我看了源码,MaxLifeTime 这个参数是当连接闲置的时候才会生效,当所有的连接都是处于inUse状态是不生效的。开始用的时候我也设置了MaxLifeTime这个参数,一样出现这个问题,然后才改成现在的样子,而且不设置 MaxLifeTime 的话,默认的时间是1s,这个应该不会影响

> You can set MaxLifeTime so that the connection will be closed when idle. 设置了没有用,而且我看了源码,MaxLifeTime 这个参数是当连接闲置的时候才会生效,当所有的连接都是处于inUse状态是不生效的。开始用的时候我也设置了MaxLifeTime这个参数,一样出现这个问题,然后才改成现在的样子,而且不设置 MaxLifeTime 的话,默认的时间是1s,这个应该不会影响
Author

You can set MaxLifeTime so that the connection will be closed when idle.

而且现在的连接主要都是处于 inUse 状态,导致无法新建连接,我看了mysql的processlist,发现并没有连接

> You can set MaxLifeTime so that the connection will be closed when idle. 而且现在的连接主要都是处于 inUse 状态,导致无法新建连接,我看了mysql的processlist,发现并没有连接
Author

image
我在每次sql查询的时候都增加了db的state状态的查询,发现inUse会在某一个时间内直线上升并且,waitCount数量也会增加,而且后面inUse就不会释放了

![image](/attachments/61b73b48-01c9-4ee2-ac83-78dda143e84b) 我在每次sql查询的时候都增加了db的state状态的查询,发现inUse会在某一个时间内直线上升并且,waitCount数量也会增加,而且后面inUse就不会释放了
532 KiB
Owner

If you create with sess := engine.NewSession, don't forget to defer sess.Close().

If you create with `sess := engine.NewSession`, don't forget to `defer sess.Close()`.
Author

If you create with sess := engine.NewSession, don't forget to defer sess.Close().

我们所有的调用全部放在transaction里面的,类似这样
image

> If you create with `sess := engine.NewSession`, don't forget to `defer sess.Close()`. 我们所有的调用全部放在transaction里面的,类似这样 ![image](/attachments/dfcd520b-da94-434b-83b0-6ff0e291c4d9)
193 KiB
Owner

You should use session if you are in a Transaction function.

You should use `session` if you are in a `Transaction` function.
Author

You should use session if you are in a Transaction function.

哦哦哦 明白了,是我没用对框架,我先把这个改过来,然后再观察观察,感谢感谢,也十分抱歉

> You should use `session` if you are in a `Transaction` function. 哦哦哦 明白了,是我没用对框架,我先把这个改过来,然后再观察观察,感谢感谢,也十分抱歉
lunny closed this issue 2021-06-06 13:02:30 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1834
No description provided.