• 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来说,用不用连接池的效率几乎相同。

  • 相关阅读:
    设置开机启动时指定非ROOT用户执行相应的脚本
    passwd的使用
    redis安装过程中遇到的问题
    linux增大交换分区
    初学Pexpect
    系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    CentOS系统下各文件夹的作用
    Python的学习
    syntax error near unexpected token `then'
    10.24 小组会议记录
  • 原文地址:https://www.cnblogs.com/claricre/p/6665649.html
Copyright © 2020-2023  润新知