Hibernate学习记录5 配置C3P0
2017-04-15 15:54
323 查看
Spring+Hibernate+C3P0
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.whr</groupId> <artifactId>info</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>info Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <slf4j.version>1.7.21</slf4j.version> <spring.version>4.3.0.RELEASE</spring.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.15.0-GA</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.0.GA</version> </dependency> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.1_spec</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.0.1.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">jdbc</property> <property name="connection.password"></property> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">10</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.timeout">3600</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="current_session_context_class">thread</property> <property name="hibernate.c3p0.testConnectionOnCheckout">true</property> <mapping resource="com/cn/ustcit/rbac/RbacAdmin.hbm.xml" /> <mapping resource="com/cn/ustcit/rbac/RbacAdminLog.hbm.xml" /> <mapping resource="com/cn/ustcit/welllid/WelllidItemsLog.hbm.xml" /> <mapping resource="com/cn/ustcit/welllid/WelllidItems.hbm.xml" /> </session-factory> </hibernate-configuration>
Hibernate+C3P0,不用pom.xml
引用jar包:- c3p0.0.9.5-pre10.jar
- hibernate-c3p0-4.2.3.Final.jar
- mchange-commons-jar-0.2.12.jar
- mysql-connector-jar-5.1.26-bin.jar
- 其它hibernate包
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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">jdbc</property> <property name="connection.password"></property> <property name="show_sql">true</property> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">10</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.timeout">3600</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="current_session_context_class">thread</property> <property name="hibernate.c3p0.testConnectionOnCheckout">true</property> <property name="hibernate.connection.release_mode">after_statement</property> <mapping resource="com/cn/ustcit/welllid/WelllidItemsLog.hbm.xml" /> </session-factory> </hibernate-configuration>
另外:
给连接字符串加上自动重连
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
相关文章推荐
- 【Hibernate学习笔记-4】在hibernate.cfg.xml中配置C3P0数据源
- Hibernate学习-12-分页,c3p0配置
- hibernate二级缓存配置学习记录
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等
- Hibernate学习记录3 Hibernate 配置多数据源
- Spring 结合 Hibernate 配置 C3P0
- Hibernate使用C3P0的连接池,并队c3p0配置。
- MOSS Search学习记录(二):配置爬网并尝试一个简单的搜索
- MOSS Search学习记录(二):配置爬网并尝试一个简单的搜索
- Java学习之hibernate配置要点详谈
- 传智播客hibernate学习,开发环境配置
- hibernate c3p0的配置
- Spring 结合 Hibernate 配置 C3P0
- 传智播客oa学习,配置struts+ hibernate + jbpm
- Spring+Hibernate 配置和使用c3p0和proxool连接池
- Hibernate 中配置C3P0连接池
- Spring 结合 Hibernate 配置 C3P0
- Spring 结合 Hibernate 配置 C3P0
- c3p0在hibernate连接配置
- MOSS Search学习记录(三):配置搜索范围