在 Java 应用程序上,我在 Activity 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
synchronized 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;
}
提前致谢
最佳答案
似乎是 DBConnections.getHibernateSessionProxyed()
方法中的错误。如果您在静态存储中设置 SessionFactory,则应该同步
。
关于Java - 随着时间的推移打开 hibernate session 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696971/