当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - postgresql connection and setting autocommit off

问题描述:

I am programming in perl and have an existing connection (progammatically) with my postgres database. After the connection is established, I do not control the connection code, I want to set autocommit to off.

How does one do that? When I do it interactively, I do not see any changes. I \set autocommit off and then do an insert and I see the change. I do a rollback, I get there is no transaction exists.

I want to set autocommit off, do my work and then commit. How does one accomplish that with an existing connection?

Thanks

Reza

网友答案:

You should not disable autocommit in a middle of connection, as it is unexpected and error prone.

The best is to explicitly start and end transactions using something like this:

$dbh->begin_work();
foreach($statement1, $statement2, $statement3) {
  $dbh->execute($_)
    or do { $dbh.rollback(); return -1 }
}
$dbh->commit();

begin_work() will temporarily turn autocommit off, until next commit or rollback.

But if you think you really need to hurt yourself, then perl won't stop you:

$dbh->{AutoCommit} = 0;
分享给朋友:
您可能感兴趣的文章:
随机阅读: