---
title: "hibernate取得连接"
author: "weishuwei"
date: "2007-03-28T10:48:06Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2007-03-28T10:48:06Z"
id: 66008
url: "https://weishuwei.iteye.com/blog/66008"
---

1,Connection conn = session.getBatcher().openConnection();  
try {  
....................................................................................  
}catch (SQLException e) {  
   session.getBatcher().abortBatch(e);//注意抛出异常时要有这句  
   e.printStackTrace();  
  }finally{  
   if(conn!=null){  
    try {  
     session.getBatcher().closeConnection(conn);//  
     conn.close();  
    } catch (SQLException e) {  
     e.printStackTrace();  
    }  
   }

2,Connection conn1=session.connection();  
  Transaction tr=session.beginTransaction();  
  try{  
...............................................................  
  tr.commit();  
}catch(Exception e){  
  tr.rollback();  
}

总结:两个都是从hibernate获取Connection的方式,但第一个默认自动提交为true,第二个则不是自动提交,要tr.commit();才好提交