java 解释 EJB 事务属性的示例或用例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1759554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:51:21  来源:igfitidea点击:

Examples or Uses Cases to explain EJB Transaction Attributes

javatransactionsjakarta-eeejb-3.0

提问by cwash

There are some good explanations of EJB Transaction Attributes (and annotations) out there, for example, OpenEJB's.

有一些关于 EJB 事务属性(和注释)的很好的解释,例如OpenEJB 的.

But sometimes when I try to cover this with someone who hasn't worked with many transactional resources, I see their eyes start to glaze over.

但有时,当我试图与一个没有使用过很多交易资源的人讨论这个问题时,我看到他们的眼睛开始呆滞。

So my question - how would you explain EJB Transaction Attributes to your grandmother?

所以我的问题 - 您将如何向您的祖母解释 EJB 事务属性?

  • Required
  • RequiresNew
  • Mandatory
  • NotSupported
  • Supports
  • Never
  • 必需的
  • 需要新的
  • 强制的
  • 不支持
  • 支持
  • 绝不

I'm thinking a contrived example, analogy, or a concise real-world use case would be helpful.

我认为一个人为的例子、类比或简洁的现实世界用例会有所帮助。

采纳答案by cwash

I think it makes sense to think about this in terms of the container's interaction with a caller to the EJB method as a true monitor... so I'd like to use a bouncer metaphorin various different scenarios.

我认为将容器与 EJB 方法调用者的交互视为真正的监视器来考虑这一点是有道理的……所以我想在各种不同的场景中使用保镖比喻

See this pagefor a good description/overview of the transaction attributes.

有关交易属性的良好描述/概述,请参阅此页面

Required (REQUIRED @TransactionAttribute)
Night club

必需(必需@TransactionAttribute)
夜总会

Show up at the club, need a ticket to enter. If you don't have one it will be (purchased?) given to you at the door.

出现在俱乐部,需要门票才能进入。如果你没有,它会在门口(购买?)给你。

Transaction is the TICKET.
Container is the BOUNCER.

交易是TICKET。
容器是 BOUNCER。

Requires New (REQUIRES_NEW @TransactionAttribute)
Comedy clubs, 1 drink-minimum, no re-entry

需要新的 (REQUIRES_NEW @TransactionAttribute)
喜剧俱乐部,至少 1 杯饮料,不可重新进入

Show up at the club, no outside food/drink, you must leave them at the door. To get in you must purchase 1-drink minimum every time you leave and re-enter.

出现在俱乐部,没有外面的食物/饮料,你必须把它们留在门口。要进入,您必须在每次离开和重新进入时至少购买 1 杯饮品。

Transaction is the DRINK.
Container is the BOUNCER.
Suspending the transaction is LEAVING AT THE DOOR.

交易是DRINK。
容器是 BOUNCER。
暂停交易正在离开。

Supports (SUPPORTS @TransactionAttribute)
House party

支持 (SUPPORTS @TransactionAttribute)
家庭聚会

Show up at the party, alcohol is permitted. We'll let you in with it if you have your own alcohol, if you don't we'll let you in too.

出现在派对上,酒精是允许的。如果你有自己的酒,我们会让你进去,如果你没有,我们也会让你进去。

Transaction is the ALCOHOL.
Container is the HOST.

交易是酒精。
容器是主机。

Mandatory (MANDATORY @TransactionAttribute)
Invite-only party

强制性 (MANDATORY @TransactionAttribute)
仅限邀请方

Show up at the party, need a invitation to enter: If you don't have one and try to get in, the bouncer calls the authorities.

出现在派对上,需要邀请才能进入:如果你没有邀请并试图进入,保镖会打电话给当局。

Transaction is the INVITATION.
Container is the HOST.
Throwing an exception is CALLING THE AUTHORITIES.

交易就是邀请。
容器是主机。
抛出异常是调用权威。

Not Supported (NOT_SUPPORTED @TransactionAttribute)
Concert, cameras are prohibited.

不支持 (NOT_SUPPORTED @TransactionAttribute)
音乐会,禁止使用相机。

Show up at the concert, cameras are prohibited. You can leave it at the door and pick it up when you leave.

出现在音乐会上,禁止使用相机。可以放在门口,走的时候再拿。

Transaction is the CAMERA.
Container is the DOORMAN.
Suspending the transaction is LEAVING AT THE DOOR.

交易是相机。
集装箱是门卫。
暂停交易正在离开。

Never (NEVER @TransactionAttribute)
High school dance

从不(从不@TransactionAttribute)
高中舞蹈

Show up at the dance, alcohol is prohibited. If you try to get in with it and are caught, the chaperone calls the authorities.

参加舞会,禁止饮酒。如果你试图介入并被抓住,监护人会打电话给当局。

Transaction is the ALCOHOL.
Container is the CHAPERONE. Throwing an exception is CALLING THE AUTHORITIES.

交易是酒精。
容器是伴侣。抛出异常是调用权威。

回答by Miguel Ping

You can think of them in terms of Collaboration. Let's say you are a chef, and you have a dozen ovens to work with. You need to bake some cakes. For this example, youare the called method, the ovenis the transaction and the your bossis the caller.

您可以将它们视为 Collaboration。假设您是一名厨师,您有十几个烤箱可以使用。你需要烤一些蛋糕。在这个例子中,是被调用的方法,烤箱是事务,你的老板是调用者。

  • Required: if your boss tells you to bake some cakes without telling you what oven you use, you just go ahead and pick one. At the end, you close the oven, making sure nobody else can use it.
  • Requires New: You will always bake the cakes on a free oven you choose. If you're in the middle of the process of baking some cakes and your boss tells you to bake another batch, you interrupt the current baking process, go and bake some cakes at a new oven, the resume the baking for the old ones.
  • Mandatory: You are a dumb cook. Your boss alwayshave to tell you what oven you have to use. If your boss doesn't tell you what oven to use, you shout back "stupid!".
  • NotSupported: Think of this as doing a dessert that doesn't need the oven. If you are in the middle of baking some cakes at the oven, you stop that, create the dessert, and the resume the cake baking.
  • Supports: This is more suited to a cook's helper. You are the helper. If the main cook asks you to bake a cake with oven X, you just do that. If he asks you to do a dessert, you just do that. The main difference in regards to the other ones is that you never ask any question nor choose to do anything. You just follow orders.
  • Never: This is yet another dumb cook. If your boss asks you to do a dessert while you're in the middle of baking a cake, you shout back and say "I quit!". No cake is ever baked. So your boss has to be careful and only ask you to bake some cakes when you are not baking any cakes.
  • 要求:如果你的老板告诉你烤一些蛋糕,但没有告诉你你用的是什么烤箱,你直接选一个。最后,关闭烤箱,确保没有其他人可以使用它。
  • 需要新:您将始终在您选择的免费烤箱上烘烤蛋糕。如果你在烤蛋糕的过程中,你的老板告诉你再烤一批,你会中断当前的烘焙过程,去新烤箱烤一些蛋糕,继续烤旧的。
  • 必填项:你是个笨厨师。你的老板总是不得不告诉你你必须使用什么烤箱。如果你的老板没有告诉你用什么烤箱,你就会大喊“愚蠢!”。
  • NotSupported:可以将其视为不需要烤箱的甜点。如果你正在烤箱里烤一些蛋糕,你就停下来,制作甜点,然后继续烤蛋糕。
  • 支持:这更适合做厨师的帮手。你是帮手。如果主厨让你用 X 烤箱烤蛋糕,你就照做。如果他要你做甜点,你就去做。与其他人的主要区别在于,您从不提出任何问题,也不选择做任何事情。你只是听从命令。
  • 从不:这又是一个愚蠢的厨师。如果你的老板在你烤蛋糕的过程中让你做甜点,你会大喊“我不干了!”。没有蛋糕是烤的。所以你的老板要小心,只有在你不烤蛋糕的时候才让你烤一些蛋糕。

Hope that helps.

希望有帮助。