每个请求创建Hibernate-Session [英] Create Hibernate-Session per Request

查看:111
本文介绍了每个请求创建Hibernate-Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一切都可以正常工作,但我感觉到我实际上写了太多的样板代码。由于我在这些Java框架中没有太多的经验,我只是想知道有没有人可以给我一个提示如何改善这种情况。


  1. 每个请求创建Hibernate会话

嗯,据了解,我必须为每个请求创建一个Hibernate会话,结束我不得不关闭它。所以目前我所有的服务方法如下所示:

 会话session = HibernateUtil.getInstance()。getSessionFactory()。openSession ); 
...
...
...
session.close();

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



在C ++中,可以很容易地创建一个关闭会话结束的作用域对象。但是在Java中?


  1. 每当提出这样的REST请求时,我必须检查一个有效的会话(用户必须先登录)。一个ServletFilter是正确的方法吗?

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



到目前为止!

解决方案

Hibernate目前推荐的管理会话的方法详细介绍了这个 wiki页面。特别是,我想你需要阅读最后一段:这是非常困难的,这不可能做得更容易?



最后,你需要告诉持久层,我要做某事(通常也会得到会话进行)和我做完了。您可以使用注释 JTA交易,但是这些信息仍然需要传达!


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.

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.

  1. Creting Hibernate Sessions per Request

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:

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?

In C++ this can easily done be creating a scoped object which closes the session at the end. But in 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?

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.

THanks so far!

解决方案

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?

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!

这篇关于每个请求创建Hibernate-Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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