为什么80%的码农都做不了架构师?>>>   

1.接上面的事物介绍

/*** IAtom support transaction of database.* It can be invoked in Db.tx(IAtom atom) method.* <br>* Example:<br>* Db.tx(new IAtom(){<br>*        public boolean run() throws SQLException {<br>*           int result1 = Db.update("update account set cash = cash - ? where id = ?", 100, 123);<br>*           int result2 = Db.update("update account set cash = cash + ? where id = ?", 100, 456);<br>*          return result1 == 1 && result2 == 1;<br>*         }});*/

2.Db.tx他的原理呢,我这里详细介绍一下,其实非常简单

/*** Execute transaction.* @param transactionLevel the transaction level* @param atom the atom operation* @return true if transaction executing succeed otherwise false*/public  static boolean tx(int transactionLevel, IAtom atom) {Connection conn = DbKit.getThreadLocalConnection();if (conn != null) {   // Nested transaction supporttry {if (conn.getTransactionIsolation() < transactionLevel)conn.setTransactionIsolation(transactionLevel);boolean result = atom.run();if (result)return true;throw new ActiveRecordException("Nested transaction is failure.");  // important:can not return false}catch (SQLException e) {throw new ActiveRecordException(e);}}Boolean autoCommit = null;try {conn = DbKit.getDataSource().getConnection();autoCommit = conn.getAutoCommit();DbKit.setThreadLocalConnection(conn);conn.setTransactionIsolation(transactionLevel);conn.setAutoCommit(false);boolean result = atom.run();if (result)conn.commit();elseconn.rollback();return result;} catch (Exception e) {if (conn != null)try {conn.rollback();} catch (Exception e1) {e1.printStackTrace();}return false; // throw new ActiveRecordException(e);} finally {try {if (conn != null) {if (autoCommit != null)conn.setAutoCommit(autoCommit);conn.close();}} catch (Exception e) {e.printStackTrace();  // can not throw exception here, otherwise the more important exception in previous catch block can not be thrown} finally {DbKit.removeThreadLocalConnection();    // prevent memory leak}}}

3.跟我上一篇博客基本上差不多,下面就是链接
http://my.oschina.net/skyim/blog/140867
boolean result = atom.run(); 这个地方就是调用我们

int result1 = Db.update("update account set cash = cash - ? where id = ?", 100, 123);
int result2 = Db.update("update account set cash = cash + ? where id = ?", 100, 456);

4.如果返回false就回滚,返回true就提交
下一阶段我会开始写扩展ext的源码,尽情期待

转载于:https://my.oschina.net/skyim/blog/141032

JFinal 源码导读第八天(1) Db.tx 事物相关推荐

  1. JFinal 源码导读第二天(2)configPlugin,configRoute

    为什么80%的码农都做不了架构师?>>>    1.接上面的代码 if (map.containsKey(controllerKey))throw new IllegalArgume ...

  2. 2021年最新以太坊源码导读-p2p架构

    前面部分的内容都是源码导读,可能有的朋友没有耐心把所有代码看完.这里我采用尽可能简单的方式来介绍p2p这部分的代码究竟做了什么. 一.概念篇 p2p是peer-to-peer的缩写,以太坊网络是一个去 ...

  3. 鸿蒙源码导读-01:蓝海与红海

    本文摘录自 OHOZ 团队的 OpenHarmony 源码导读项目,在线阅读(腾讯云.Github Pages)中包含最新的内容. 鸿蒙的蓝海与红海 Arch.SoC.target 环境.源码.编译 ...

  4. java activerecord.db_JFinal 源码超详细解析之DB+ActiveRecord

    我记得以前有人跟我说,"面试的时候要看spring的源码,要看ioc.aop的源码"那为什么要看这些开源框架的源码呢,其实很多人都是"应急式"的去读,就像读一篇 ...

  5. java activerecord.db_JFinal 源码超详细解析之DB+ActiveRecord-java-火龙果软件工程

    我记得以前有人跟我说,"面试的时候要看spring的源码,要看ioc.aop的源码"那为什么要看这些开源框架的源码呢,其实很多人都是"应急式"的去读,就像读一篇 ...

  6. JFinal 源码build脚本

    为什么80%的码农都做不了架构师?>>>    目前 github上jfinal的库暂时没有提供构建脚本,有需要自己编译源码的朋友可以参考我fork的jfinal项目,目前maste ...

  7. Spring IoC 源码导读

    源码记录:spring-framework-5.1.7-source-code-read 文章导读 Spring IoC 源码系列(一)BeanDefinition 初始化与注册 Spring IoC ...

  8. leanote 支持php,Leanote source leanote源码导读

    源码结构 leanote/app/ controllers 控制器 db mongodb通用数据库访问方法, 由service调用 info 数据表的模型和其它数据结构 lea 通用方法 servic ...

  9. Unreal Virtual Texture 源码导读

    上一篇<浅谈Virtual Texture>主要是对理论知识的介绍,本篇开始对Unreal Virtual Texture的源码做一个导读. 内容包括Virtual Texture的流程和 ...

最新文章

  1. 一人一天发两篇Science,配视频揭秘:植物如何在与病菌的斗争中取胜?
  2. 电机的入门之路系列3--直流电机的工作原理
  3. Jarvis Oj Pwn 学习笔记-level3
  4. 为什么有的人开车舍不得开空调?车载空调耗油吗?
  5. 阿里拟 20 亿美元收购网易考拉;联通 5G 套餐最低 190 元;Rust 1.37.0 发布 | 极客头条...
  6. 一阶差分单位根检验_计量经济学第11讲(时间序列计量经济学模型:平稳性及其检验)...
  7. 解决Eclipse报错:the file dx.jar was not loaded from the SDK
  8. matlab帕累托分布函数,matlab 进行广义的帕累托参数估计
  9. Undefined symbols for architecture x86_64:
  10. Java实现简单计算器
  11. cefsharp设置cookie_CefSharp 设置cookie
  12. hive 计算周几_HIVE 计算指定日期本周的第一天和最后一天
  13. SwiftUI 高级用法之ForEach如何使用 enumerated() UserDefaults.standard(教程含源码)
  14. G6 3.1 线条的属性
  15. 下列python语句的输出结果是_下列Python语句的输出结果是 __________ 。 print(数量%4d,单价%3.3f %(100,285.6)) (3.0分)_学小易找答案...
  16. VS2015 LoadLibrary加载DLL失败的解决方案,GetLastError()返回值193
  17. Android开发者指南-Manifest.xml-uses-feature
  18. Transformer课程 业务对话机器人Rasa 3.x Reaching Out to the User
  19. 【PPT】《使用ChatGPT一键制作带自动配图的PPT演讲稿》- 知识点目录
  20. 树莓派GPIO针脚在python中BCM与BOARD模式

热门文章

  1. 分布式之消息队列复习精讲
  2. 北航学长:DCIC 2021的算法方案讲解
  3. GNN教程:DGL框架实现GCN算法!
  4. Datawhale专访 | 周涛:从窄门进最终走出宽路来
  5. 谷歌提出「卷积+注意力」新模型,超越ResNet最强变体!
  6. 超全Python IDE武器库大总结,优缺点一目了然!
  7. 那个14岁上大学、17岁读博、24岁成教授的天才神童,如今怎样了?
  8. 16年前卖猪肉的北大高材生如今怎么样了?
  9. 树、森林与二叉树的转换
  10. Pytorch张量tensor的使用