原生php 配合异常 处理事务
来源:互联网 发布:二维数组定义 编辑:程序博客网 时间:2024/06/04 20:08
mysql_connect('localhost','root','pwd');
mysql_select_db('cms');
mysql_query('start transaction');
mysql_query('SET autocommit=0');
try{
mysql_query('update cms_users set user_money = user_money - 10 where user_id = 43');
if(mysql_affected_rows()){
echo '转出成功'.'<br>';
}else{
throw new UpdateException('转出失败<br>');
}
mysql_query('update cms_users set user_money = user_money + 10 where user_id = 6');
if(mysql_affected_rows()){
echo '转入成功'.'<br>';
}else{
throw new UpdateException('转入失败<br>');
}
echo '交易成功';
}catch(UpdateException $e){
try{
echo $e->getMessage();
$e->ReExcute();
echo '交易成功';
}catch(UpdateException $e){
echo $e->getMessage();
mysql_query('rollback');
}
}catch(Exception $e){
echo $e->getMessage().'交易失败';
mysql_query('rollback');
}
mysql_query('commit');
mysql_query('SET autocommit=1');
/**
* 数据库更新异常类
*/
class UpdateException extends Exception
{
function __construct($message)
{
parent::__construct($message);
}
public function ReExcute(){
mysql_query('update cms_users set user_money = user_money + 10 where user_id = 44');
echo '重新执行一次转入<br>';
if(mysql_affected_rows()){
echo '转入成功'.'<br>';
}else{
throw new UpdateException('再次转入失败,请确认账户是否正确<br>交易失败!');
}
}
}
mysql_select_db('cms');
mysql_query('start transaction');
mysql_query('SET autocommit=0');
try{
mysql_query('update cms_users set user_money = user_money - 10 where user_id = 43');
if(mysql_affected_rows()){
echo '转出成功'.'<br>';
}else{
throw new UpdateException('转出失败<br>');
}
mysql_query('update cms_users set user_money = user_money + 10 where user_id = 6');
if(mysql_affected_rows()){
echo '转入成功'.'<br>';
}else{
throw new UpdateException('转入失败<br>');
}
echo '交易成功';
}catch(UpdateException $e){
try{
echo $e->getMessage();
$e->ReExcute();
echo '交易成功';
}catch(UpdateException $e){
echo $e->getMessage();
mysql_query('rollback');
}
}catch(Exception $e){
echo $e->getMessage().'交易失败';
mysql_query('rollback');
}
mysql_query('commit');
mysql_query('SET autocommit=1');
/**
* 数据库更新异常类
*/
class UpdateException extends Exception
{
function __construct($message)
{
parent::__construct($message);
}
public function ReExcute(){
mysql_query('update cms_users set user_money = user_money + 10 where user_id = 44');
echo '重新执行一次转入<br>';
if(mysql_affected_rows()){
echo '转入成功'.'<br>';
}else{
throw new UpdateException('再次转入失败,请确认账户是否正确<br>交易失败!');
}
}
}
阅读全文
0 0
- 原生php 配合异常 处理事务
- 事务的异常处理
- Spring 事务异常处理
- php处理mysql事务
- MSSQLSERVER 事务与异常处理
- MySQL 异常处理和事务
- C# 事务异常TransactionScope处理
- struts2全局异常处理及配合log4j异常日志记录
- PHP自定义异常处理
- PHP异常处理
- php异常处理
- PHP异常处理
- PHP异常处理
- PHP 异常处理
- php 异常处理
- PHP异常处理
- php异常处理函数
- PHP 异常处理
- 周中记录--2017.11.23
- 个人编程娱乐小游戏--扫雷
- 双目测量空间中两点距离
- Java学习笔记
- Residual Networks
- 原生php 配合异常 处理事务
- JAVA-任意对象转换成JSON,json转换成指定对象
- C#调用DLL方案收集
- 在有序但含有None的数组中查找字符串 Python 版
- PAT 乙级 1001——1005 C语言
- 深度学习硬件(GPU、FPGA、ASIC、DSP)
- JAVA-使用反射为任意对象生成XML
- (六)java并发编程--synchronized同步块
- jstat命令详解