在hibernate3中,使用了c3p0连接池,尝试了多种办法取得connection对象,以下两种可以使用。

Java代码 
     Connection conn;  

    // 方法1:hibernate4中将要废弃这个方法
conn = session.connection(); // 方法2:这个方法也可以用,速度稍慢
SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor)new Configuration().configure().buildSessionFactory();
conn = sessionFactory.getConnectionProvider().getConnection(); //方法:3
ConnectionProvider cp =((SessionFactoryImplementor)sessionFactory).getConnectionProvider();
cp.getConnection();

按hibernate的计划,4.0开始将除去Session.connection()这个方法,所以还是最好不要使用它了。
官方的替代方法是用Session.doWork();
如:

Java代码 
         getSession().doWork(
new Work() {
public void execute(Connection connection) {
// 这里已经得到connection了,可以继续你的JDBC代码。
// 注意不要close了这个connection。
}
}
);

另外一个方法:

package com.trendcom.base.util;

import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.web.context.ContextLoaderListener; public class DataSourceUtil { private static SessionFactory sessionFactory=null; static{
sessionFactory=(SessionFactory) ContextLoaderListener.getCurrentWebApplicationContext().getBean("sessionFactory");
} public static Connection getConnection(){
try {
return getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} public static SessionFactory getSessionFactory() {
return sessionFactory;
} public static void setSessionFactory(SessionFactory sessionFactory) {
DataSourceUtil.sessionFactory = sessionFactory;
} private static DataSource getDataSource() {
return SessionFactoryUtils.getDataSource(getSessionFactory());
} }

hibernate4中取得connection的方法的更多相关文章

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

    Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...

  2. 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set

    <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  3. ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)

    一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...

  4. 使用JDBC从数据库中查询数据的方法

    * ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...

  5. nginx 学习笔记(4) Connection处理方法

    nginx支持connection的多种处理方法.nginx运行的平台确定了可用的处理方法. 在特定平台下connection的多种处理方法中,nginx会自动选择一种最有效的方法.然而,如果需要,你 ...

  6. php面向对象类中常用的魔术方法

    php面向对象类中常用的魔术方法   1.__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例: //__construct( ...

  7. android中The connection to adb is down,问题和解决

    android中The connection to adb is down,问题和解决   自己总结的在android中常会出现的不好解决的问题和方法(其中第三个方法经过了四天的折磨.....哎) 1 ...

  8. HTTP协议中request报文请求方法和状态响应码

    一个HTTP请求报文由4部分组成: 请求行(request line) 请求头部(header) 空行 请求数据 下图给出了请求报文的一般格式: 请求行中包括了请求方法,常见的请求方法有: GET:从 ...

  9. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

随机推荐

  1. iOS推送通知

    推送通知 此通知非彼通知. NSNotification是抽象的,看不见的,但是可以监听,属于观察者模式的一种设计模式. 推送通知是可见的,能用肉眼看见的,是真正的和用户打交道的通知. 推送通知分为两 ...

  2. python-汉诺塔递归实现

    摘录自廖雪峰老师教程下的评论,个人备忘,脑细胞已死光 def move(from,to): #将盘子从from移动到to,动画效果需要脑补 print(from,'->',to) def han ...

  3. bzoj2959: 长跑

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. UOJ34 多项式乘法

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  5. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  6. C语言实现贪吃蛇源码

    先放效果 源代码 //2016-2-12 //zhaoyu //Gmail:zhaoyu1995.com@gmail.com //Language: C //Platform:Code::Blocks ...

  7. Error: cannot find a valid baseurl for repo: rpmfusion-free 解决办法

    今天在玩CentOS的时候出现了: Error: cannot find a valid baseurl for repo: rpmfusion-free 这个问题真到好恶心啊,以前一直使用到是ubu ...

  8. BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊

    传送门 Link-Cut-Tree套路题 //BZOJ 2002 //by Cydiater //2016.9.12 #include <iostream> #include <cs ...

  9. break; continue; goto; return在循环中的应用

    1. break表示跳出循环,程序指向循环体后的第一条语句: ; ) { ) break; console.writeline("{0}",i++); } console.read ...

  10. 最简单的jQuery插件

    <script src="./jquery-1.7.1.min.js"></script><script>;(function($,undefi ...