如何有效测试事务的酸性(Java/Spring/EJB 或其他)
只是想知道。 在某些应用程序中,原子性和一致性确实非常重要,我想应该对其进行测试...
由于我是一名 Java 开发人员,我将谈论我所知道的。使用带有注释的 Spring 或 EJB3 事务管理。
当在应用程序的业务层中进行一些重构时,我们有时必须重新处理事务传播以及其他事情,并且它很容易引入回归。
我只是想知道是否有简单的方法来测试我们应用程序的事务管理。 例如,假设您在 PayPal 工作。您需要确保客户的 PayPal 帐户已记入贷方,并且客户的银行帐户已扣款。例外情况不应允许在不向客户收取任何费用的情况下存入 PayPal 帐户。
- 有没有工具可以测试这些东西?
- 你已经做过这样的事了吗?
- 或者你会怎么做?
我想它可以使用一些 AOP 并设计方法来“自制”,以便更容易测试......
Just wonder.
In some applications, the atomicity and consistency are really reallly important that i guess it should be tested...
As i'm a Java developer i'll talk about what i know. Using Spring or EJB3 transaction management with annotations.
When doing some refactoring in the business layer of an application, we sometimes have to rework the transation propagation amon other things, and it can easily introduce regressions.
I just wonder if there are easy ways to test the transaction management of our application.
For exemple, imagine you work at PayPal. You want to be sure that the customer PayPal account is credited, AND the customer is charged on his bank account. An exception should not permit to credit the paypal account without charging anything to the client.
- Are there tools to test such things?
- Have you already done such a thing?
- Or how would you do?
I guess it could be done "home made" using a bit of AOP and designing the methods so that it will be easier to test...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用 Byteman 等字节码工具在测试中注入异常:
http://java.dzone.com/articles/fault-injection-unit-tests
一旦引发异常,您可以检查数据库状态是否已不一致更新或已回滚...
It's possible to inject exceptions in tests with bytecode tools like Byteman:
http://java.dzone.com/articles/fault-injection-unit-tests
Once you raise an exception you can check if the db state has been inconsistently updated or been rollbacked...
您必须设置补偿交易来与 PayPal 或银行等第三方打交道。您的客户将必须询问响应并进行处理。
据我所知,没有什么比用于多个数据库之间的两阶段提交的 XA 驱动程序更简单的了。您需要 JCA 之类的东西和事务管理器来监督它们。
You have to set up compensating transactions to deal with 3rd parties like PayPal or banks. Your client will have to interrogate the response and deal with it.
There's nothing as easy as the XA driver for two-phase commit between multiple databases that I know of. You'd need something like JCA with a transaction manager to oversee them.