你能在java中每个线程只有一个hibernate会话吗? [英] Can you only have one hibernate session per thread in java?

查看:157
本文介绍了你能在java中每个线程只有一个hibernate会话吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个没有多线程的应用程序。我们有一个主要的业务逻辑方法,可以像这样打开一个Hibernate会话:
$ b

  session = HibernateSessionFactory.getSession(); 
tx = session.beginTransaction();

然后执行一些轻量级数据库操作(少数选择)。然后调用一个我在子类中重写的方法来执行另一个选择。我无法将会话作为参数传递,而无需更新20ish子类。



我尝试打开新会话和事务并在完成后关闭它们,但是当我的方法被踢回了商业方法,我得到了一个关于会话关闭的错误。我试图不关闭会议并提交交易,但那也不起作用。



现在我只是使用它,一切似乎都在本地正常工作:

  session = HibernateSessionFactory.getSession(); 
tx = session.getTransaction();

我只关闭会话并回滚错误。我只是不确定getSession是否会始终返回已经打开的会话。我是否有权假定每个线程每次只能打开一个会话?



以下是getSession的实现:

 会话会话=(会话)threadLocal.get(); 

if(session == null ||!session.isOpen())
{
if(sessionFactory == null)
{
rebuildSessionFactory );
}
session =(sessionFactory!= null)? sessionFactory.openSession():null;
threadLocal.set(session);
}

返回会话;

谢谢!

解决方案

从我的理解,getSession将返回一个新的休眠会话。在Hibernate中有两种使用会话的方法,这些方法在文档中描述了事务模式: https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch02.html



到数据库的实际会话取决于您与数据库的连接。大多数人使用连接池,如C3P0,可用于确保会话重用和加速代码。

我使用的最佳结构是在应用程序启动时创建一个SessionFactory,因为这会将连接池设置为数据库。然后,利用可能的单例模式来保存一个SessionFactory,为您从单个SessionFactory执行的每个事务请求新的会话。 Hibernate将利用底层连接池来处理会话重用,以加快速度和优化。

So I'm working on an application without multi threading. We have a main business logic method that opens a Hibernate session like this:

session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();

and then performs some light DB operations (a few selects). A method that I've overridden in a subclass is then called that needs to perform another select. I can't pass the session as a parameter without having to update 20ish subclasses.

I tried opening a new session and transaction and closing them when I was done, but when my method kicked back to the business method, I got an error that the session was closed. I tried not closing the session and committing the transaction, but that wasn't working either.

Right now I'm just using this and everything seems to be working properly locally:

session = HibernateSessionFactory.getSession();
tx = session.getTransaction();

and I'm only closing the session and rolling back incase of errors. I'm just uncertain if getSession will always return the session thats already open. Am I right to assume that only one session can be open at a time per thread?

Here's the implementation of getSession:

Session session = (Session) threadLocal.get();

    if (session == null || !session.isOpen())
    {
        if (sessionFactory == null)
        {
            rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession() : null;
        threadLocal.set(session);
    }

    return session;

Thanks!

解决方案

From my understanding, getSession will return a new hibernate session. There are two methods of using sessions in Hibernate that is described transactional patterns in the document: https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch02.html

The actual "session" to the database depends on your connection to the database. Most people utilize a connection pool, like C3P0 that can be used to ensure session reuse and speed up your code.

The best structure I use is to create a SessionFactory once in application launch, as this sets up the connection pool to the database. Then, utilizing maybe a singleton pattern to keep a single SessionFactory, request new sessions for each transaction you perform from the single SessionFactory. Hibernate will utilize the underlying connection pool to handle session reuse for speed and optimization.

这篇关于你能在java中每个线程只有一个hibernate会话吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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