知识库--Creating Nested Transactions By Akka 内存嵌套事务(129)
2017-03-02 08:28
399 查看
public class Account { final private Ref<Integer> balance = new Ref<Integer>(); public Account(int initialBalance) { balance.swap(initialBalance);//自带事务 } public int getBalance() { return balance.get();//自带事务 }
存款操作必须在一个事务中完成
原因:涉及到账户的读和写操作
public void deposit(final int amount) { new Atomic<Boolean>() { public Boolean atomically() { System.out.println("Deposit " + amount); if (amount > 0) { balance.swap(balance.get() + amount);//外层事务保证 return true; } throw new AccountOperationFailedException(); } }.execute(); }
历史:使用锁进行事务化
public boolean transfer( final Account from, final Account to, final int amount) throws LockException, InterruptedException { final Account[] accounts = new Account[] {from, to}; Arrays.sort(accounts); if(accounts[0].monitor.tryLock(1, TimeUnit.SECONDS)) { try { if (accounts[1].monitor.tryLock(1, TimeUnit.SECONDS)) { try { if(from.withdraw(amount)) { to.deposit(amount); return true; } else { return false; } } finally { accounts[1].monitor.unlock(); } } } finally { accounts[0].monitor.unlock(); } } throw new LockException("Unable to acquire locks on the accounts"); }
now:使用STM–akka
public void transfer( final Account from, final Account to, final int amount) { new Atomic<Boolean>() { public Boolean atomically() { to.deposit(amount); from.withdraw(amount); return true; } }.execute(); }
相关文章推荐
- 知识库--Creating Actors By Akka Using Java(139)
- 知识库--Akka 事务配置-using jvm(134)
- 知识库--Sending and Receiving Messages By Akka Using Java(139)
- 脱离 Spring 实现复杂嵌套事务,之四(NESTED - 嵌套事务)
- spring 嵌套事务(Nested Transaction) 和新建事务测试
- 知识库--Using Akka Refs and Transactions(124)
- 知识库--The Write Skew Anomaly Fixed By Akka(136)
- akka学习教程(九) STM软件事务内存
- Oracle嵌套事务(Nested Transaction)与自治事务(Autonomous Transaction)详解
- 知识库--STM内存事务简介(118)
- 知识库--Creating Transaction in Java Using Akka(124)
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十三:用Spring嵌套事务(Nested Transaction) 回滚并返回自定义Exception
- spring的 TransactionDefinition.PROPAGATION_NESTED 嵌套事务 应用说明
- Creating a 10gr2 Data Guard Physical Standby database with Real-Time apply [ID 343424.1]
- 一个网上找到的,在Grid中嵌套Grid的示例:Nested Grids Example
- 1、步步为营VS 2008 + .NET 3.5(1) - VS 2008新特性之Multi Targeting(多定向)、Web Designer and CSS(集成了CSS的web设计器)和Nested Master Page(嵌套母版页)
- PostgreSQL事务特性之嵌套事务
- 91.Oracle数据库SQL开发之 修改表内存——数据库事务的并发事务
- NestedScrollView嵌套ListView,RecyclerView
- Haswell 事务内存相关的一些资源介绍