org.hibernate.TransactionException: JDBC rollback failed

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

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时,要把自动提交关闭.

 

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

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值