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请求 ...
随机推荐
- Spark 编程基础
1. 初始化Spark import org.apache.spark.{SparkContext, SparkConf} val conf=new SparkConf().setAppName(&q ...
- Android Studio上面最好用的插件
转载:http://www.jianshu.com/p/d76b60a3883d 在开发过程中,本人用的最爽的就是代码生成的插件,帮助我们自动完成大量重复简单的工作.个人也觉得代码自动生成工具是最值得 ...
- BZOJ4721 [Noip2016]蚯蚓
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- IRC(Internet Relay Chat Protocol) Protocal Learning && IRC Bot
catalogue . Abstract . INTRODUCTION . 通信协议Connection Registration Action . 通信协议Channel operations Ac ...
- ecshop /flow.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 ECSHOP的配送地址页面网页没有验证地区参数的有效性,存在sql注入漏 ...
- 树链剖分I 原理
树链剖分(Heavy Light Decomposition, HLD)是一种将对[树上两点间的路径]上[边或点]的[修改与查询]转化到[序列]上来处理的方法. 目的:将树的边或点转化到一个线性结构( ...
- UVa 11149 Power of Matrix(倍增法、矩阵快速幂)
题目链接: 传送门 Power of Matrix Time Limit: 3000MS Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...
- linux(centos) 项目部署阶段相关命令汇总
1.ssh免密码登陆主要命令cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys-->添加公钥service sshd restart -- ...
- Blast本地化
转载]Blast本地化:使用Blastall进行数据库比对 (2012-02-13 21:25:31) 用blastall进行序列比对 blastall是最常用的blast程序之一,其功能非常强大 ...
- SQL 字段保留下划线后部分
select SUBSTRING(b.SUMMARY,0,charindex('_',b.SUMMARY))as SUMMARY from UltimusDB.dbo.INCIDENTS b