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());
 }

}

Hibernate获取Connection的更多相关文章

  1. Hibernate Session 获取connection

    Hibernate Session 获取connection 由于最近一个项目要用到一条辅助的SQL ,hibernate里面的SQLQuery API 总的SQL语句不能包含 : 冒号, 固放弃Hi ...

  2. Hibernate4获取Connection,ResultSet对象

    项目中需要一个json对象,封装的时候,需要数据的列名. 在jdbc里面,可以有个ResultMetaData对象获取列名字.因为我用的是hibernate,这个框架已经封装了很多,一般是难以获得re ...

  3. atitit.获取connection hibernate4

    atitit.获取connection hibernate4 1. SessionFactoryUtils法(推荐) 1 2. ConnectionProvider 法( ) 1 3. 嘎自实现法(不 ...

  4. Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

    docs.jboss.org文档示例代码:(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/) sta ...

  5. 不同版本Hibernate.获取SessionFactory的方式

    不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...

  6. 通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题

    在使用hibernate的应用中,如果需要直接使用Java.sql.Connection,一般我们是通过hibernate的session.connection()获得的,然后session.clos ...

  7. java业务逻辑,利用hibernate获取所连接的数据库信息

    1.本人程序架构是springMVC+hibernate,这次的需求是要针对不同的数据库,做不同的处理. 2.获取所连接的数据库是什么,oracle?  mysql?  sql server?  基础 ...

  8. hibernate将connection放进threadlocal里实现数据库连接池

    Why ThreadLocal? 无论如何,要编写一个多线程安全(Thread-safe)的程序是困难的,为了让线程共享资源,必须小心地对共享资源进行同步,同步带来一定的效能延迟,而另一方面,在处理同 ...

  9. jdbc 获取connection 对象的三种方式

    获取数据库连接方法一:驱动实现类 //创建mysql的Driver对象 Driver driver=new com.mysql.jdbc.Driver(); //jdbc url 定位一个数据库: S ...

随机推荐

  1. INS-30001 ADMIN口令为空

    1.错误描写叙述 2.错误原因 管理口令为空.导致出错 3.解决的方法 填写管理口令和确认口令

  2. GREENPLUM简单介绍

    原帖:http://www.itpub.net/thread-1409964-1-1.html 什么是GREENPLUM? 对于非常多IT人来说GREENPLUM是个陌生的名字.简单的说它就是一个与O ...

  3. Rabbit.Rpc

    .NET轻量级RPC框架:Rabbit.Rpc 最近准备写一个.NET的管理平台应用在公司,由于存在大量的Client => Server,Server => Client的请求需求在加上 ...

  4. Wince下实现ImageButton

    我们在winform中给按钮设置个背景图片超级简单,是不?可是在wince下面就没那么简单了,下面我来介绍一种方式来实现ImageButton. 实现思路是重新写一个usercontrol就ok.具体 ...

  5. boost 分析命令行参数

    #include <boost/program_options.hpp> #include <iostream> #include <vector> using n ...

  6. elasticsearch的javaAPI之query

    elasticsearch的javaAPI之query API the Search API同意运行一个搜索查询,返回一个与查询匹配的结果(hits). 它能够在跨一个或多个index上运行, 或者一 ...

  7. PHP - 计算执行程序耗时

    效果: 首先在includes文件夹下编写,global.func.php函数库: <?php /* * Version:1.0 * CreateTime:2015年11月11日 * Autho ...

  8. 3890: [Usaco2015 Jan]Meeting Time( dp )

    简单的拓扑图dp.. A(i, j), B(i, j) 表示从点 i 长度为 j 的两种路径是否存在. 用bitset就行了 时间复杂度O(m) --------------------------- ...

  9. BZOJ 3173: [Tjoi2013]最长上升子序列( BST + LIS )

    因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn) --------------------------------- ...

  10. Struts2运行机制(MVC)的分析:

    C:(controller)控制器          M:(model)模型处理    V:(view)视图 Struts 2 的运行过程:     核心控制器是FilterDispatcher会过滤 ...