为什么Hibernate似乎是为短期会话而设计的?

时间:2022-02-10 21:06:43

I know this is a subjective question, but why does Hibernate seem to be designed for short lived sessions? Generally in my apps I create DAOs to abstract my data layer, but since I can't predict how the entity objects are going to be used some of its collections are lazy loaded, or I should say fail to load once the session is closed.

我知道这是一个主观问题,但为什么Hibernate似乎是为短期会话设计的?通常在我的应用程序中,我创建DAO来抽象我的数据层,但由于我无法预测实体对象将如何使用它的一些集合是延迟加载的,或者我应该说会话关闭后无法加载。

Why did they not design it so that it would automatically re-open the session, or have sessions always stay open?

为什么他们没有设计它以便它会自动重新打开会话,或者会话始终保持打开状态?

6 个解决方案

#1


4  

Becuase once you move out of your transaction boundary you can't hit the database again without starting a new transaction. Having long running transactions 'just in case' is a bad thing (tm).

如果您离开事务边界,则无法在不启动新事务的情况下再次访问数据库。长时间运行交易“以防万一”是件坏事(tm)。

I guess you want to lazy load object from your view - take a look here for some options. I prefer to define exactly how much of the object map is going to be returned by my session facade methods. I find this makes it easier to unit test and to performance test my business tier.

我想你想从你的视图中延迟加载对象 - 看看这里的一些选项。我更喜欢定义我的会话Facade方法将返回多少对象映射。我发现这样可以更轻松地对我的业务层进行单元测试和性能测试。

#2


2  

I worked on a desktop app that used EJB and Hibernate. We had to set lazy=false everywhere, because when the objects get serialized, they lose their ability to be fetched from the backend. That's just how it goes, unfortunately.

我在一个使用EJB和Hibernate的桌面应用程序上工作。我们不得不在任何地方设置lazy = false,因为当对象被序列化时,它们将失去从后端获取的能力。不幸的是,这就是它的方式。

If you are concerned with performance, you could use caching on the backend so that your non-lazy fetches are not as painful.

如果您关心性能,可以在后端使用缓存,这样您的非延迟提取就不会那么痛苦。

#3


1  

You're looking for the OpenSessionInView pattern, which is essentially a conceptual filter (and sometimes implemented as a servlet filter) that detects when a session needs to be transparently reopened. Several frameworks implement this so it handles it automagically.

您正在寻找OpenSessionInView模式,它本质上是一个概念过滤器(有时实现为servlet过滤器),可以检测何时需要透明地重新打开会话。几个框架实现了这一点,因此它可以自动处理它。

#4


1  

I'm writing a desktop application so using a filter isn't applicable.

我正在编写桌面应用程序,因此使用过滤器不适用。

#5


0  

Connections are a scarce resource that need to be recycled as soon as you are done using them. If you are also using connection pooling, getting another one when you need it should be quick. This is the architecture that you have to use to make websites scale -- even though you are a desktop app, their use-cases probably concentrate on scalable sites.

连接是一种稀缺资源,需要在使用它们后立即回收。如果您还使用连接池,则在需要时再使用连接池应该很快。这是您必须使用的架构,以使网站扩展 - 即使您是桌面应用程序,他们的用例可能集中在可扩展的网站上。

If you look at MS ADO.NET, you will see a similar focus on keeping connections open for a short time -- they have a whole offline model for updating data disconnected and then applying to a database when you are ready.

如果你看一下MS ADO.NET,你会发现类似的重点是保持连接在短时间内保持打开状态 - 它们有一个完整的离线模型,用于更新断开连接的数据,然后在准备就绪时应用于数据库。

#6


0  

Hibernate is designed as a way to map Objects to Relational Database tables. It accomplishes that job very well. But, it can't please everybody all of the time. I think there is some complexity in learning how initialization works but once you get the hang of it it makes sense. I don't know if it was necessarily "designed" to specifically to anger you, it's just the way it happened.

Hibernate被设计为将对象映射到关系数据库表的方法。它很好地完成了这项工作。但是,它无法让所有人满意。我认为在学习初始化如何工作方面有一些复杂性但是一旦掌握了它,它就有意义了。我不知道它是否一定是“专门设计”来激怒你,它只是它发生的方式。

If it was going to magically reopen sessions in non-webapps I think the complexity of learning the framework would far outweight the benefits.

如果它能够在非webapps中神奇地重新开放会话,我认为学习框架的复杂性远远超过了它的好处。

#1


4  

Becuase once you move out of your transaction boundary you can't hit the database again without starting a new transaction. Having long running transactions 'just in case' is a bad thing (tm).

如果您离开事务边界,则无法在不启动新事务的情况下再次访问数据库。长时间运行交易“以防万一”是件坏事(tm)。

I guess you want to lazy load object from your view - take a look here for some options. I prefer to define exactly how much of the object map is going to be returned by my session facade methods. I find this makes it easier to unit test and to performance test my business tier.

我想你想从你的视图中延迟加载对象 - 看看这里的一些选项。我更喜欢定义我的会话Facade方法将返回多少对象映射。我发现这样可以更轻松地对我的业务层进行单元测试和性能测试。

#2


2  

I worked on a desktop app that used EJB and Hibernate. We had to set lazy=false everywhere, because when the objects get serialized, they lose their ability to be fetched from the backend. That's just how it goes, unfortunately.

我在一个使用EJB和Hibernate的桌面应用程序上工作。我们不得不在任何地方设置lazy = false,因为当对象被序列化时,它们将失去从后端获取的能力。不幸的是,这就是它的方式。

If you are concerned with performance, you could use caching on the backend so that your non-lazy fetches are not as painful.

如果您关心性能,可以在后端使用缓存,这样您的非延迟提取就不会那么痛苦。

#3


1  

You're looking for the OpenSessionInView pattern, which is essentially a conceptual filter (and sometimes implemented as a servlet filter) that detects when a session needs to be transparently reopened. Several frameworks implement this so it handles it automagically.

您正在寻找OpenSessionInView模式,它本质上是一个概念过滤器(有时实现为servlet过滤器),可以检测何时需要透明地重新打开会话。几个框架实现了这一点,因此它可以自动处理它。

#4


1  

I'm writing a desktop application so using a filter isn't applicable.

我正在编写桌面应用程序,因此使用过滤器不适用。

#5


0  

Connections are a scarce resource that need to be recycled as soon as you are done using them. If you are also using connection pooling, getting another one when you need it should be quick. This is the architecture that you have to use to make websites scale -- even though you are a desktop app, their use-cases probably concentrate on scalable sites.

连接是一种稀缺资源,需要在使用它们后立即回收。如果您还使用连接池,则在需要时再使用连接池应该很快。这是您必须使用的架构,以使网站扩展 - 即使您是桌面应用程序,他们的用例可能集中在可扩展的网站上。

If you look at MS ADO.NET, you will see a similar focus on keeping connections open for a short time -- they have a whole offline model for updating data disconnected and then applying to a database when you are ready.

如果你看一下MS ADO.NET,你会发现类似的重点是保持连接在短时间内保持打开状态 - 它们有一个完整的离线模型,用于更新断开连接的数据,然后在准备就绪时应用于数据库。

#6


0  

Hibernate is designed as a way to map Objects to Relational Database tables. It accomplishes that job very well. But, it can't please everybody all of the time. I think there is some complexity in learning how initialization works but once you get the hang of it it makes sense. I don't know if it was necessarily "designed" to specifically to anger you, it's just the way it happened.

Hibernate被设计为将对象映射到关系数据库表的方法。它很好地完成了这项工作。但是,它无法让所有人满意。我认为在学习初始化如何工作方面有一些复杂性但是一旦掌握了它,它就有意义了。我不知道它是否一定是“专门设计”来激怒你,它只是它发生的方式。

If it was going to magically reopen sessions in non-webapps I think the complexity of learning the framework would far outweight the benefits.

如果它能够在非webapps中神奇地重新开放会话,我认为学习框架的复杂性远远超过了它的好处。