Golang – 多个正在使用的连接

huangapple go评论121阅读模式
英文:

Golang - many in use connections

问题

我正在尝试理解最大连接数是如何工作的。基本上,我有这个数据库配置:

params.MinSessions = 5
params.MaxSessions = 6
params.SessionTimeout = 0
params.WaitTimeout = 5 * time.Second
params.SessionIncrement = 0
params.ConnClass = "GOLANGPOOL"

// 连接!
result, err := sql.Open("godror", params.StringWithPassword())
result.SetMaxIdleConns(0)

然而,我可以看到使用sql.DB.Stats有242个连接:

DB Established Open Conn (use + idle): 242
DB Idle Conn: 0
DB In Use Conn: 242
DB Max Idle Closed: 766
DB Max Idle Time Closed: 0
DB Max Lifetime Closed: 0
DB Max Open Conn: 0
DB Wait Count: 0
DB Wait Duration (sec): 0

这是怎么可能的?限制不应该是6吗?

谢谢

英文:

#golang #oracle

Im trying to understand how the Max connection works. Basically I have this db configuration:

params.MinSessions = 5
params.MaxSessions = 6
params.SessionTimeout = 0
params.WaitTimeout = 5 * time.Second
params.SessionIncrement = 0
params.ConnClass = "GOLANGPOOL"

// Connect!
result, err := sql.Open("godror", params.StringWithPassword())
result.SetMaxIdleConns(0)

However I can see 242 connections using sql.DB.Stats:

DB Established Open Conn (use + idle): 242
DB Idle Conn: 0
DB In Use Conn: 242
DB Max Idle Closed: 766
DB Max Idle Time Closed: 0
DB Max Lifetime Closed: 0
DB Max Open Conn: 0
DB Wait Count: 0
DB Wait Duration (sec): 0

How is it possible? The limit shouldn't be 6?

Thanks

答案1

得分: 1

在Oracle中,连接(connection)和会话(session)是不同的概念。

连接是与数据库的网络连接,而会话是用户与数据库交互的封装。

参考这本书,以及https://stackoverflow.com/questions/1039637/relation-between-oracle-session-and-connection-pool。

英文:

In Oracle connections and sessions are different concepts.

> A connection is a network connection to the DB, while a session is an
> encapsulation of a user's interaction with the DB...

refering to this book, and https://stackoverflow.com/questions/1039637/relation-between-oracle-session-and-connection-pool.

答案2

得分: 1

假设您正在使用最新版本的驱动程序,https://github.com/godror/godror

sql.Open("godror", params.StringWithPassword())

这意味着设置了standaloneConnection=0。

您所看到的统计信息来自go sql连接池。go sql调用驱动程序的连接方法,该方法再尝试从另一个池中获取连接(OCI由于设置standaloneConnection=0而维护该池)。

最大出站连接数没有超过params.MaxSessions,但只是go sql连接计数器numOpen等。

理想情况下,您应该将go sql池设置调整得更接近另一个池的值,以便go例程不会被阻塞。

您可以使用godror.Conn的GetPoolStats()方法检查OCI池的统计信息,并确认实际的最大出站连接数。示例在这里https://github.com/godror/godror/blob/main/z_test.go。

英文:

Assuming you are using latest version of driver, https://github.com/godror/godror

sql.Open("godror", params.StringWithPassword()) 

implies standaloneConnection=0 setting.

The stats you are seeing is from go sql connection pool. The go sql calls the driver connect method which inturn tries to get connection from another pool (OCI maintains it due to setting standaloneConnection=0 ).

The max outbound connections hasn't exceeded the params.MaxSessions but its just the go sql connection counter

numOpen, ....

It is ideal you tune the go sql pool settings closer to another pool values so that the go routines just don't block.

You can check the OCI pool stats by using GetPoolStats()
method from godror.Conn and confirm the real number of maximum outbound connections. example here

https://github.com/godror/godror/blob/main/z_test.go

答案3

得分: 0

> 正在使用的数据库连接数:242
> 最大空闲关闭的数据库连接数:766

这个总数接近1000,就像这个默认值一样

> poolMaxSessions=1000

我认为你并没有同时使用242个连接。你有一个连接池,数据库会限制同时会话的数量。

你应该检查一下sql包是如何处理的(它是开源的!),以及具体的驱动程序是如何处理的(也是开源的!),如果有必要,在驱动程序项目上提出一个问题

https://github.com/godror/godror

英文:

> DB In Use Conn: 242
> DB Max Idle Closed: 766

The sum is almost 1000, like the value of this default

> poolMaxSessions=1000

I think you don’t have 242 simultaneous connections in use. You have a pull of connections and the database will limit the number of simultaneous sessions.

You should check how the sql package handles it (it is open source!) and how the specific driver handles it (also open source!) and if necessary open an issue on the driver project

https://github.com/godror/godror

huangapple
  • 本文由 发表于 2022年3月26日 05:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/71623245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定