should I commit in the following code?

最后都变了- 提交于 2019-12-14 04:16:45

问题


My code:

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 }

The warning is:

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

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

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

My question is: The warning means that there are uncommitted transactions? That is why I need to commit or disconnect explicitly to fix the warning. But there is only SELECT operation in the code. What I am missing?

Thanks.


回答1:


  • 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

  • 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




回答2:


The warning means that there are uncommitted transactions?

There is an uncommitted transaction since you requested for transactions to be used, but the warning actually notifies you that a rollback was performed implicitly. It tells you this because this may result in a loss of information. Obviously, it won't result in a loss of information in this case, but the check isn't smart enough of realize this.

What I am missing?

$hfpDbh->disconnect(); or $hfpDbh->rollback();



来源:https://stackoverflow.com/questions/35158587/should-i-commit-in-the-following-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!