简体   繁体   English

休眠会话工厂范围

[英]hibernate session factory scope

What should be the scope of the Hibernate session factory in a web application? Web 应用程序中 Hibernate 会话工厂的范围应该是什么? I am guessing HTTP session scope, is that correct?我猜是 HTTP 会话范围,对吗?

Context: I have a spring web app that uses Hibernate.上下文:我有一个使用 Hibernate 的 Spring Web 应用程序。 I have inherited years old code and the old developer has left long ago.我继承了多年的旧代码,而老开发人员早就离开了。 The code seems to be using a global hibernate session factory for the application.该代码似乎正在为应用程序使用全局休眠会话工厂。 This does not seem right to me, because this allows only one user to talk to the database at one time.这对我来说似乎不正确,因为这一次只允许一个用户与数据库交谈。

It should be application scope.应该是适用范围。 Sessionfactory is like a connection pool, it generates Hibernate sessions for you. Sessionfactory 就像一个连接池,它为您生成 Hibernate 会话。 It's an expensive created object and you shouldn't create it more than once during application lifetime.它是一个昂贵的创建对象,在应用程序生命周期内不应多次创建它。 Each request gets session from the session factory and does every DB operation through the session and not through the session factory.每个请求从会话工厂获取会话,并通过会话而不是通过会话工厂执行每个数据库操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM