Hibernate4:如何配置C3P0连接池以及二级缓存?

来源:互联网 发布:手机淘宝店铺管理在哪 编辑:程序博客网 时间:2024/06/06 02:44

首先,你要有对应的JAR包,以下是maven的dependency代码:

<dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-c3p0</artifactId>  <version>4.1.9.Final</version></dependency>
<dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-ehcache</artifactId>  <version>4.1.9.Final</version></dependency>
其次,是hibernate.cfg.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>        <property name="hibernate.c3p0.max_size">10</property>        <property name="hibernate.c3p0.min_size">1</property>        <property name="hibernate.c3p0.max_statements">3</property>        <property name="hibernate.c3p0.timeout">30</property>        <property name="hibernate.c3p0.acquire_increment">1</property>        <property name="hibernate.c3p0.idle_test_periodt">10</property>                <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/demo</property>        <property name="hibernate.connection.username">postgres</property>        <property name="hibernate.connection.password">postgres</property>        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>        <property name="hibernate.show_sql">true</property>        <property name="hibernate.current_session_context_class">thread</property>        <property name="hibernate.cache.use_query_cache">true</property>        <property name="hibernate.cache.use_second_level_cache">true</property>        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>                <mapping class="com.homeland.myapp.entity.Employee"/>        <mapping class="com.homeland.myapp.entity.EmployeeDetails"/>        <mapping class="com.homeland.myapp.entity.Department"/>        <mapping class="com.homeland.myapp.entity.Role"/>    </session-factory></hibernate-configuration>
最后,是entity类的annotation代码:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
这一段加在类的开头。



原创粉丝点击