org.hibernate.TransactionException: JDBC rollback failed

来源:互联网 发布:数据科学实战手册 pdf 编辑:程序博客网 时间:2024/06/05 03:16

    最近有遇到很多新的问题,也解决了不少问题,不过因为时间紧也没做记录,做下整理,然后做个记录,已备以后忘了.

org.hibernate.TransactionException: JDBC rollback failed这个错误也折磨我好几天了,最无奈的就是总要服务器重起,连到一定时间就自动挂了,我还要重起一下服务器,就又恢复了正常,我写的方法起实很简单就一个录入,

 

public void NewRecord(LawInfo li) {

        Session session 
= null;
        Transaction tr 
= null;
        List list
=null;
        
try{
            session 
= HibernateSessionFactory.currentSession();
            tr 
= session.beginTransaction();
            session.save(li);
            tr.commit();
        }
 catch (HibernateException he) {
            
if(tr!=null)
            System.
out.println("录入出错" + he.getMessage());
            tr.rollback();
        }
 finally {
            session.close();
        }

    }

 过一段时间就要重起一下实在很郁闷,不过也没找出什么好的解决方法

看了一个帖子很有启发,

 

It is quite simple, make sure turn autcommit off for your database 

Well, first of all, your code 
is actually read user information from database, not write to database, so you do not have to use transaction and rollback method.

Secondly, when you update or insert a record to the database, when you use transaction, the database should turn autcommit off already, but I am not sure 
do you use your own connection pool or not. Please check your connection pool as well. 

Thirdly, when you have an error, make sure you always close your hibernate connection. When you 
catch an Exception, please use the finally to close the hibernate session or connection.
Otherwise you probably will run 
out of connections.

Please put your detailed error stack trace here 
if you need more tips for this problem 

 

我认为他说的很有道理,

 我个人认为这个错误的引起有两种情况,不一定都是对的,

1) 连接中断,在操作过程中程序的连接与数据库间的连接突然断开,引起断开的原因很多,有网络问题的,也有程序自身问题的,比如内存溢出.这里就不多说了,连接中断导致操作的中断,jdbc的事物处理就会抛出这样的异常.而这个问题也是最常见的.

2)就是上面所说的,使用hibernate时,要把自动提交关闭.

 

再就是养成好的编码习惯,我以前编码习惯也很不好,过一段时间自己都不知道是用来做什么用的了,也不利与对程序的排错,调试.   大家一起共勉.

 

原创粉丝点击