我遇到了Spring3和Hibernate4的问题。我想用@ transactions来管理hibernate事务。我的JUnit测试可以与HSQDB一起工作。但在Tomcat上部署时,会发生异常。
我尝试了许多配置,使用单元测试的方法之一是删除会话工厂配置中的hibernate.current_session_context_class属性。然后在服务器模式下,会发生以下异常:
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:990)
at myproject.dao.impl.UserDaoImpl.create(UserDaoImpl.java:37)当我将线程配置为“hibernate.current_session_context_class”值时,异常是:
org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at com.sun.proxy.$Proxy25.save(Unknown Source)
at myproject.dao.impl.UserDaoImpl.create(UserDaoImpl.java:39)下面是我的配置:
<beans>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager"/>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>myproject.dao.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.pool_size">1</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>发布于 2013-11-30 15:29:13
只要使用HibernateTemplate就可以了,因为它会自动进行事务管理。配置事务并不令人头疼。
https://stackoverflow.com/questions/19458922
复制相似问题