文章出處

Hibernate3.3.2版本中getSession().connection()已被棄用,hibernate4中官方推薦使用Session doWork()方法進行jdbc操作

首先看看Work接口類的定義

public interface Work {
//Execute the discrete work encapsulated by this work instance using the supplied connection.
//@param connection The connection on which to perform the work.
// @throws SQLException Thrown during execution of the underlying JDBC interaction.
// @throws HibernateException Generally indicates a wrapped SQLException.
public void execute(Connection connection) throws SQLException;
}

具體代碼如下:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestDoWork(){
public void testSessionDowork() throws Exception {
        Session session = getSession();
        final String sql="select * from t_cp_user";
        try{
             session.beginTransaction();
             session.doWork(
//定義一個匿名類,實現了Work接口
                     new Work() {
                         public void execute(Connection connection) throws SQLException {
//經由過程JDBC API執行SQL語句
                             PreparedStatement ps = connection.prepareStatement( sql );
                             ResultSet rs = ps.executeQuery();
                             try {
                                 ResultSetMetaData metadata = rs.getMetaData();
                              while (rs.next()) {
                                user.setUserId(rs.getLong("USER_ID"));
                                user.setUsername(rs.getString("USERNAME"));
                              }
                             }
                             finally {
                                 doClose(null,ps,rs);
                             }
                         }
                     }
             );
             session.getTransaction().commit();
             //session.close();
        }catch(Exception ex){
            log.error(ex,ex);
        }
        finally{
            this.doClose(session, null, null);
        }
      }
     //釋放數據資源 by rhine
      
    protected void doClose(Session session, Statement stmt, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
                rs=null;
            } catch (Exception ex) {
                rs=null;
                log.error(ex,ex);
                ex.printStackTrace();
            }
        }
        // Statement對象關閉時,會自動釋放其管理的一個ResultSet對象
        if(stmt != null){
            try {
                stmt.close();
                stmt=null;
            } catch (Exception ex) {
                stmt=null;
                log.error(ex,ex);
                ex.printStackTrace();
            }
        }
//      當Hibernate的事務由Spring接管時,session的關閉由Spring管理.不用手動關閉
//      if(session != null){
//          session.close();
//      }
    }

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()