什么是数据库会话? [英] What is a database session?

查看:338
本文介绍了什么是数据库会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解数据库事务概念的一般理解。我们访问事务内的数据库以确保ACID属性。

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



为了解释更多,我已经看到了hibernate代码




  • 从会话工厂获取会话
  • 打开会话

  • 开始交易
  • >
  • 提交交易

  • 关闭会话



需要知道这里的会话的重要性是什么?为什么没有像事务工厂那样的东西,开始事务和提交事务? 解决方案

实施 UnitOfWork 模式。换句话说,它可以保持已加载的对象,知道哪些对象必须被持久保存等。


工作单元跟踪所有内容您在可能影响数据库的业务事务中执行操作。完成后,它会根据您的工作情况计算出所有需要完成的工作来改变数据库。


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


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

这是用于session-per-request实现模式的最常用的编程模型。单个Session和单个数据库事务实现特定请求事件(例如Web应用程序中的Http请求)的处理。千万不要使用每个操作的会话反模式! (当每次操作的session可能是合适的时候,极少有例外,如果你刚刚学习Hibernate,你将不会遇到这些情况。)另一种编程模型是长对话,例如一个实现多步对话的应用程序,例如向导对话框,用于在多个请求/响应周期中与用户进行交互。
实现这一点的一种方法是每个请求与分离对象的会话模式。一旦持久对象在用户思考期间被认为是分离的,并且必须在它们被修改后重新附加到新的会话中。



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



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

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?

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?

解决方案

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

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.

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.

这篇关于什么是数据库会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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