JPA 2.0 中的会话和事务有什么区别? [英] What is the difference between a session and a transaction in JPA 2.0?

查看:20
本文介绍了JPA 2.0 中的会话和事务有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始我的 JPA 2.0 研究,我有这段代码:

I just begin my JPA 2.0 studies, and I have this piece of code:

em = SessionFactory.startSession();
tx = em.getTransaction();

我的问题是:我不确定我是否完全理解使用会话和使用事务之间的区别.在几行中,有人能告诉我它们之间最大的区别吗?谢谢!

My problem is: I'm not sure if I completly understand the difference between the use of a session and the use of a transaction. In a few lines, can anyone please tell me the biggest differences between them ? Thanks !

推荐答案

你去银行存了2张支票,取了一小笔钱.

You go to the bank to deposit 2 checks, and withdraw a small sum.

所以你要排队直到柜员开门.

So you stand in line until a teller opens.

您进行了第一笔存款.
然后是你的第二个.
然后你提款.

You make your first deposit.
Then your second.
Then you make your withdrawal.

现在你已经完成了,离开出纳线.

Now you're done, you leave the teller line.

找到出纳员就像创建您的会话,现在您在银行,准备工作.

Getting to the teller is like creating your session, now you're in the bank, ready to work.

每次存款和取款都是他们自己包含的一组工作,这些是您的交易.

Each deposit and withdrawal are their own contained set of work, these are your transactions.

当您完成工作并离开时,您将关闭或放弃您的会话.

When you're done with your work and leave, you're closing or abandoning your session.

所以,本质上,一个会话包含你的交易,毕竟如果你不去银行,你不能存入银行对吧?

So, in essence, a session contains your transactions, after all you can't make a bank deposit if you never go to the bank right?

这篇关于JPA 2.0 中的会话和事务有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆