Home > Database > Mysql Tutorial > body text

Hibernate错误解决(TransactionException:Transactionnotsuccess

WBOY
Release: 2016-06-07 15:56:05
Original
1882 people have browsed it

【关键的错误信息】: TransactionException: Transaction not successfully started 和一个 doCommit 从该关键字看,是使用Hibernate事务管理器(而不是Spring,下面详细log的绿色字体)处理事务的commit时抛出的, 【具体解决】: //下面的代码段会报错 Se

【关键的错误信息】: TransactionException: Transaction not successfully started 和一个doCommit

从该关键字看,是使用Hibernate事务管理器(而不是Spring,下面详细log的绿色字体)处理事务的commit时抛出的,

【具体解决】:

//下面的代码段会报错

        Session session =  getSessionFactory().getCurrentSession();  
        session.beginTransaction();          //
        String queryString = "update.... "; //具体数据库字段内容的更新
        Query query = session.createQuery(queryString);         
        query.executeUpdate();  
        session.getTransaction().commit(); //应该是这里导致了 org.hibernate.TransactionException: Transaction not successfully started
Copy after login
//改为下面的代码段好了
	Session session =  getSessionFactory().openSession();  
        Transaction tran = session.beginTransaction();   //这么修改后,ok
        String queryString = "update ....";
        Query query = session.createQuery(queryString);  
        query.executeUpdate();  
        tran.commit(); 
        session.close();
Copy after login

【事后诸葛亮】该错误在dao层的一个执行update的语句里面,但该处没有提示错误,而是在调用这个函数的上一级函数要return时抛出的。

1. 可能是session结束时的检查。

2. 没有申请transaction对象,直接使用函数导致了错误?

3. 而且虽然最后报错,但事实上已经成功的修改了数据库(持久化已经完成)。

4. 生搬硬套固然能快速实现,理解透彻才是正道。

5. 这么改可能会影响执行时间,多数据量时可能需要优化。

附:详细的stacktrace:

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)

Caused by: org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:130)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
... 77 more

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template