【问题标题】:Java - issue with opening hibernate session over timeJava - 随着时间的推移打开休眠会话的问题
【发布时间】:2015-07-29 09:51:28
【问题描述】:

在一个 java 应用程序上,我在 12-24 小时的活动中收到以下错误消息

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.NullPointerException
    Caused by: java.lang.NullPointerException
    at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.xxxxxx.persistence.proxy.SessionFactoryProxy$TransactionAwareInvocationHandler.invoke(SessionFactoryProxy.java:322)
    at $Proxy11.openSession(Unknown Source)
    at com.xxxxxx.persistence.proxy.SessionFactoryProxy.openSession(SessionFactoryProxy.java:221)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:425)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:322)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:255)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)

我将这个类与另一个我没有这个问题的应用程序进行了比较,区别是private 同步 SessionFactory getSessionFactory()

我不确定这是否能解决问题,或者如果它是其他问题怎么办

private SessionFactory getSessionFactory() {
    SessionFactory sf = sessionFactoryBuilder.getSessionFactory();

    if (!DBConnections.getHibernateSessionsProxyed().containsKey(DBConnections.getCustomer())) {
        Class sfInterface = SessionFactory.class;
        if (sf instanceof SessionFactoryImplementor) {
            sfInterface = SessionFactoryImplementor.class;
        }
        SessionFactory proxy = (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(), new Class[] { sfInterface },
                new TransactionAwareInvocationHandler(sf));
        DBConnections.addSessionProxyed(proxy);
    }
    return DBConnections.getHibernateSessionProxyed();
}

public Session openSession() throws HibernateException {
    return getSessionFactory().openSession(); //line 221 from the error message
}

public static Map<String, SessionFactory> getHibernateSessionsProxyed() {
    if(hibernateSessionsProxyed == null) {
        hibernateSessionsProxyed = new HashMap<String, SessionFactory>();
    }
    return hibernateSessionsProxyed;
}

public static void addSessionProxyed(SessionFactory session) {
    if(hibernateSessionsProxyed == null) {
        hibernateSessionsProxyed = new HashMap<String, SessionFactory>();
    }
    hibernateSessionsProxyed.put(getCustomer(), session);
}

public TransactionAwareInvocationHandler(SessionFactory target) {
        this.target = target;
    }

提前致谢

【问题讨论】:

    标签: java hibernate synchronized


    【解决方案1】:

    似乎是您的 DBConnections.getHibernateSessionProxyed() 方法中的一个错误。如果您将 SessionFactory 设置在静态存储中,您应该 synchronize

    【讨论】:

    • 这里的代码 DBConnections.getHibernateSessionProxyed() public static Map&lt;String, SessionFactory&gt; getHibernateSessionsProxyed() { if(hibernateSessionsProxyed == null) { hibernateSessionsProxyed = new HashMap&lt;String, SessionFactory&gt;(); } return hibernateSessionsProxyed; } 你的意思是我应该改变这个静态来同步?抱歉,我对那种类型的代码不太熟悉
    • 您需要将此发布到您的问题以及DBConnections.addSessionProxyed(proxy)TransactionAwareInvocationHandler 的代码
    • 对不起,我在问题中添加了方法
    • hibernateSessionsProxyed 似乎是共享的,因此您需要在添加/删除条目时正确同步它。
    【解决方案2】:

    我将私有 SessionFactory getSessionFactory() 更改为私有同步 SessionFactory getSessionFactory() 并且在活动 4 周后问题没有再次出现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 2020-04-21
      • 1970-01-01
      相关资源
      最近更新 更多