Java 春季3&x2B;Hibernate 4,当前线程没有会话,尝试了所有操作
我对此束手无策,似乎找不到任何帮助。我一直有例外Java 春季3&x2B;Hibernate 4,当前线程没有会话,尝试了所有操作,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我对此束手无策,似乎找不到任何帮助。我一直有例外 org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImp
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
at com.thing.basicwebform.dao.AbstractHbnDao.getSession(AbstractHbnDao.java:31)
at com.thing.basicwebform.dao.HbnAccountDao.findByUsername(HbnAccountDao.java:38)
at com.thing.basicwebform.service.AccountServiceImpl.validateUsername(AccountServiceImpl.java:34)
at com.thing.basicwebform.service.AccountServiceImpl.registerAccount(AccountServiceImpl.java:27)
at com.thing.basicwebform.AccountController.postRegistrationForm(AccountController.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
我已经设置了我的应用程序上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/basicwebform" resource-ref="true" />
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<util:properties id="hibernateProperties">
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
</util:properties>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.thing.basicwebform.domain</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.thing.basicwebform.dao" />
<context:component-scan base-package="com.thing.basicwebform.service" />
</beans>
最后,我的抽象DAO看起来是这样的(顶层其实没有什么不同,有两个很好的包装器方法,就是这样):
错误,一旦我添加了
<property name="hibernate.current_session_context_class">thread</property>
线程
添加到hibernate属性对象,但在添加beginTransaction()和transaction.commit()调用后,这一问题得到了修复。谢谢,伙计们:)如果没有当前会话,请像这样打开一个新会话
protected Session getSession() {
Session session = null;
try {
session = getSessionFactory().getCurrentSession();
} catch (HibernateException e) {
System.out.println("Current session is NULL..opening new session");
session = getSessionFactory().openSession();
}
return session;
}
提供更多的stacktrace帮助我们了解异常发生的时间。我看不出有什么明显的错误。两个后续问题。所有的处理都发生在一个线程上吗?你还可以粘贴AccountDao吗?你已经通过了吗?这就是我所缺少的
@Transactional
和
的全部要点是不要手动调用beginTransaction()
和commit()
。我担心的是,我完成后如何杀死它?还有,请检查一下,那个链接让我走上了正确的轨道。现在出现了一个新错误,但我现在应该能够克服它了,谢谢:)
org.hibernate.HibernateException: getNamedQuery is not valid without active transaction
<property name="hibernate.current_session_context_class">thread</property>
protected Session getSession() {
Session session = null;
try {
session = getSessionFactory().getCurrentSession();
} catch (HibernateException e) {
System.out.println("Current session is NULL..opening new session");
session = getSessionFactory().openSession();
}
return session;
}