异常栈:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
at org.springside.modules.orm.hibernate.SimpleHibernateDao.getSession(SimpleHibernateDao.java:90)
at org.springside.modules.orm.hibernate.SimpleHibernateDao.createCriteria(SimpleHibernateDao.java:284)
at org.springside.modules.orm.hibernate.SimpleHibernateDao.findUniqueBy(SimpleHibernateDao.java:165)
使用SessionFactory.getCurrentSession执行hibernate操作时,hibernate的操作默认必须包含在一个transaction中,也就是开始要用session.begionTransaction得到一个transaction 实例(譬如tx), 操作结束时在这个实例上进行事务的提交tx.commit或回滚tx.rollback. 如果这些CRUD操作不被包括在一个具体的transaction中,hibernate就会抛出上述异常。
在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层使用transaction,事务被配置在service层上更为合理,因为业务层方法表示逻辑上的一个原子操作。在这种环境下,如果你遇到上述异常,请考虑一下service层上没有配置transaction,transaction有没有打开!
分享到:
相关推荐
No Hibernate Session bound to thread,没有绑定线程解决办法,找了好久才找到的
问题描述 用SpringBoot + Spring Data JPA操作数据库 项目启动的时候 报了一个错 SpringBoot的版本是2.2.6.RELEASE org.springframework.beans.factory.BeanCreationException: Error creating bean with name '...
由于项目需求的需要,我们引入了连接池。...我们采用了Hibernate,所以可以考虑hibernate自带的连接池机制,但是发现效率不高,而且Hibernate也推荐使用c3p0或Proxool连接池,在我们的项目中采用了Proxool
2、报错:org.hibernate.HibernateException: No Session found for current thread 意思是必须在transcation.isActive()条件下才能执行, 可以解决办法是:当方法不需要事务支持的时候,使用 Session ...
Hibernate3技术精辟详解 Hibernate3 Hibernate3详解,看过就知道Hibernate3怎么配置和应用
* Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */ public ...
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal....
6.8 HibernateException,Checked 6.9 Template/Callback 简化业务方法 6.10 版面:一棵自身循环的树 6.11 版面与帖子:Master/Detail 主从关系 6.12 帖子树及其分页 6.13 与Web层交互 6.14 帖子的多形扩展:...
1.Caused by: org.hibernate.HibernateException: Connection cannot be null when ‘hibernate.dialect’ not set 报错就配置吧 启动项目不报错了,但是访问项目报错了 2.访问报错...
6.8 HibernateException,Checked 6.9 Template/Callback 简化业务方法 6.10 版面:一棵自身循环的树 6.11 版面与帖子:Master/Detail 主从关系 6.12 帖子树及其分页 6.13 与Web层交互 6.14 帖子的多形扩展:...
hibernate详细解析.pdf文档,所有的都有。。
public void executeTransactionBySQL() throws HibernateException { TestJTAByOracleVo vo1=new TestJTAByOracleVo(); vo1.setName("3333333"); HibernateByOracleDAO.addByOracle(vo1); ...
文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说web.xml配置: [java] view plaincopy 01.<?xml version="1.0" encoding="UTF-8"?> 02.<web-app xmlns:xsi=...
public void deleteUsers(Users users) throws HibernateException { Session session = HibSessionFactory.currentSession(); Transaction tx = session.beginTransaction(); session.delete(users); tx....
Session session=sessionFactory.openSession(); String hql="from User as u where u.username=? and u.userpass=? and u.userright=?"; Query query=session.createQuery(hql) ; query.setString(0, u....
public List<News> doInHibernate(Session session) throws HibernateException,SQLException { List<News> list = null; Query query = session.createQuery("from News order by id desc"); query....
例如: 代码如下:HibernateTest.java ... public class HibernateTest { public static void main(String[] args) throws HibernateException { SessionFactory sessionFactory = new Configuration().configure().bui