hibernate c3p0 mysql_hibernate-使用c3p0数据库连接池,以及其它配置

关键代码:

10

5

2

2000

2000

10

其它配置:

100

30

代码片段:

hibernate.cfg.xml:<?xml  version="1.0" encoding="UTF-8"?>

hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

root

111111

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Australia/Melbourne&

org.hibernate.dialect.MySQL5InnoDBDialect

true

true

update

10

5

2

2000

2000

10

100

30

pom.xml一定要使用hibernate-c3p0,使用原生c3p0会报错

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.shuoeasy

test

0.0.1-SNAPSHOT

jar

test

http://maven.apache.org

UTF-8

1.8

1.8

1.8

junit

junit

4.12

test

org.hibernate

hibernate-core

4.3.11.Final

mysql

mysql-connector-java

6.0.2

org.hibernate

hibernate-c3p0

4.3.11.Final

AppTest.java 测试是否为c3p0连接:package com.shuoeasy.test;

import java.sql.Connection;

import java.sql.SQLException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.jdbc.Work;

import org.hibernate.service.ServiceRegistry;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

/**

* Unit test for simple App.

*/

public class AppTest {

Session session;

SessionFactory sf;

@Before

public void init() {

Configuration conf = new Configuration().configure();

ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();

sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr);

session = sf.openSession();

session.beginTransaction();

System.out.println("init");

}

@After

public void destory() {

session.getTransaction().commit();

session.close();

sf.close();

System.out.println("dectory");

}

/**

* 查看是否为c3p0的数据库连接

*/

@Test

public void test() {

session.doWork(new Work() {

@Override

public void execute(Connection connection) throws SQLException {

// TODO Auto-generated method stub

System.out.println(connection);// 输出:com.mchange.v2.c3p0.impl.NewProxyConnection@5965be2d

}

});

}

}


版权声明:本文为weixin_42349787原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。