hibernate4中使用Session doWork()方法进行jdbc操作(代码)

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();
//      }
    }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值