DAO,Hibernate和Web项目中的会话 [英] DAO, Hibernate and sessions in web projects

查看:68
本文介绍了DAO,Hibernate和Web项目中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于DAO和会话的问题,但不明白问题。现在我使用这样的东西:核心DAO (我添加了一些核心DAO中的额外检查)以及基于Web项目的 DAO实施在检票框架上。一般来说,这种模式可以被描述为:
$ b $ 1)使用1个线程的每个线程;

<2>从来不关闭会话;
$ b $ 3)总是在动作后提交事务。



我使用DDoS实用程序测试它,实施工作(每秒100个连接)。但是当我增加连接数时,关于关闭会话和未开始事务会有很多异常。我如何理解我应该使用getCurrentSession()而不是openSession(),但是当我使用getCurrentSession()在核心DAO中修改方法getSession()时,我没有修复错误:(然后我尝试使用c3p0和bonecp。强大的工具并没有帮助我,也许你解释我的错误:(除了DNA中的错误之外)

从任何地方复制,将hibernate会话存储在一个ThreadLocal对象中,也就是说,一个hibernate会话绑定到一个线程,就像您提到的那样。



但是你正在做一个web项目。你应该把一个Hibernate会话绑定到一个用户(或浏览器),即一个Http会话。但是你不知道在一个http会话中哪个线程处理了请求,因此在你的解决方案中,同一个Http会话可能会有所不同Hibernate会话或可能不同的Http会话可能会得到相同的Hibernate会话,这取决于您的Http服务器。解决方案:将Hibernate sessi放到Http会话中(而不是使用ThreadLocal对象)。使用 HttpServletRequest.getSession() HttpSession.getAttribute() / <$ c $获得Http会话对象c> HttpSession.setAttribute()可以设置Hibernate会话和其他Http会话相关的数据。


I have read a lot of question about DAO and sessions but don't understand problem. Now I use something like this: core DAO (I added some additional checks in core DAO) and DAO implementation for web project based on wicket framework. In general this pattern can be described like:

1) using 1 session per 1 thread;

2) never close session;

3) always commit transaction after action.

I test it using DDoS utility and this implementation work (100 connections per second). But when I increase count of connections I will have lots of exception about closing session and not started transaction. How I understand I should use getCurrentSession() instead of openSession() but when I modified method getSession() in core DAO using getCurrentSession(), I didn't fix bugs :(. Then I tried to use c3p0 and bonecp. But those powerful tools didn't help me. Maybe you explain my mistakes :( except bug in the DNA

解决方案

The DAO, which you copied from anywhere, stores the hibernate session in a ThreadLocal object, i. e. one hibernate session is bound to one thread, as you mentioned.

But you're doing a web project. There you should bind one Hibernate session to one user (or browser), i. e. to one Http session. But you do not know in which thread the request from one http session is processed. Thus in your solution the same Http session may get different Hibernate sessions or perhaps different Http sessions may get the same Hibernate session. This depends to your Http server.

Solution: Put the Hibernate session into the Http session (and do not use the ThreadLocal object). You get the Http session object with HttpServletRequest.getSession() and with HttpSession.getAttribute()/HttpSession.setAttribute() you can set the Hibernate session and other Http session related data.

这篇关于DAO,Hibernate和Web项目中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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