hibernate4中取得connection的方法
在hibernate3中,使用了c3p0连接池,尝试了多种办法取得connection对象,以下两种可以使用。
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();
如:
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的方法的更多相关文章
- hibernate4中使用Session doWork()方法进行jdbc操作(代码)
Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...
- 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)
一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...
- 使用JDBC从数据库中查询数据的方法
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...
- nginx 学习笔记(4) Connection处理方法
nginx支持connection的多种处理方法.nginx运行的平台确定了可用的处理方法. 在特定平台下connection的多种处理方法中,nginx会自动选择一种最有效的方法.然而,如果需要,你 ...
- php面向对象类中常用的魔术方法
php面向对象类中常用的魔术方法 1.__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例: //__construct( ...
- android中The connection to adb is down,问题和解决
android中The connection to adb is down,问题和解决 自己总结的在android中常会出现的不好解决的问题和方法(其中第三个方法经过了四天的折磨.....哎) 1 ...
- HTTP协议中request报文请求方法和状态响应码
一个HTTP请求报文由4部分组成: 请求行(request line) 请求头部(header) 空行 请求数据 下图给出了请求报文的一般格式: 请求行中包括了请求方法,常见的请求方法有: GET:从 ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
随机推荐
- js-JavaScript高级程序设计学习笔记3
第五章 引用类型 1.使用new操作符和Date构造函数创建日期对象. 2.创建特定日期有两个方法--Date.parse()和Date.UTC().后者小时数为0时显示时间是8点,应该是因为本地是东 ...
- 硬盘分区时GPT和MBR的区别/选择
最明显的区别是MBR最大支持2T的硬盘,而GPT则更大. 1.最先出现在Windows8中设置新磁盘,系统会询问你是想要使用MBR还是GPT分区,GPT是一种新的硬盘分区标准.GPT带来了很多新特性, ...
- hdu 5035 概率题
直接推公式的题目了.... Refer:http://blog.csdn.net/u012139398/article/details/39458623 https://www.zybuluo.com ...
- CodeForces 37E Trial for Chief
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Description Having ...
- Windows Directory ACL Security Check By ACL Baseline
catalog . Windows NTFS ACL(MAC) Permission . How the System Uses ACLs . 服务器不安全ACL配置带来的攻击向量 . NTFS AC ...
- primefaces 通过selectOneMenu更新显示隐藏区域
最重要的是update的区域要指定整个panel,而不是想更新的那个组件 <h:form id="frm"> <h:panelGrid id="pane ...
- Bumped Map And Normal Map
http://freespace.virgin.net/hugo.elias/graphics/x_polybm.htm 先留着,准备以后开垦
- 9 HTML&JS等前端知识系列之Ajax post请求带有token向Django请求
我们 在母板上写入这段代码: <script type="text/javascript"> // 个人定义大函数,不是重点,可以忽略 $(document).read ...
- STM8L --- External interrupt
note 1: Several interrupts can be pending at the same time. When an interrupt request is not servic ...
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
在struts2.3.20下,web.xml中使用 会出现*********************************************************************** ...