简体   繁体   English

使用带有Go上下文作为参数的SQL函数是否是一种好习惯?

[英]Is it a good practice to use SQL functions with Go context as parameter?

I have RESTFUL web service in Golang and I'm using it's SQL driver for database. 我在Golang中拥有RESTFUL Web服务,并且正在使用SQL的数据库驱动程序。 my question is should I use SQL functions with context as parameter or without it? 我的问题是我应该使用带有上下文作为参数的SQL函数还是不使用它? For instance, there are both QueryContext and Query methods. 例如,同时存在QueryContextQuery方法。

Actually I do know what is the context in Go, I just don't know why I should send it to the database methods. 实际上,我确实知道Go中的上下文是什么,我只是不知道为什么要将其发送到数据库方法。

Context aware methods allow for cancellable query execution. Context感知方法允许可取消的查询执行。

The way the cancellation is handled is driver specific, but even if the driver is not directly supports taking the context any query that was started with a context should be closed when the context is closed. 处理取消的方式是特定于驱动程序的,但是即使驱动程序不直接支持获取上下文,在关闭上下文时也应关闭以上下文启动的任何查询。

This should help with potential connection leaks and query specific timeouts. 这应该有助于避免潜在的连接泄漏和查询特定的超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM