java 每个请求创建休眠会话

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

Create Hibernate-Session per Request

javahibernatejettydesign-patternsresteasy

提问by duselbaer

I just started a simple Java testproject which manages some entities using Hibernate and provides a REST interface to manipulate these objects and provide some additional business logic. The REST interface is created using RESTEasy and Jetty.

我刚刚开始了一个简单的 Java 测试项目,它使用 Hibernate 管理一些实体,并提供一个 REST 接口来操作这些对象并提供一些额外的业务逻辑。REST 接口是使用 RESTEasy 和 Jetty 创建的。

Everything works fine so far, but I have the feeling that I'm actually writing too much boilerplate code. As I don't have much experience in these Java frameworks I'm just wondering if anyone could give me a hint on how to improve the situation.

到目前为止一切正常,但我觉得我实际上编写了太多样板代码。由于我在这些 Java 框架方面没有太多经验,我只是想知道是否有人可以给我一些有关如何改善这种情况的提示。

  1. Creting Hibernate Sessions per Request
  1. 创建每个请求的休眠会话

Well, as far as I understood I have to create a Hibernate session per request and at the end I have to close it. So currently all of my service methods look like this:

好吧,据我所知,我必须为每个请求创建一个 Hibernate 会话,最后我必须关闭它。所以目前我所有的服务方法都是这样的:

Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
...
...
...
session.close();

Is there any way to remove these two lines in order to somehow do this automatically? Currently my service is registered as a RestEASY singleton. Will changing to a RESTeasy ressource and creating the session in the constructor solve the problem? I think it will solve the problem of creating the session. But wherer to close it?

有没有办法删除这两行,以便以某种方式自动执行此操作?目前我的服务已注册为 RestEASY 单身人士。更改为 RESTeasy 资源并在构造函数中创建会话会解决问题吗?我认为它将解决创建会话的问题。但是在哪里关闭它?

In C++ this can easily done be creating a scoped object which closes the session at the end. But in Java?

在 C++ 中,这可以很容易地通过创建一个作用域对象来完成,该对象在最后关闭会话。但是在 Java 中呢?

  1. Whenever such a REST request is made I have to check for a valid session (the user has to login previously). Is a ServletFilter the right way to do this?
  1. 每当发出这样的 REST 请求时,我都必须检查有效的会话(用户必须事先登录)。ServletFilter 是正确的方法吗?

General: Are there any other patterns or frameworks I should consider using? I mean I want to keep it as simple as possible and especially as I dont have that much experience I dont want to end up using Spring or whatever heavyweight framework. Seems that I'm used to the simplicity of Python and Django but for this little project I have to use Java.

一般:我应该考虑使用其他模式或框架吗?我的意思是我想让它尽可能简单,尤其是因为我没有那么多经验,我不想最终使用 Spring 或任何重量级框架。似乎已经习惯了 Python 和 Django 的简单性,但是对于这个小项目,我必须使用 Java。

THanks so far!

到目前为止,谢谢!

采纳答案by sharakan

Hibernate's current recommended approach for managing Sessions is detailed on this wiki page. In particular, I think you need to read the last paragraph: This is all very difficult, can't this be done easier?

Hibernate 当前推荐的管理会话的方法在这个wiki 页面上有详细说明。特别是,我认为你需要阅读最后一段:这一切都非常困难,难道这不能更容易吗?

In the end, you do need to tell the persistence layer that "I'm about to do something" (which usually also gets the Session to do it with) and "I'm done doing it". You can do it with annotations, or JTA transactions, but that information still has to be communicated!

最后,您确实需要告诉持久层“我将要做某事”(这通常也让 Session 去做)和“我已经完成了”。您可以使用注解JTA 事务来实现,但这些信息仍然需要传达!

回答by vinodn

Inject SessionFactory to your Data Access Object and use sessionFactory.getCurrentSession() to access Hibernate Session object. you can make use of any of the Factory classes available to implement this..

将 SessionFactory 注入您的数据访问对象并使用 sessionFactory.getCurrentSession() 访问 Hibernate Session 对象。您可以使用任何可用的工厂类来实现这一点..

Then your code should look like this..

那么你的代码应该是这样的..

sessionFactory.getCurrentSession().save(newInstance); 

回答by verhage

You should try writing a Filter that does this. Spring's OpenSessionInViewFilter is a good place to start if you need an example.

您应该尝试编写一个过滤器来执行此操作。如果您需要示例,Spring 的 OpenSessionInViewFilter 是一个很好的起点。