begintrans返回值_PHP: PDO::beginTransaction - Manual

// If you need to set an ISOLATION level or LOCK MODE it needs to be done BEFORE you make the BeginTransaction() call...

//

//  **note** you should always check result codes on operations and do error handling.  This sample code

//  assumes all the calls work so that the order of operations is accurate and easy to see

//

//  THIS IS using the PECL PDO::INFORMIX module, running on fedora core 6, php 5.2.4

//

//    This is the correct way to address an informix -243 error (could not position within table) when there

//    is no ISAM error indicating a table corruption.  A -243 can happen (if the table/indexes, etc., are ok)

//    if a row is locked.  The code below sets the LOCK MODE to wait 2 minutes (120 seconds) before

//    giving up.  In this example you get READ COMMITTED rows, if you don't need read committed

//    but just need to get whatever data is there (ignoring locked rows, etc.) instead of

//    "SET LOCK MODE TO WAIT 120" you could "SET ISOLATION TO DIRTY READ".

//

//    In informix you *must* manage how you do reads because it is very easy to trigger a

//    lock table overflow (which downs the instance) if you have lots of rows, are using joins

//    and have many updates happening.

//

// e.g.,

$sql= "SELECT FIRST 50 * FROM mytable WHERE mystuff=1 ORDER BY myid";                    /* define SQL query */

try                                                                                /* create an exception handler */

{

$dbh = new PDO("informix:host=......");

if ($dbh)    /* did we connect? */

{

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$dbh->query("SET LOCK MODE TO WAIT 120")

# ----------------

# open transaction cursor

# ----------------

if    ( $dbh->beginTransaction() )                                         # explicitly open cursor

{

try    /* open exception handler */

{

$stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT))

{

$data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t" . $row[3] . "\t" . $row[4] . "\t" . $row[5] . "\t" . $row[6] . "\t" . $row[7] . "\n" . $row[8] ;

//print $data;

print_r($row);

};

$stmt = null;

}

catch (PDOException $e)

{

print "Query Failed!\n\n";

print "DBA FAIL:" . $e->getMessage();

};

$dbh->rollback();                                                       # abort any changes (ie. $dbh->commit()

$dbh = null;                                                            # close connection

}

else

{

# we should never get here, it should go to the exception handler

print "Unable to establish connection...\n\n";

};

};

}

catch (Exception $e)

{

$dbh->rollback();

echo "Failed: " . $e->getMessage();

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值