关闭

Hibernate pool资料收集

2026人阅读 评论(0) 收藏 举报

简化后的spring+hibernate配置
spring部分:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!--
            加入spring后,把dataSource定义交给hibernate.cfb.xml,感觉hibernat部分要完整些,与没加入spring基本没区别(不知道是好事还是坏事)
            <property name="dataSource">
            <ref bean="dataSource" />
            </property>
        -->
        <property name="configLocation">
            <value>classpath:/mapping/hibernate.cfg.xml</value>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <bean id="AbstractDAOProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        abstract="true">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean id="UserDAO"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        parent="AbstractDAOProxy">
        <property name="target">
            <!--
                   DAO直接定义在这儿,省得单独定义一个bean id="xxxxDAO"
                   由于是测试工程,就直接由测试代码操作DAO,不写services了
              -->
            <bean class="dao.UserDAOHibernate">
                <property name="sessionFactory">
                    <ref local="sessionFactory" />
                </property>
            </bean>
        </property>
    </bean>
</beans>

Hibernate.cfb.xml 部分(与没使用spring相同)
<?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 name="spring_hibernate">
        <property name="hibernate.connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <property name="hibernate.connection.url">
            jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =
            (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = 1521)) )
            (CONNECT_DATA = (SID = xx)(SERVER = DEDICATED)) )
        </property>
        <property name="hibernate.connection.username">xxxx</property>
        <property name="hibernate.connection.password">
            xxxxxxxxxxxx
        </property>
        <property name="hibernate.dbcp.maxActive">100</property>
        <property name="hibernate.dbcp.whenExhaustedAction">1</property>
        <property name="hibernate.dbcp.maxWait">120000</property>
        <property name="hibernate.dbcp.maxIdle">10</property>
        <property name="hibernate.dialect">
            org.hibernate.dialect.OracleDialect
        </property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.cache.provider_class">
            org.hibernate.cache.OSCacheProvider
        </property>
        <mapping resource="mapping/People.hbm.xml" />
        <mapping resource="mapping/Lob.hbm.xml" />
        <class-cache usage="read-only" class="domain.People" />
    </session-factory>
</hibernate-configuration>

 

 

 

 

 

 

最近用hibernate做东西,结合上网查一些资料,总结出Hibernate中连接数据的几种经典方式的Hibernate.cfg.xml文件配置,奉献出来,我调试通过:
第一种:用jdbc连接的配置                

<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sample</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">localhost</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="com/lzb/TUser.hbm.xml"></mapping>

第二种:用c3p0连接池的配置:

在hibernate.cfg.xml中加入

<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sample</property>
<property name="connection.password">root</property>
<property name="connection.username">root</property>
<!-- c3p0连接池的配置,它会自动调用jdbc驱动,管理数据库的连接 -->                
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">120</property>
<property name="hibernate.c3p0.acquire_increment">2</property>


第三种:用proxool连接池的配置:

在hibernate.cfg.xml中加入

<property name="proxool.pool_alias">pool1</property>
<property name="proxool.xml">ProxoolConf.xml</property>
<property name="connection.provider_class">net.sf.hibernate.connection.ProxoolConnectionProvider</property>

然后,在和hibernate.cfg.xml同一个目录下,加一个ProxoolConf.xml文件,内容为
<?xml version="1.0" encoding="utf-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>pool1</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>jdbc:mysql://localhost:3306/sample</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="root"/>
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>

 

 

 

 

 

hibernate.connection.driver_class=COM.ibm.DB2.JDBC.net.DB2Driver
hibernate.connection.url=jdbc:db2://server1/sample
hibernate.connection.username=db2admin
hibernate.connection.password=password
hibernate.default_schema=db2admin
hibernate.dialect=net.sf.hibernate.dialect.DB2Dialect
hibernate.show_sql=true
# The maximum number of active connections that can be allocated # from this pool at the same time, or zero for no limit.
hibernate.dbcp.maxActive 100
# Action to take in case of an exhausted DBCP statement pool
# ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.whenExhaustedAction 1
hibernate.dbcp.maxWait 120000
# The maximum number of active connections that can remain
# idle in the pool, without extra ones being released, or zero
# for no limit.
hibernate.dbcp.maxIdle 10
# The SQL query that will be used to validate
# connections from this pool before returning them to the caller.
# hibernate.dbcp.validationQuery=TODO
## prepared statement cache
hibernate.dbcp.ps.maxActive 100
# Action to take in case of an exhausted DBCP statement
#pool ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.ps.whenExhaustedAction 1
# The maximum number of milliseconds that the pool will
# wait (when there are no available connections) for a connection
# to be returned before throwing an exception, or -1 to
# wait indefinitely.
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 100

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:96573次
    • 积分:1374
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:16篇
    • 译文:0篇
    • 评论:11条
    最新评论
    bloger's blog