When should I close database connection?

blov · · 894 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I build pkg called database inside this pkg function open to open db connection</p> <pre><code>// this function inside database pkg func Open() *sql.DB { return db } </code></pre> <p>when I use <a href="https://golang.org/pkg/database/sql/#DB.Close" rel="nofollow">Close</a> function inside handler function, I found no Result appear after refresh my page because database connection closed.</p> <pre><code>func example(w http.ResponseWriter, *http.Request){ defer database.Open.Close() // do something } </code></pre> <blockquote> <p>It is rarely necessary to close a DB.</p> </blockquote> <p>if i used another database like mongodb or blotdb, What is the best way to close database connection?</p> <hr/>**评论:**<br/><br/>unitedcreatures: <pre><p>You shouldn&#39;t close it really. <code>*sql.DB</code> is a pool of connections, so individual opens/closes are handled internally.</p></pre>seufert: <pre><p>Yep, that should be stated very clearly: sql.DB is NOT a database connection (open does not actually open one, you have for example to Ping) and should never be called a connection.</p></pre>gernest_: <pre><p>As someone here as already pointed out, most of the times you don&#39;t have to worry about closing the connection.</p> <p>All the connections will be closed if the application exits( If that is what you are worried about), and by the way the <code>database/sql</code> manages the connections pool for you.</p></pre>DavidDavidsonsGhost: <pre><p>It really depends on the driver, for example the mgo db driver uses a connection pool, when you call close on the session, it does not close the connection it instead releases the connection to the pool, so in many situations after doing the action is the best solution, in others holding it open forever is the best solution.</p></pre>cihangirsavas: <pre><p>Close it when you are done with your app, in other words while you are exiting from your app - in normal cases that should not be your handler&#39;s responsibility.</p></pre>C0odY: <pre><p>If I have understood correctly, I will use Close() function from main() function.</p></pre>FIuffyRabbit: <pre><p>If you don&#39;t know what you are doing, don&#39;t make a close function. </p></pre>cihangirsavas: <pre><p>No, always close the connections you open after you are done* with them, otherwise you will leak connections and some of the database systems does not close idle connections automatically if not configured to do so. </p> <ul> <li>&#34;done&#34; state can be different per use case and database client, for OP&#39;s case DB conn should be closed in main function - most probably after closing the listener.</li> </ul></pre>FIuffyRabbit: <pre><p>Not really because a lot of them handle it for you. Someone of the ones that rely on a on separate process do need to be though.</p></pre>krzykizza: <pre><p>I&#39;ve had similar dillema some time ago, read on here <a href="http://go-database-sql.org/accessing.html" rel="nofollow">http://go-database-sql.org/accessing.html</a> that you shoud defer close for a connection that has no use beyond a scope of a given function.</p></pre>hayzeus: <pre><p>You don&#39;t normally close an sql.DB connection, as the connection pooling handles closing and reuse.</p> <p>You must close mongo connections, however, which will release them back to the mongo connection pooler. If you don&#39;t close a mongo connection when done, you will leak connections, goroutines, and memory.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

894 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传