【发布时间】: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