WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - Communications link failure

后端 未结 2 1082
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 03:57

I have problem with Hibernate (hibernate-core-4.1.9.Final.jar)

Case 1: Hibernate testing inside for loop. Everything went well.

for(in         


        
2条回答
  •  一生所求
    2020-12-18 04:16

    I had the same issue just because I forgot to wrap my hibernate code in a transaction. Apparently the connection to DB is not closed until you commit a transaction. Here is a valid example:

    Session session = HibernateUtils.getSession();
    Transaction tx = session.beginTransaction();
    Category cat = null;
    
    try{
        cat = (Category)session.get(Category.class, 1);
        tx.commit();
    }catch(Exception ex){
        tx.rollback();
    }finally{           
        session.close();
    }
    
    return cat;
    

提交回复
热议问题