关于spring+hibernate配置文件配置数据连接池(来源于JAVA视线)

源地址:http://www.iteye.com/t/18095.html

这些连接池的配置按照hibernate的文档配置没有问题,这段时间使用spring的时候配置的是c3p0的数据库连接池,

按照网上某些网友的介绍的配置,在日志中我没有看到连接池的初始化信息,为此自己研究,发现应该如此配置。
先我把配置写下,希望大家以后在配置的时候,不要再走弯路了

代码
 <?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- ========================= Start of PERSISTENCE DEFINITIONS ========
================= --> 
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 

<property name="location">
<value>classpath:init.properties</value>

<
/property>
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"
/>

 

<bean id="oracleLobHandle" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor"/>
</property>
<
/bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="driverClass">
<value>${datasource.driverClassName}</value>
<
/property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
<
/property>

<property name="properties">
<props>

<prop key="c3p0.minPoolSize">${hibernate.c3p0.minPoolSize}</prop>
<prop key="hc3p0.maxPoolSize">${hibernate.c3p0.maxPoolSize}<
/prop>
<prop key="hc3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="c3p0.max_statement">${hibernate.c3p0.max_statement}<
/prop>
<prop key="c3p0.testConnectionOnCheckout">true</prop>
<prop key="user">${datasource.username}<
/prop>
<prop key="password">${datasource.password}</prop>

<
/props>
</property>
<
/bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="lobHandler"><ref local="oracleLobHandle"
/></property>
<property name="mappingResources">

<list>

<value>com
/cs2c/oa2/pojo/Testfile.hbm.xml</value>
<
/list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}<
/prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.dialect">${hibernate.dialect}<
/prop>
<prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}<
/prop>
</props>
<
/property>

</bean>



<bean id="test" class="com.cs2c.oa2.test.TestHibernate">
<property name="sessionFactory">
<ref bean="sessionFactory"
/>
</property>
<
/bean>

</beans>

 

在配置的时候 cp30的属性的名字必须是 ''driverClass' , 'jdbcUrl', 'user' , 'password' 而 <property name="properties">的名字也必须是“properties”,另外cp30配置文件的属性名也都是诸如:"c3p0.minPoolSize"、"hc3p0.maxPoolSize"等等,
java开源,希望大家也多多open,呵呵。我使用的是 spring1.2.6+hibernate3.1,ok,我觉得这些配置是基础知识,希望大家在今后一定要多看文档,在spring文档中有对dbcp如何配置,好了,不多说了。

自动重连的设置。

Configuring Recovery From Database Outages
c3p0 DataSources are designed (and configured by default) to recover from temporary database outages, such as those which occur during a database restart or brief loss of network connectivity. You can affect how c3p0 handles errors in acquiring Connections via the following configurable properties:

acquireRetryAttempts
acquireRetryDelay
breakAfterAcquireFailure

When a c3p0 DataSource attempts and fails to acquire a Connection, it will retry up to acquireRetryAttempts times, with a delay of acquireRetryDelay between each attempt. If all attempts fail, any clients waiting for Connections from the DataSource will see an Exception, indicating that a Connection could not be acquired. Note that clients do not see any Exception until a full round of attempts fail, which may be some time after the initial Connection attempt. If acquireRetryAttempts is set to 0, c3p0 will attempt to acquire new Connections indefinitely, and calls to getConnection() may block indefinitely waiting for a successful acquisition.

Once a full round of acquisition attempts fails, there are two possible policies. By default, the c3p0 DataSource will remain active, and will try again to acquire Connections in response to future requests for Connections. If you set breakAfterAcquireFailure to true, the DataSource will consider itself broken after a failed round of Connection attempts, and future client requests will fail immediately.

Note that if a database restart occurs, a pool may contain previously acquired but now stale Connections. By default, these stale Connections will only be detected and purged lazily, when an application attempts to use them, and sees an Exception. If you wish to avoid application Exceptions, you must adopt a connection testing strategy that is likely to detect stale Connections prior to their delivery to clients. (See "Configuring Connection Testing".) Even with active Connection testing (testConnectionsOnCheckout set to true, or testConnectionsOnCheckin and a short idleConnectionTestPeriod), your application may see occasional Exceptions on database restart, for example if the restart occurs after a Connection to the database has already been checked out.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值