梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
hibernate开发组推荐c3p0,spring开发组推荐dbcp,但是dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法 reconnect ,告诉连接被重置,这个设置可以解决。hibernate in action推荐c3p0和proxool。

我推荐proxool,因为他不但可以监控后台。还可以有效的释放连接。在connection close时,也就是归还connection,
关闭所有的statement,并且判断是否autocommit,如果不行,就rollback,并且设置true,
可以参考proxool的org.logicalcobwebs.proxool.ConnectionResetter类
connection pool把connection reset置回initial state。
dbcp的配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>

 <property name="validationQuery">
<value>SELECT 1</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
</bean>

c3p0的配置, 注意是''driverClass' , 'jdbcUrl', 'user' , 'password'

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass">
        <value>net.sourceforge.jtds.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">

<value>jdbc:jtds:sqlserver://localhost:1433/hua</value>
        </property>
        <property name="user">
        <value>sa</value>
        </property>
        <property name="password">
        <value>hua</value>
        </property>
        <property name="minPoolSize">
        <value>15</value>
        </property>
        <property name="acquireIncrement">
        <value>5</value>
        </property>
        <property name="maxPoolSize">
        <value>25</value>
        </property>
        </bean>

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${db.driverClass}</value>
        </property>
        <property name="jdbcUrl">
            <value>${db.url}</value>
        </property>
         <!--
        <property name="user">
            <value>${db.user}</value>
        </property>
        <property name="password">
            <value>${db.pass}</value>
        </property>
        -->
        <property name="properties">
            <props>
                <prop key="c3p0.acquire_increment">5</prop>
                <prop key="c3p0.idle_test_period">100</prop>
                <prop key="c3p0.max_size">100</prop>
                <prop key="c3p0.max_statements">0</prop>
                <prop key="c3p0.min_size">10</prop>
                <prop key="user">${db.user}</prop>
                <prop key="password">${db.pass}</prop>
            </props>
        </property>
    </bean>
xapool的配置
 <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardPoolDataSource" destroy-method="stopPool">
    <constructor-arg index="0">
      <bean class="org.enhydra.jdbc.standard.StandardConnectionPoolDataSource">
        <property name="driverName"><value>com.mysql.jdbc.Driver</value></property>
        <property name="url"><value>jdbc:mysql://localhost/dbname</value></property>
      </bean>
    </constructor-arg>
    <property name="user"><value>root</value></property>
    <property name="password"><value>mypass</value></property>
    <property name="minSize"><value>1</value></property>
    <property name="maxSize"><value>5</value></property>
    <property name="jdbcTestStmt"><value>select 1</value></property>
  </bean>
c-jdbc的配置
<bean id="dataSource" class="org.objectweb.cjdbc.driver.DataSource">
    <property name="url"><value>jdbc:cjdbc://127.0.0.1:25322/vdb?user=vuser</value></property>
  </bean>

weblogic的连接池解决办法:Test Reserved Connections: 如果选择了这个选项,服务器会在把连接提供给客户端之前
对其进行测试。 Test Created Connections: 如果选择了这个选项,就会在创建一个JDBC
连接之后和在把它添加到JDBC连接池中的可用连接列表之前,对该JDBC连接进行测试。

tomcat的jndi关于dbcp的配置:

<parameter>
         <name>factory</name>
         <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
       </parameter>
       <parameter>
         <name>driverClassName</name>
         <value>com.sybase.jdbc2.jdbc.SybDriver</value>
       </parameter>
       <parameter>
         <name>url</name>
         <value>xyz</value>
       </parameter>
       <parameter>
         <name>username</name>
         <value>xyz</value>
       </parameter>
       <parameter>
         <name>password</name>
         <value>xyz</value>
       </parameter>
       <parameter>
         <name>maxActive</name>
         <value>5</value>
       </parameter>
       <parameter>
         <name>maxIdle</name>
         <value>5</value>
       </parameter>
       <parameter>
         <name>maxWait</name>
         <value>-1</value>
       </parameter>
       <parameter>
         <name>removeAbandoned</name>
         <value>true</value>
       </parameter>
       <parameter>
         <name>validationQuery</name>
         <value>select count(*) from sometable where 1 = 0</value>
       </parameter>
       <parameter>
         <name>testOnBorrow</name>
         <value>true</value>
       </parameter>

proxool
<bean id="proxooldataSource" class="*.proxool.ProxoolDataSource" destroy-method="close">
      <property name="alias"><value>newstest</value></property>
      <property name="driver"><value>net.sourceforge.jtds.jdbc.Driver</value></property>
      <property name="driverUrl"><value>jdbc:jtds:sqlserver://127.0.0.1:1433/news<value></property>
      <property name="user"><value>sa</value></property>
      <property name="password"><value>1</value></property>
      <property name="houseKeepingSleepTime"><value>90000</value></property>
      <property name="prototypeCount"><value>5</value></property>
      <property name="maximumConnectionCount"><value>100</value></property>
      <property name="minimumConnectionCount"><value>10</value></property>
      <property name="trace"><value>true</value></property>
      <property name="verbose"><value>true</value></property>
    </bean>

posted on 2008-04-16 10:30 HUIKK 阅读(1951) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: