org.hibernate.TransactionException: nested transactions not supported

贴主要部分代码

        public PlUser findByUsernameAndPassword(String username, String password) {
		Session sx = this.getHibernateTemplate().getSessionFactory().openSession();
		Transaction tx = sx.getTransaction();
		tx.begin();
		PlUser user = (PlUser) sx.createQuery("from PlUser u where u.username=? and u.password=?")
				.setString(0, username)
				.setString(1, password)
				.uniqueResult();
		tx.commit();
		sx.close();
		return user;
	}

这是修改后正确的代码。

原先的代码:

        public PlUser findByUsernameAndPassword(String username, String password) {
		Session sx = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
		Transaction tx = sx.getTransaction();
		tx.begin();
		PlUser user = (PlUser) sx.createQuery("from PlUser u where u.username=? and u.password=?")
				.setString(0, username)
				.setString(1, password)
				.uniqueResult();
		tx.commit();
		sx.close();
		return user;
	}

提示:org.hibernate.TransactionException: nested transactions not supported

而后删除Transaction tx那一行,提示:org.hibernate.SessionException:
Session is closed

尽管在applicationContext.xml那里配置了连接池,初始化为20条连接,但似乎并没效果。

getHibernateTemplate()可参考http://blog.csdn.net/zmhinzaghi/article/details/7091629

个人案例,请谅解。

时间: 2024-10-14 17:19:19

org.hibernate.TransactionException: nested transactions not supported的相关文章

hibernate(nested transactions not supported)异常

org.hibernate.TransactionException: nested transactions not supported错误的解决方法! 原因:事务没有提交,事务提交后正常 Transaction tx = session.beginTransaction(); tx.commit(); //缺少这句话

hibernate exception nested transactions not supported 解决方法

开启事务之前先判断事务是否已经打开,方法如下: JdbcTransaction tx=(JdbcTransaction) session.beginTransaction(); 改为JdbcTransaction tx = session.getTransaction().getLocalStatus()==LocalStatus.ACTIVE?(JdbcTransaction)session.getTransaction():(JdbcTransaction) session.beginTra

Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML

1. 环境准备 Spring , Spring Data, JPA, HIbernate, JDK 1.7 2.  问题提出 在使用Spring Data 来更新数据之时,爆出了如下错误信息: @Query("UPDATE User u SET u.state = ?1 WHERE u.server.id = ?2") public void updateAllUsers(long state, long serverid); 错误信息如下: org.springframework.d

500 - org.hibernate.TransactionException: JDBC rollback failed

Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: JDBC rollback failed Caused by: java.sql.SQLException: Couldn't perform the operation rollback: You can't perform any operations on this connection. It

org.hibernate.TransactionException: commit failed

1.错误描述 org.hibernate.TransactionException: commit failed at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:187) at filter.TranscationFilter.doFilter(TranscationFilter.java:35) at org.apache.catalina.c

Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection

1.错误描述 Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116) at org.hibernate.engine.transaction.spi.AbstractTransa

System.InvalidOperationException nested transactions are not supported

如下bll方法,在执行时会报事务嵌套异常.bll方法里开启了分布式事务,dal方法里又启动了数据库事务.通过查看异常堆栈,发现异常是在执行BillsDal.Add(bill);方法里的var trans = conn.BeginTransaction();这条语句抛出来的. 持久层框架:dapper, db:mysql. bll方法: public static bool AddInterestBill(t_bills bill, t_info_jxdetailed his) { using (

org.hibernate.TransactionException: Transaction not successfully started

http://www.yinyuetai.com/playlist/3310702http://www.yinyuetai.com/playlist/3310711http://www.yinyuetai.com/playlist/3310712http://www.yinyuetai.com/playlist/3310724http://www.yinyuetai.com/playlist/3310725http://www.yinyuetai.com/playlist/3310739http

关于hibernate的一些事

关于hibernate的一些事 (此博文为http://my.oschina.net/u/555061/blog/506216后续) 1.简单说一下SessionFactory.Session 源自: http://blog.csdn.net/javaloveiphone/article/details/8155340 1.1.Hibernate中SessionFactory对象的创建代价很高,它是线程安全的对象,被设计成可以为所有的应用程序线程所共享.通常,SessionFactory会在应用