加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么我的应用程序中的每个线程都使用不同的hibernate

发布时间:2020-12-15 08:33:23 所属栏目:Java 来源:网络整理
导读:我有一个使用hibernate的web应用程序,由于某种原因,每个线程(httprequest或与排队相关的其他线程)使用不同的会话. 我已经实现了一个HibernateSessionFactory类,如下所示: public class HibernateSessionFactory {private static final ThreadLocalSession t
我有一个使用hibernate的web应用程序,由于某种原因,每个线程(httprequest或与排队相关的其他线程)使用不同的会话.
我已经实现了一个HibernateSessionFactory类,如下所示:

public class HibernateSessionFactory {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private  static Configuration configuration = new AnnotationConfiguration();    
private static org.hibernate.SessionFactory sessionFactory;
static {
    try {
        configuration.configure(configFile);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {}
}
private HibernateSessionFactory() {}
public static Session getSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
    rebuildSessionFactory();//This method basically does what the static init block does
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
   return session;
}
//More non relevant methods here.

从我的测试开始,似乎threadLocal成员在JVM首次加载类时确实只初始化了一次,但由于某些原因,当不同的线程访问getSession()方法时,它们使用不同的会话.当一个线程首次访问这个类(Session)threadLocal.get();将返回null,但正如预期的那样,所有其他访问请求将会生成相同的会话.我不确定这是怎么发生的,因为threadLocal变量是final,而方法threadLocal.set(session)仅用于上面的上下文中(我99.9%肯定会有非空会话,因为我会在我的应用程序的不同部分遇到NullPointerException).

我不确定这是否相关,但这些是我的hibernate.cfg.xml文件的主要部分:

<hibernate-configuration>
<session-factory>
<property name="connection.url">someURL</property>
<property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

<property name="hibernate.connection.isolation">1</property> 

<property name="hibernate.connection.username">User</property>
<property name="hibernate.connection.password">Password</property>
<property name="hibernate.connection.pool_size">10</property>

<property name="show_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Mapping files -->

我很感激任何帮助,当然如果有人有任何问题我会很乐意澄清.
以太

解决方法

你知道ThreadLocal的目的吗?

从the docs开始:

This class provides thread-local
variables. These variables differ from
their normal counterparts in that each
thread that accesses one (via its get
or set method) has its own,
independently initialized copy of the
variable. ThreadLocal instances are
typically private static fields in
classes that wish to associate state
with a thread (e.g.,a user ID or
Transaction ID).

您正在为每个线程准确地获得不同的Hibernate会话,因为这是您的代码所要做的.

现在,我们无法评论这是否是一件好事 – 尽管在许多情况下它是合情合理的.毕竟,你不希望两个线程共享同一个会话并与彼此的交易进行交互,不是吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读