我应该提交以下代码吗? [英] should I commit in the following code?

查看:87
本文介绍了我应该提交以下代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

122 #
123 my $hfpDbh = undef;
124 unless (
125         $hfpDbh = DBI->connect("dbi:Pg:host=....")#removed something
128 ) {
129         Log( ERROR, "" );
130         Exit( 1 )
131 }
132 $hfpDbh->{RaiseError} = 1;
133 $hfpDbh->{AutoCommit} = 0;
134 
135 ( my $mydata, $msg ) = load_data( $hfpDbh, $DatFile );
136 unless ( defined($mydata) )
137 {
138         Log(INFO, "Calling exit...2");
139 }
140 #$hfpDbh->disconnect();
141 Exit( 0 );
142 Log(INFO, "Calling exit...4");
143 
144 
145 sub load_data
146 {
147         my ( $dbh, $DatFile ) = @_;
148         my $msg = '';
149         unless ( $dbh ) {
150                 $msg = 'cannot load data, no DB handle';
151                 Log( ERROR, $msg );
152         }
153         Log(INFO, "Call load_data...");
154         my $q = "SELECT ip as ip FROM rules WHERE active = 'true' AND isGood = 'true';";
155         my $stmt = undef;
156         unless ( $stmt = $dbh->prepare( $q ) ) {
157                 $msg = "unable to prepare SQL query: $q";
158                 Log( ERROR, $msg );
159         }
160 
161         eval { $stmt->execute() };
162         if ( $@ ) {
163                 $msg = "failed to execute SQL query: $@";
164                 Log( ERROR, $msg );
165         }
166 
167         my $data = {};
168         while ( my $row = $stmt->fetchrow_hashref() ) {
169                 #Log(INFO, "testing row");
170         }
171         $stmt->finish();
172         return $data, $msg;
173 }

警告是:

Issuing rollback() due to DESTROY without explicit disconnect() of DBD::Pg::db handle

如果我在171行之后添加 $ dbh-> commit(),则上述警告消失了。

If I add "$dbh->commit()" after Line 171, the above warn disappeared.

如果我没有在第171行之后添加 $ dbh-> commit(),而是调用了 $ hfpDbh-> disconnect();。在第140行中,上述警告也消失了。

If I did not add "$dbh->commit()" after Line 171 but called "$hfpDbh->disconnect();" in Line 140, the above warn disappeared too.

我的问题是:
警告是否表示存在未提交的交易?这就是为什么我需要显式提交或断开连接以修复警告的原因。但是代码中只有SELECT操作。我缺少什么?

谢谢。

推荐答案


  • 由于您根本没有修改数据库,因此不需要启用事务,因此请勿设置 AutoCommit 归零。这样,也无需在任何地方调用 commit ,并且当句柄脱离dcope时,数据库将自动断开连接。

    • Since you aren't modifying the database at all, you don't need to enable transactions, so don't set AutoCommit to zero. That way there's no need to call commit anywhere either, and the database will be disconnected automatically when the handle goes out of dcope

      由于您自己正在处理错误,因此不应将 RaiseError 设置为1。如果发生任何错误,您的程序将立即死亡,而您自己的处理代码将不会执行

      Since you're handling errors yourself you shouldn't set RaiseError to 1. That will cause your program to die immediately if any error occurs and your own handling code won't get executed

      无需调用完成。它不会在这里造成任何伤害,但也没有意义,几乎永远都没有必要

      There's no need to call finish. It won't do any harm here, but it's also pointless and should almost never be necessary

      这篇关于我应该提交以下代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆