用SessionFactory.getCurrentSession执行hibernate操作时,hibernate的操作默认必须包含在一个transaction中,也就是开始要用session.begionTransaction得到一个transaction 实例(譬如tx), 操作结束时在这个实例上进行事务的提交tx.commit或回滚tx.rollback. 如果这些CRUD操作不被包括在一个具体的transaction中,hibernate就会抛出上述异常。
getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到当前线程绑定的session,而当前线程绑定的session是通过当前的事务产生的,如果你没有配置事务的话,当前线程threadlocal中就不存在 session,这样就出现no session错误
Solution:
first of all if you want to use getcurrentSession() method you need to add these properties to HibernateProperties:
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
second thing is from the exception, Hibernate3 doesn't allow you to run queries without starting a transaction. you have to add this to your init method before running the query:
sessionFactory.getCurrentSession().beginTransaction();
and of course commit it in the end of your unit of work.
到这这种情况的发生有两种情况:
1,没有配置事物只要在Spring配置文件中添加如下代码:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/>
然后在DAO程序前面加上@Transactional即可。
2,连接数据的配置是否正确,如果连接字符串不正确的话,就不能够创建SessionFactory,也就无从谈起事务了。
在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层使用transaction,事务被配置在service层上更为合理,因为业务层方法表示逻辑上的一个原子操作。在这种环境下,如果你遇到上述异常,请核查一下service层上有没有配置transaction,transaction有没有打开,service层配置的transaction是否正确。
分享到:
相关推荐
No Hibernate Session bound to thread,没有绑定线程解决办法,找了好久才找到的
Hibernate-nosession浅谈代码案例。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...
Name salesDataSource is not bound in this Context,连接池的问题
配置tomcat7sqlserver数据连接池java 利用数据源对象建立数据库连接时的错误: Name jdbc is not bound in this Context 解决方案
1. Introduction to Hibernate 1.1. Preface 1.2. Part 1 - The first Hibernate Application 1.2.1. The first class 1.2.2. The mapping file 1.2.3. Hibernate configuration 1.2.4. Building with Ant 1.2.5. ...
branch and bound
本案例主要是分别实现Spring与jdbc和hibernate结合的事务控制,如果该案例有出入的地方,请给我留言,我们一起交流
On a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search ...
SQL Server needs to lock data that does not exist! If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are ...
在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉
UIUC Math 482: Linear Programming. Lecture 33: The Branch-and-Bound Method(课程讲义)
NULL 博文链接:https://mentats.iteye.com/blog/1883825
“POINT TO POINT CALLS” means any call made directly between no more than two users on compatible devices that does not utilize POLYCOM’s multipoint bridging technology. POINT TO POINT CALLS may not...
If you do not agree to all of the terms and conditions contained in this EULA, you may not install or use the SOFTWARE. If you have already installed or begun to install the SOFTWARE you should ...
MATLAB分支定界法求解,大家
Authored by industry veteran and Python expert Will McGugan, who most recently worked on the MotorStorm game for Play Station 3, youll be privy to insights that will not only help you to exploit ...
END-USER LICENSE AGREEMENT FOR MICROSOFT SOFTWARE IMPORTANT - READ CAREFULLY: This End-User License Agreement ... If you do not agree to the terms of this EULA, do not install, copy, or use the Product.
(i) to install and use the Registered Software on one or more computer(s) or storage media, provided that the Registered End User does not save the Registration Key on any computer; or (ii) to ...
分支定界法,branch and bound用于slinding window 图像识别的应用,linux下的应用环境。原作者的源码,仅限大家学习,研究。