java 什么是数据库会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10521947/
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-31 01:24:34  来源:igfitidea点击:

What is a database session?

javahibernatesessiontransactions

提问by Can't Tell

I understand a general understanding of the concept of a database transaction. We access a database within transaction to ensure ACID properties.

我对数据库事务的概念有一个大致的了解。我们在事务中访问数据库以确保 ACID 属性。

In Hibernate there is a concept called a session. What is the use of a session? When should database access happen in two sessions rather than in the same session?

在 Hibernate 中有一个叫做会话的概念。会话有什么用?什么时候数据库访问应该发生在两个会话而不是同一个会话中?

To explain more, I have seen hibernate code that

为了解释更多,我看到了休眠代码

  • gets a session from a session factory
  • opens a session
  • begins a transaction
  • commits the transaction
  • closes the session
  • 从会话工厂获取会话
  • 打开一个会话
  • 开始交易
  • 提交事务
  • 关闭会话

What I need to know is what is the importance of a session here? Why not have something like a transaction factory, begin transaction and commit transaction?

我需要知道的是这里的会议的重要性是什么?为什么没有像事务工厂这样的东西,开始事务和提交事务?

采纳答案by Dmitry

Session is more than just a transaction, it is an implementation of UnitOfWorkpattern. In other words it holds on to the loaded objects, knows which objects has to be persisted etc:

Session 不仅仅是一个事务,它是UnitOfWork模式的一个实现。换句话说,它持有加载的对象,知道哪些对象必须被持久化等:

A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

工作单元会跟踪您在可能影响数据库的业务事务期间所做的一切。完成后,它会计算出由于您的工作而需要完成的所有更改数据库。

To better understand relation between Session and Transaction you can take a look at this article.

为了更好地理解 Session 和 Transaction 之间的关系,你可以看看这篇文章

A single Hibernate Session might have the same scope as a single database transaction.

This is the most common programming model used for the session-per-request implementation pattern. A single Session and a single database transaction implement the processing of a particular request event (for example, a Http request in a web application). Do never use the session-per-operation anti-pattern! (There are extremely rare exceptions when session-per-operation might be appropriate, you will not encounter these if you are just learning Hibernate.)

Another programming model is that of long Conversations, e.g. an application that implements a multi-step dialog, for example a wizard dialog, to interact with the user in several request/response cycles. One way to implement this is the session-per-request-with-detached-objects pattern. Once persistent objects are considered detached during user think-time and have to be reattached to a new Session after they have been modified.

The session-per-conversation pattern is however recommended. In this case a single Session has a bigger scope than a single database transaction and it might span several database transactions. Each request event is processed in a single database transaction, but flushing of the Session would be delayed until the end of the conversation and the last database transaction, to make the conversation atomic. The Session is held in disconnected state, with no open database connection, during user think-time. Hibernate's automatic optimistic concurrency control (with versioning) is used to provide conversation isolation.

单个 Hibernate Session 可能与单个数据库事务具有相同的范围。

这是用于 session-per-request 实现模式的最常见的编程模型。单个 Session 和单个数据库事务实现对特定请求事件(例如,Web 应用程序中的 Http 请求)的处理。不要使用 session-per-operation 反模式!(当 session-per-operation 可能合适时,有极少数例外,如果您只是在学习 Hibernate,则不会遇到这些。)

另一种编程模型是长对话的编程模型,例如实现多步骤对话的应用程序,例如向导对话,以在多个请求/响应周期中与用户交互。实现这一点的一种方法是 session-per-request-with-detached-objects 模式。一旦持久对象在用户思考期间被认为是分离的,并且在它们被修改后必须重新附加到新的会话。

但是,建议使用 session-per-conversation 模式。在这种情况下,单个会话比单个数据库事务具有更大的范围,并且它可能跨越多个数据库事务。每个请求事件都在单个数据库事务中处理,但 Session 的刷新将延迟到会话结束和最后一个数据库事务,以使会话具有原子性。在用户思考期间,会话保持在断开连接状态,没有打开的数据库连接。Hibernate 的自动乐观并发控制(带版本控制)用于提供对话隔离。

回答by Santosh

@Dmitry has answered very nicely.

@Dmitry 的回答非常好。

Another way to look at the session is as Instance of Database Usage. When you create a session, you have a context ready for any database interaction with support services (e.g. transaction, caching, connection etc) required therein. A transaction is an independent service used within the session.

查看会话的另一种方法是Instance of Database Usage。当您创建会话时,您有一个上下文准备好与其中所需的支持服务(例如事务、缓存、连接等)的任何数据库交互。事务是会话内使用的独立服务。

Also the session is the first level cache used by the typical OR mapping tools like hibernate. Session acts as temporary context created upon request to facilitate a DB interaction.

此外,会话是典型的 OR 映射工具(如休眠)使用的第一级缓存。会话充当根据请求创建的临时上下文,以促进数据库交互。