[英]Go database/sql - Issue Commands on Reconnect
I have a small application written in Go that connects to a PostgreSQL database on another server, utilizing database/sql
and lib/pq
. 我有一个用Go编写的小应用程序,它使用
database/sql
和lib/pq
连接到另一台服务器上的PostgreSQL数据库。 When I start the application, it goes through and establishes that all the database tables and indexes exist. 当我启动应用程序时,它会通过并确定所有数据库表和索引都存在。 As part of this process, it issues a
SET search_path TO preferredschema,public
command. 作为此过程的一部分,它会发出
SET search_path TO preferredschema,public
命令。 Then, for the remainder of the database access, I do not have to specify the schema. 然后,对于数据库访问的其余部分,我不必指定架构。
From what I've determined from debugging it, when database/sql
reconnects (no network is perfect) , the application begins failing because the search path isn't set. 从我通过调试确定的,当
database/sql
重新连接(没有网络是完美的)时 ,应用程序开始失败,因为没有设置搜索路径。 Is there a way to specify commands that should be executed when it reconnects? 有没有办法指定重新连接时应该执行的命令? I've searched for an event that might be able to be leveraged, but have come up empty so far.
我已经搜索过一个可能被利用的事件,但到目前为止已经空了。
Thanks! 谢谢!
From the fine manual : 从精细手册 :
Connection String Parameters
连接字符串参数
[...][...]
In addition to the parameters listed above, any run-time parameter that can be set at backend start time can be set in the connection string.除了上面列出的参数之外,还可以在连接字符串中设置可在后端开始时间设置的任何运行时参数。 For more information, see http://www.postgresql.org/docs/current/static/runtime-config.html .
有关更多信息,请参阅http://www.postgresql.org/docs/current/static/runtime-config.html 。
Then if we go over to the PostgreSQL documentation , you'll see various ways of setting connection parameters such as config files, SET
commands , command line switches, ... 然后,如果我们转到PostgreSQL文档 ,您将看到设置连接参数的各种方法,例如配置文件,
SET
命令 ,命令行开关,......
While the desired behavior isn't exactly spelled out, it is suggested that you can put anything you'd SET
right into the connection string: 虽然没有准确说明所需的行为,但建议您可以将任何您
SET
放入连接字符串中:
connStr := "dbname=... user=... search_path=preferredschema,public"
// -----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and since that's all there is for configuring the connection, it should be used for every connection (including reconnects). 由于这是配置连接的全部内容,因此应该用于每个连接(包括重新连接)。
The Connection String Parameters section of the pq documentation also tells you how to quote and escape things if whatever preferredschema
really is needs it or if you have to grab a value at runtime and add it to the connection string. pq文档的“ 连接字符串参数”部分还告诉您如何引用和转义事物,无论
preferredschema
真正需要它,或者如果必须在运行时获取值并将其添加到连接字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.