Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大佬 为什么你数据库连接都不关闭的? #18

Closed
luodaozhang opened this issue Oct 24, 2018 · 3 comments
Closed

大佬 为什么你数据库连接都不关闭的? #18

luodaozhang opened this issue Oct 24, 2018 · 3 comments

Comments

@luodaozhang
Copy link

emm 都不关闭数据库连接的吗

@iflamed
Copy link

iflamed commented Nov 11, 2018

看了一下 CloseDB 没有调用

@eddycjy
Copy link
Owner

eddycjy commented Dec 16, 2018

@luodaozhang @iflamed 首先需要有个基础概念,如下:

sql.Open() 实际上不建立与数据库的任何连接。也不验证驱动程序连接参数。相反,它只是准备数据库抽象以供以后使用。当第一次需要时,将懒惰地建立与底层数据库实例的第一个实际连接


在本项目中,我们将 sql.DB 视为一个长期使用的包全局对象,它是一个连接池。不需要频繁 Open()Close(),需要的是为不同的数据库请求返回不同的连接句柄

而每次都 defer db.Close() 的行为更适合当该连接句柄已超出其生存函数时,应当关闭

那么显然,用法上就不一样了

@Devying
Copy link

Devying commented Dec 17, 2018

使用的是长链接。只有程序终止的时候才调用close。

@eddycjy eddycjy closed this as completed Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants