我的代码:
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()”,上面的警告就消失了。
如果我没有在第 171 行之后添加“$dbh->commit()”,而是调用了“$hfpDbh->disconnect();” 在第 140 行,上述警告也消失了。
我的问题是: 警告意味着有未提交的交易?这就是为什么我需要明确提交或断开连接以修复警告。但是代码中只有 SELECT 操作。我错过了什么?
谢谢。