Java 中类似事务的编程
我想用java实现类似事务的功能。我想要执行“n”操作,例如:一项更新数据库的操作、一项插入队列的操作、一项更新另一数据结构的操作等,所有这些操作都应表现为一个事务,即,如果成功,则所有操作都应该成功完成,否则,如果其中一个失败,则一切都应该失败。一种强力方法是编写 try-catch 块并恢复 catch 块中的所有操作。有解决此类问题的建议吗?有没有任何模式或库可以实现这一目标?
I want to achieve transaction like functionality in java. I want to do `n' operations like- one operation to update database, one to insert in a queue, one operation to update another data structure etc., All these operations should behave as one transaction, i.e., if it succeeds, all the operations should be successfully done, otherwise, if one fails, everything should fail. One of the brute force approach is to write try-catch blocks and revert all the operations in catch blocks. Any pointers in solving these type of problems? Is there any pattern or library for achieving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找的模式是 Command。
I think the pattern you are looking for is Command.
我结合了命令设计模式和复合设计模式来完成此任务。 Transaction 类是抽象的,包含 begin() 和 begin() 。 rollback() 方法。 CompositeTransaction 派生自 Transaction,并存储 Transaction 对象的列表。对于需要被视为原子事务的每组操作,创建 CompositeTransaction 的子类并向其中添加事务类。请参阅此处的 CompositeTransaction:
http://hillside.net/plop/plop99/proceedings/盛大/plop_99_transaction_patterns.pdf
I've done this with a combination of the Command and Composite Design Patterns. The Transaction class is abstract and contains begin() & rollback() methods. The CompositeTransaction is derived from Transaction and stores a list of Transaction objects. For each group of operations that need to be treated as an atomic transaction, create a subclass of the CompositeTransaction and add your Transaction classes to this. see the CompositeTransaction here:
http://hillside.net/plop/plop99/proceedings/grand/plop_99_transaction_patterns.pdf
访客模式效果很好。
此外,您需要确保在正确的时间发出提交。如果您等到插入/更新集完成,然后发出提交,您所描述的行为应该是自动的。
听起来也许您需要对 sql 语句类进行一些重构,以确保您可以在没有隐含提交的情况下发出一些语句。
a visitor pattern works well.
also, you will need to make sure you issue the commit at the right time. if you wait until the set of inserts/updates in complete, then issue commit, your described behavior should be automatic.
sounds like maybe you need a little refactor of your sql staement class to make sure you can issue some statements without the implied commit.
这只是关于如何实现这一目标的逻辑。
根据需要为每笔交易编写一个方法。也许它会拥有所有的资源。像jdbc事务会有Connection对象和查询作为要求,文件操作如果有的话会有文件路径,等等。
因此对于 5 笔交易,将有 5 种不同的方法。您也可以通过单一方法来实现它,但这只是为了简单起见。
例如
,然后编写一个方法为(以下只是一个模板):
谢谢。
This is just a logic about how can you achieve this.
Write a method for each transaction as you want. Probably it would have all its resources. Like jdbc transaction will have Connection object and query as a requirement, file operation if any will have file path, and so on.
So for 5 transaction, there will be 5 different methods. You can achieve it in a single method as well but this is just for simplicity.
e.g.
Then write a method as (following is just a template):
Thanks.
不,你想要 JTA。
强力方法是使用 JDBC 并自行管理提交和回滚。
最简单的方法是使用 Spring 或 EJB3.1 和声明性事务。
Nope, you want JTA.
The brute force way would be to use JDBC and manage commit and rollback yourself.
The easiest way of all would be to use Spring or EJB3.1 and declarative transactions.