DBIx::something 可以在重新连接时恢复会话变量?
DBIx::something that can restore session variables on reconnection?
普通 DBI::db 处理程序将丢失使用 $dbh->do('SET variable_name=value').
进行的所有数据库会话设置
是否有任何 DBIx::* class/package 左右提供类似 "set_session" 的方法来设置会话变量并且可以在检测到连接丢失后恢复此变量(90% 的实际连接超时)例)?
它可能看起来像这样:
# inside the user code:
$dbh->set(variable => 'string', yet_another_variable => 42)
# inside the DBIx::* package:
sub reconnect {
# ...
while (my ($var, $val) = each %{$self->saved_vars}) {
$self->dbh->do("SET $var=?", {}, $val)
}
# ...
}
DBI 支持 something called Callbacks。我不能 link 文档的这一点,因为这部分很长,所以这里是逐字的。
A more common application for callbacks is setting connection state
only when a new connection is made (by connect() or connect_cached()).
Adding a callback to the connected method (when using connect) or via
connect_cached.connected (when useing connect_cached()>) makes this
easy. The connected() method is a no-op by default (unless you
subclass the DBI and change it). The DBI calls it to indicate that a
new connection has been made and the connection attributes have all
been set. You can give it a bit of added functionality by applying a
callback to it. For example, to make sure that MySQL understands your
application's ANSI-compliant SQL, set it up like so:
my $dbh = DBI->connect($dsn, $username, $auth, {
Callbacks => {
connected => sub {
shift->do(q{
SET SESSION sql_mode='ansi,strict_trans_tables,no_auto_value_on_zero';
});
return;
},
}
});
我相信这就是您的 use-case。连接后执行此操作而不是 运行 您自己的代码。
普通 DBI::db 处理程序将丢失使用 $dbh->do('SET variable_name=value').
进行的所有数据库会话设置是否有任何 DBIx::* class/package 左右提供类似 "set_session" 的方法来设置会话变量并且可以在检测到连接丢失后恢复此变量(90% 的实际连接超时)例)?
它可能看起来像这样:
# inside the user code:
$dbh->set(variable => 'string', yet_another_variable => 42)
# inside the DBIx::* package:
sub reconnect {
# ...
while (my ($var, $val) = each %{$self->saved_vars}) {
$self->dbh->do("SET $var=?", {}, $val)
}
# ...
}
DBI 支持 something called Callbacks。我不能 link 文档的这一点,因为这部分很长,所以这里是逐字的。
A more common application for callbacks is setting connection state only when a new connection is made (by connect() or connect_cached()). Adding a callback to the connected method (when using connect) or via connect_cached.connected (when useing connect_cached()>) makes this easy. The connected() method is a no-op by default (unless you subclass the DBI and change it). The DBI calls it to indicate that a new connection has been made and the connection attributes have all been set. You can give it a bit of added functionality by applying a callback to it. For example, to make sure that MySQL understands your application's ANSI-compliant SQL, set it up like so:
my $dbh = DBI->connect($dsn, $username, $auth, { Callbacks => { connected => sub { shift->do(q{ SET SESSION sql_mode='ansi,strict_trans_tables,no_auto_value_on_zero'; }); return; }, } });
我相信这就是您的 use-case。连接后执行此操作而不是 运行 您自己的代码。