Store提交和回滚

Store的提交和回滚类似于数据库中的commit、rollback,当客户端修改了一条Record的属性之后,这条Record记录和后台数据库中的实际数据就不一致了,此时的Record对象就成了"脏数据"

commitChanges():void

Commit all Records with outstanding changes. To handle updates for changes, subscribe to the Store's update event, and perform updating when the third parameter is Ext.data.Record.COMMIT.
Parameters:
  • None.
Returns:
  • void

rejectChanges():void

Rejectoutstanding changes on all modified records.
Parameters:
  • None.
Returns:
  • void

上面是将Store中的所有记录都进行提交或回滚,而有时我们只需要回滚或提交某条记录,就会使用到Ext.data.Record

commit([Booleansilent]):void

Usually called by the Ext.data.Storewhich owns the Record. Commits all changes made to the Record since either creation, or the last commit operation.

Developers should subscribe to theExt.data.Store.updateevent to have their code notified of commit operations.

Parameters:
  • silent: Boolean
    (optional) True to skip notification of the owning store of the change (defaults to false)
Returns:
  • void

reject([Booleansilent]):void

Usually called by the Ext.data.Storewhich owns the Record. Rejects all changes made to the Record since either creation, or the last commit operation. Modified fields are reverted to their original values.

Developers should subscribe to theExt.data.Store.updateevent to have their code notified of reject operations.

Parameters:
  • silent: Boolean
    (optional) True to skip notification of the owning store of the change (defaults to false)
Returns:
  • void
就仅仅是提交某条记录。


### 添加事务后如何使缓存回滚 在分布式系统中,当引入事务管理时,确保数据一致性完整性至关重要。特别是在涉及多个资源(如数据库缓存)的情况下,如果其中一个操作失败,则整个事务应被撤销以保持一致性。 对于缓存层,在实现其与事务同步方面存在多种策略: #### 使用事件驱动机制 一种常见方法是在应用程序逻辑层面监听特定业务对象的变化,并通过发布/订阅模式通知其他组件执行相应动作。例如,每当检测到事务提交回滚时触发相应的处理程序来更新或清除关联的缓存条目[^1]。 ```java @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK) public void handleCacheEvictionOnRollback(TransactionFailedEvent event) { // 清除受影响的数据项对应的缓存键值对 } ``` #### 配置声明式事务支持 利用框架提供的功能简化开发工作量。比如Spring提供了强大的声明式编程模型用于定义跨服务边界的ACID属性。可以配置`@Transactional`注解指定哪些方法应该参与全局协调过程并自动传播给下游调用者[^2]。 ```xml <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> </bean> ``` #### 编程式控制本地化存储单元 除了依赖外部中间件外还可以考虑直接操纵内存中的临时副本作为一级缓冲区。这样即使发生异常也能及时恢复原始状态而不会影响持久化记录的真实版本[^3]。 ```python import weakref class CacheProxy: _cache_store = {} @classmethod def get(cls, key): return cls._cache_store.get(key) @classmethod def set(cls, key, value): cls._cache_store[key] = value def perform_operation_with_cache(): try: # 执行可能抛出异常的操作... # 更新成功则保存更改至永久介质之前先暂存于代理类内部字典结构里 CacheProxy.set('example_key', 'new_value') commit_changes() # 假设这是个会引发实际写入磁盘等I/O行为的方法 except Exception as e: del CacheProxy._cache_store['example_key'] # 发生错误立即删除未完成的任务所占用的空间位置 raise e ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值