代码中的写法是:
然后我上网查了好多资料,大多都说要使用getCurrentSession()方法,要在hibernate里配置:
<prop key="hibernate.current_session_context_class">thread</prop>
加上上面的配置之后运行会报这样的错:
org.hibernate.HibernateException: createCriteria is not valid without active transaction
我上网查了很多关于这个错的原因,大多的都说是dao层没有加上事务的原因,都建议在dao层的实现类上也加上@Transaction这个注解,但是我加上之后还是没用,一点效果都没有
问了好几个群,大多都说是因为service里没有配置上事务造成的,可我service类里确实加上了@Service和@Transaction这两个注解了啊。
也有地方说这样配置:
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
但这样配置之后还是会报没有配置之前的错:
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
还有说在web.xml里配置的:
这样配置了之后确实没问题,不过用的是OpenSessionInViewFilter,我不太明白是什么意思?跟getCurrentSession()有什么区别吗?
----------------------------------------分割线----------------------------------------
除了上面的配置,我还试了下面这些配置,均无效,都是报错
让我诧异的一个地方是如果是junit4测试的话,是不会报错的,配置如下:
applicationContext.xml里的事务配置:
service里的配置:
dao里的配置:
junit4测试的时候是没有在dao里配置@Transaction ,也没有在applicationContext.xml里的hibernateProperties里配置hibernate.current_session_context_class属性,但测试就是不报错,控制台也有sql语句输出。
----------------------------------------分割线----------------------------------------
解决方案:
<tx:annotation-driven transaction-manager="transactionManager" />
上面这句需要加在DispatcherServlet指定的xml中
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
加在下面的root xml中,会导致在servlet时调用不到事务管理器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
之所以你用junit可以,是因为junit进来不是经过servlet进来所以你配置在root中的事务管理器能找到。但是普通通过servlet进来的请求,找不到root中的事务管理器,所以需要在DispachServlet指定的xml中配置事务管理器供其使用
getCurrentSession()需要在事物环境下才能取得!,通过给controller层配置事物解决;
相关推荐
报错 org.apache.hadoop.hdfs.BlockMissingException: Could not obtain block 2、百度结果 参考 https://blog.csdn.net/xiaozhaoshigedasb/article/details/88999595 防火墙记得关掉; 查看DataNode是否启动;...
2部分
由于50M的限制不得不分成3部分,将3个文件解压后放到一个文件夹中直接使用,
编写一个程序,如何让机器自动输出我们想要的数字。例如编写程序得到数列393 163 67 29 9的下一个数字是。
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a ...
获取文件的属性,可以知道文件的大小,即多少字节,只读,隐藏等属性
## you do NOT need this setting if you specify hibernate.transaction.manager_lookup_class #jta.UserTransaction jta/usertransaction #jta.UserTransaction javax.transaction.UserTransaction #jta....
里面详细介绍了怎么去使用这个用户控件,很方便的上传控件,4步搞定上传,上传后你还能用这个控件察看/下载刚 上传的文件,简单来说这个控件包含了上传/邦定/下载/察看缩列图的多重功能
freertos 在msp430F5529上得移植代码。
The Paxos Commit algorithm runs a Paxos consensus algorithm on the commit/abort decision of each participant to obtain a transaction commit protocol that uses 2F + 1 coordinators and makes progress ...
The Genymotion virtual device could not obtain an IP address 尝试了 http://blog.csdn.net/beiminglei/article/details/17399333 还是不行, 后来发现,官方推荐的Virtual和Genymotion 不匹配, 则使用...
State-of-the-art sequence labeling systems traditionally require large mounts of task-...We obtain state-of-the-art performance on both datasets — 97.55% accuracy for POS tagging and 91.21% F1 for NER.
《Cortex-M3之STM32嵌入式系统设计》开发软件 《Cortex-M3之STM32嵌入式系统设计》介绍了以ARMCortex-M3为内核的STM32F103增强型微控制器的特点,深入讲解其硬件和软件设计方法。本书分为STM32基础开发、STM32深入...
To make use of either more or less strict isolation levels in applications, locking can be customized for an entire session by setting the isolation level of the session with the SET TRANSACTION ...
var obtainUnicode = obtainUnicode || require ( '../obtain-unicode' ) . obtainUnicode ; console . log ( obtainUnicode ( 'beer!:clinking_beer_mugs:' ) ) ; // [98, 101, 101, 114, 33, 127867] console . ...
要查看的文件: (VB: ) 如何在ResponseCapabilities事件处理程序中获取可用WMS图层的集合 此示例演示如何获取Web Map Service支持的图层的集合。 描述 为此,请使用获取集合。
4. If you can access all the partitions, insert the floppy disk that contains the SYS.COM file into drive a: and type the following command: sys c: 5. Create a DOS directory on your hard disk. ...
获取IP到指定的文件中,是通过System函数执行IPconfig命令实现的
spring_jdbc jar文件 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at ...
for any damages that could potentially arise from it's use or misuse. ---------------------------------------------------------------------------------------------------- Third Party Unmodified Tools...