Home  >  Q&A  >  body text

java编程思想读书笔记 - Java编程思想习题请教: 使用动态代理来编写一个系统以实现事务?

大家好,小弟最近在学习《Java编程思想》第四版,学习到第十四章《类型信息》中动态代理的时候,在习题23后遇到一个问题,使用动态编写一个系统以实现事务,不知道该如何解决,特来请教大家,题目如下:

使用动态代理来编写一个系统以实现事务,其中,代理在被代理的调用执行成功(不抛出任何异常)执行提交,而在执行失败时执行回滚。你的提交和回滚都针对一个外部的文本文件,该文件不在Java异常的控制范围之内。你必须注意操作的原子性。

该题目英文原文如下:

谢谢大家。

高洛峰高洛峰2744 days ago611

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-18 10:50:51

    I think the difficulty of transaction processing lies in the "recovery" part. To be able to recover, the scene must be saved, or effective reverse operations must be provided. Of course, reverse operations usually belong to the business category. Since dynamic proxy implementation is required here, state recovery should be relatively appropriate.

    For a file operation, the simplest and crudest way to save the status is to save the file backup. So I think this dynamic proxy is like this

    1. Save a backup of the file you want to operate

    2. Call the proxied code to perform its own business operations (while monitoring exceptions)

    3. If an exception occurs, restore the file backup, otherwise ignore this step

    4. Delete File Backup

    Let’s see if my answer can give you an idea

    reply
    0
  • Cancelreply