日期:2014-05-20  浏览次数:20826 次

菜鸟求教:hibernate编程式事务不能回滚;
junit test:
Java code

      public void test(){
        User user=new User();
        user.setUserName("lisi");
        UserManager userManager=new UserManagerImpl();
        userManager.addUser(user);
    }


UserManagerImpl
Java code

public class UserManagerImpl implements UserManager{
    
private LogManager logManager=new LogManagerImpl();

    public void addUser(User user) {
       Session session=null;
       Transaction tx=null;
       try {
        session = HibernateSessionFactory.getSessionFactory()
                .getCurrentSession();
        tx=session.beginTransaction();
        session.save(user);
        Log log = new Log();
        log.setType("anquanrizi");
        Integer.parseInt("asdfasf");
        log.setCont("XXX jinru xitong");
        logManager.addLog(log);
        
        tx.commit();
    } catch (RuntimeException e) {
        tx.rollback();
    }
    }
    
    
    public void setLogManager(LogManager logManager) {
        this.logManager = logManager;
    }
    

}


LogManagerImpl
Java code

package com.log.manager;

import org.hibernate.Session;

import com.HibernateSessionFactory;
import com.log.entity.Log;

public class LogManagerImpl implements LogManager{

    public void addLog(Log log) {
        Session session=null;
        session = HibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
        session.save(log);
        
    }

}




------解决方案--------------------
默认commit是提交的需要手动把它设置:
Connection conn= new Connection();
conn.setAutoCommit(false)
------解决方案--------------------
那可能存在缓存!你可以讲缓存清理
!吧myeclipse中的缓存清理一下在试试
------解决方案--------------------
探讨
默认commit是提交的需要手动把它设置:
Connection conn= new Connection();
conn.setAutoCommit(false)

------解决方案--------------------
session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
改为
session = HibernateSessionFactory.getSessionFactory().openSession();
试试