迁移到hibernate 4 + spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步会话 [英] migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
问题描述
我正在迁移到Spring 4.2.2.RELEASE和hibernate 5,并得到以下异常:
在hibernate 4中也发生异常:
原因:org.springframework.beans.factory.BeanCreationException:在类路径资源[业务上下文中定义名称'NotificationService' xml]:设置属性值时出错;嵌套异常是org.springframework.beans.PropertyBatchUpdateException;嵌套PropertyAccessExceptions(1)是:
PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'lovDao'引发异常;嵌套异常是org.hibernate.HibernateException:无法获取当前线程的事务同步会话
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518)
at org .bringframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org .springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework .beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework .beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
... 127 more
引起:org.springframework.beans.PropertyBatchUpdateException;嵌套PropertyAccessExceptions(1)是:
PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'lovDao'引发异常;嵌套异常是org.hibernate.HibernateException:无法获取当前线程的事务同步会话
在org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)
在org.springframework.beans .AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
... 135 more
: org.springframework.beans.PropertyBatchUpdateException:失败的属性:属性'lovDao'抛出异常;嵌套异常是org.hibernate.HibernateException:无法获取当前线程的事务同步会话
在org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)
在org.springframework.beans .AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support .AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
截断。查看完整堆栈跟踪的日志文件
这是'lovDao'bean的定义:
< bean id =ListOfValuesDaoclass =be.fgov.just.cjr.dao.core.ListOfValuesDaoImpl>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
以及dao-context.xml的相关部分:
<! - 启用基于注释的事务行为配置 - >
< bean id =transactionManagername =transactionManager
class =org.springframework.orm.hibernate5.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
<! - 启用基于注释的交易行为配置 - >
< tx:注解驱动的事务管理器=transactionManager/>
< context:component-scan base-package =be.fgov.just.cjr.dao/>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate5.LocalSessionFactoryBean>
< property name =dataSourceref =datasource/>
< property name =packagesToScanvalue =be.fgov.just.cjr.model/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.bytecode.use_reflection_optimizer> true< / prop>
< prop key =hibernate.dialect> be.fgov.just.cjr.oracle.OracleDialectWithXmlTypeSupport< / prop>
< prop key =hibernate.search.autoregister_listeners> false< / prop>
< prop key =hibernate.transaction.factory_class> org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory< / prop>
< prop key =hibernate.use_sql_comments> true< / prop>
< prop key =hibernate.generate_statistics> true< / prop>
< prop key =hibernate.cache.use_structured_entries> true< / prop>
< prop key =hibernate.cache.region.factory_class> org.hibernate.cache.ehcache.EhCacheRegionFactory< / prop>
< prop key =hibernate.cache.use_query_cache> true< / prop>
< prop key =hibernate.cache.use_second_level_cache> true< / prop>
< prop key =hibernate.show_sql> false< / prop>
< /道具>
< / property>
< / bean>
现在,这是lovDao的类定义:
@Transactional
// @存储库已删除以进行测试(不工作)
公共类ListOfValuesDaoImpl扩展GenericDaoImpl实现ListOfValuesDao {
以及GenericDaoImpl是事务性的
@Transactional
// @存储库被移除以进行测试(没有工作)
public class GenericDaoImpl implements GenericDao {
为什么lovDao不能获取一个事务同步的Session?
ListOfValuesDaoImpl起初并不是Transactional,而是抛出同样的例外; GenericDaoImpl是。
GenericDao也是Transactional:(不再是)
// @ Transactional已移除以进行测试(无法正常工作)
// @已删除Repository以进行测试(无法正常工作)
public interface GenericDao {
我知道类似的问题被问到,但我已经尝试了很多指针并阅读了相当多的指南,但似乎没有任何工作......
感谢指点/帮助/批评性评论;)
edit:
sessionFactory在GenericDaoImpl中设置...:
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
$ b $(我添加了@Qualifier(sessionFactory),因为IntelliJ抱怨多重bean(实际上指向相同的bean))
- >我检查了dao-context.xml是否被多次使用,但它不是
...和(在同一班级)会话是这样获得的:
public Session getSession(){
return sessionFactory.getCurrentSession();
编辑2(编辑GUISSOUMA Issam的答案):
我也有这样的bean:
< bean id = DossierDaoclass =be.fgov.just.cjr.dao.dossier.DossierDaoImpl>
< property name =sessionFactoryref =sessionFactory/>
< property name =lovDaoref =ListOfValuesDao/>
< property name =dossierValidatorref =DossierValidator/>
< / bean>
我该怎么做?
(他们引用ListOfValuesDao ...)
(@Autowiring似乎没有帮助(根据IntelliJ)它没有找到对ListOfValuesDao的引用)
< context:component-scan base-package = be.fgov.just.cjr.dao/>
ps:感谢您持续帮助我,问题是我现在无法测试东西,因为我从家里写下这些文件(并且无法到达办公室的环境:(
- >如果您提供了几个指针,我可以在明天尝试它们,但是我只能依靠IDE的输出现在:(
- >我会尽量给出尽可能多的信息)
我也在GenericDaoImpl中有这个:
private SessionFactory sessionFactory;
$ b $ @Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this .sessionFactory = sessionFactory;
}
加上:
< bean id =GenericDaoclass =be.fgov.just.cjr.dao.GenericDaoImpl>
<属性名称=sessionFactoryref =sessionFactory/>
< / bean>
基于对答案的编辑我想t他会导致问题?
(我明天早上只能检查......)
编辑3:我已经获得了一些新的信息,而不是将其全部附加在这里我创建了一个新问题 ...
解决方案将@Repository注释添加到您的DAO中。
删除此声明
< bean id =ListOfValuesDaoclass =be.fgov.just.cjr。 dao.core.ListOfValuesDaoImpl>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
从您的XML
这:
< context:component-scan base-package =be.fgov.just.cjr.dao/> gt ;
确保您的包只被扫描一次(在一个xml文件中声明)
I'm migrating to spring 4.2.2.RELEASE and hibernate 5 and get following exception:
The exception also occurs in hibernate 4:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'NotificationService' defined in class path resource [business-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ... 127 more Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) ... 135 more :org.springframework.beans.PropertyBatchUpdateException:Failed properties: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) Truncated. see log file for complete stacktrace
this is the definition of the 'lovDao' bean:
<bean id="ListOfValuesDao" class="be.fgov.just.cjr.dao.core.ListOfValuesDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
and relevant portions of the dao-context.xml:
<!-- Enable the configuration of transactional behavior based on annotations --> <bean id="transactionManager" name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- Enable the configuration of transactional behavior based on annotations --> <tx:annotation-driven transaction-manager="transactionManager"/> <context:component-scan base-package="be.fgov.just.cjr.dao" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="datasource"/> <property name="packagesToScan" value="be.fgov.just.cjr.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> <prop key="hibernate.dialect">be.fgov.just.cjr.oracle.OracleDialectWithXmlTypeSupport</prop> <prop key="hibernate.search.autoregister_listeners">false</prop> <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop> <prop key="hibernate.use_sql_comments">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.use_structured_entries">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean>
Now, this is the class definition of the lovDao:
@Transactional //@Repository removed for testing (didn't work) public class ListOfValuesDaoImpl extends GenericDaoImpl implements ListOfValuesDao {
and also GenericDaoImpl is transactional
@Transactional //@Repository removed for testing (didn't work) public class GenericDaoImpl implements GenericDao {
Why doesn't lovDao can't obtain a transaction-synchronized Session?
ListOfValuesDaoImpl wasn't Transactional at first but threw the exact same exception; GenericDaoImpl was.
also GenericDao is Transactional: (not anymore)
//@Transactional removed for testing (didn't work) //@Repository removed for testing (didn't work) public interface GenericDao {
I know that similar questions are asked around SO but I already tried quite some pointers and read quite some guides but nothing seemed to work...
thanks for pointers/help/critical remarks ;)
S.
edit:
the sessionFactory is set in GenericDaoImpl...:
private SessionFactory sessionFactory; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
(I added the @Qualifier("sessionFactory") because IntelliJ complained about multiple beans (which in fact pointed to the same bean)) -> I checked whether the dao-context.xml is used multiple times but it isn't
...and (in that same class) the session is obtained like this:
public Session getSession() { return sessionFactory.getCurrentSession(); }
edit 2 (reaction on edit of answer of GUISSOUMA Issam):
I also have beans like this one:
<bean id="DossierDao" class="be.fgov.just.cjr.dao.dossier.DossierDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> <property name="lovDao" ref="ListOfValuesDao"/> <property name="dossierValidator" ref="DossierValidator"/> </bean>
What do I do with those?
(they reference ListOfValuesDao...) (@Autowiring doesn't seem to help (according to IntelliJ) it doesn't find a reference to a ListOfValuesDao)
-> the same exception also occurs when I removed:
<context:component-scan base-package="be.fgov.just.cjr.dao" />
ps: thanks for your persistence in helping me, problem is I can't test out stuff now because I write this from home (and can't reach the environment on the office :( -> if you give a couple of pointers I'll be able to try them out tomorrow but I can only depend on the IDE's output for the present moment :( -> I'll try to give as much information as possible
I also have this in GenericDaoImpl:
private SessionFactory sessionFactory; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
in combination with:
<bean id="GenericDao" class="be.fgov.just.cjr.dao.GenericDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
based on the edit of the answer I suppose this can cause the problem? (I can only check for sure tomorrow morning...)
edit 3: I've gotten quite some new information and instead of appending it all here I created a new question...
解决方案Add @Repository annotation to your DAO.
remove this declaration
<bean id="ListOfValuesDao" class="be.fgov.just.cjr.dao.core.ListOfValuesDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
from your XML
because you have this:
<context:component-scan base-package="be.fgov.just.cjr.dao" />
Make sure your package is scanned only once(declared on in one xml file)
这篇关于迁移到hibernate 4 + spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!