Spring c3p0连接池通过Hibernate配置

Posted Claricre

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring c3p0连接池通过Hibernate配置相关的知识,希望对你有一定的参考价值。

  首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html

  然后调用这三个包。

配置hibernate.cfg.xml文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">5</property>
        <property name="hibernate.c3p0.acquire_increment">30</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/model/Nation.hbm.xml" />
    </session-factory>
</hibernate-configuration>

实现类

package com.test;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Session;

import com.model.Nation;

public class Test {

    public static void main(String[] args) {
        
        long start = Calendar.getInstance().getTimeInMillis();
        
        Session session = HibernateUtil.getSession();
        //Nation data = session.load(Nation.class, "n001");
        List<Nation> list = session.createQuery("from Nation").getResultList();
        for(Nation data :list){
            System.out.println(data);
        }
        HibernateUtil.closeSession();
        long end = Calendar.getInstance().getTimeInMillis();
        System.out.println(end-start);

    }

}

对于hibernate来说,用不用连接池的效率几乎相同。

以上是关于Spring c3p0连接池通过Hibernate配置的主要内容,如果未能解决你的问题,请参考以下文章

C3P0使用详解

JAVA连接池技术

hibernate中如何使用c3p0连接池?

Hibernate c3p0 连接池不会超时空闲连接

c3p0 连接池

c3p0 数据库连接池