`
applefan
  • 浏览: 18980 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

hibernate3 的事务管理

阅读更多
以下是一个例子
==============================
package com.adasoft.sms.dao.hibernate;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class test extends HibernateDaoSupport{

/**
*
* @param insertList
* @return
*/
  public boolean InsertEmployees(List insertList){
  boolean result=true;
//   SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
//   Session session= sessionFactory.openSession();
  Session session=SessionFactoryUtils.getSession(getSessionFactory(), true);

  session.setFlushMode(FlushMode.COMMIT );
  Transaction tx = null;
  try {
      tx = session.beginTransaction();
      for(int i=0;i<insertList.size();i++){
      //Employee employee=new Employee();
      //employee=(Employee)insertList.get(i);
      //this.saveEmployee(employee);
                  //.....
      }
      tx.commit();
  }
  catch (RuntimeException e) {
      if (tx != null) tx.rollback();
      result=false;
      throw e; // or display error message
    
  }
  finally {
      session.close();     
  }
  return result;
  }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics