熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

spring中連接池的配置

2022-06-13   來源: Java開源技術 

  在默認通過myeclipse生成的配置裡spring使用的是apache的dbcp連接池

  <bean id=dataSource

  class=monsdbcpBasicDataSource>

  <property name=driverClassName

  value=commysqljdbcDriver>

  </property>

  <property name=url

  value=jdbc:mysql://localhost:/mysql>

  </property>

  <property name=username value=root></property>

  <property name=password value=root></property>

  </bean>

  如果改為CP則為

  <bean id=propertyConfigurer

  class=orgspringframewonfigPropertyPlaceholderConfigurer>

  <property name=location>

  <value>classpath:jdbcproperties</value>

  </property>

  </bean>

  <bean id=dataSource class=commchangevcpComboPooledDataSource>

  <property name=driverClass value=commysqljdbcDriver />

  <property name=jdbcUrl value=${jdbcurl} />

  <property name=user value=${jdbcusername} />

  <property name=password value=${jdbcpassword} />

  <property name=autoCommitOnClose value=true/>

  <property name=checkoutTimeout value=${cpoolcheckoutTimeout}/>

  <property name=initialPoolSize value=${cpoolminPoolSize}/>

  <property name=minPoolSize value=${cpoolminPoolSize}/>

  <property name=maxPoolSize value=${cpoolmaxPoolSize}/>

  <property name=maxIdleTime value=${cpoolmaxIdleTime}/>

  <property name=acquireIncrement value=${cpoolacquireIncrement}/>

  <property name=maxIdleTimeExcessConnections value=${cpoolmaxIdleTimeExcessConnections}/>

  </bean>

  jdbcproperties

  # Database URL

  jdbcurl=jdbc:mysql:///db

  # Database login information

  jdbcusername=root

  jdbcpassword=

  # Time to wait for an open connection before timing out

  # (in milliseconds)

  cpoolcheckoutTimeout=

  # Connection pool size

  cpoolminPoolSize=

  cpoolmaxPoolSize=

  # How long to keep unused connections around(in seconds)

  # Note: MySQL times out idle connections after hours( seconds)

  # so ensure this value is below MySQL idle timeout

  cpoolmaxIdleTime=

  # How long to hang on to excess unused connections after traffic spike

  # (in seconds)

  cpoolmaxIdleTimeExcessConnections=

  # Acquiring new connections is slow so eagerly retrieve extra connections

  # when current pool size is reached

  cpoolacquireIncrement=

  或者將上面的部分寫成一個

  <bean id=cpDataSource class=commchangevcpComboPooledDataSource destroymethod=close>

  <property name=driverClass>

  <value>${jdbcdriverClassName}</value>

  </property>

  <property name=jdbcUrl>

  <value>${jdbcurl}</value>

  </property>

  <property name=user>

  <value>${jdbcusername}</value>

  </property>

  <property name=password>

  <value>${jdbcpassword}</value>

  </property>

  <property name=initialPoolSize><value></value></property>

  <property name=minPoolSize><value></value></property>

  <property name=maxPoolSize><value></value></property>

  <property name=acquireIncrement><value></value></property>

  <property name=maxIdleTime><value></value></property>

  <property name=maxStatements><value></value></property>

  </bean>

  如果使用的是受管理的JEE服務器則在spring中配置為JNDI連接

  <bean id=dataSource class=orgspringframeworkjndiJndiObjectFactoryBean>

  <property name=jndiName>

  <value>java:comp/env/jndi/xxx</value>

  </property>

  </bean>

  如果在應用裡有獨自使用hibernate則在spring中配置hibernate連接池使用CP如下

  <bean   id=DataSource

  class=monsdbcpBasicDataSource>

  <property   name=driverClassName>

  <value>oraclejdbcdriverOracleDriver</value>

  </property>

  <property   name=url>

  <value>jdbc:oracle:thin:@::dbsvr</value>

  </property>

  <property   name=username>

  <value>hl</value>

  </property>

  <property   name=password>

  <value>hldw</value>

  </property>

  </bean>

  <bean   id=SessionFactory

  class=orgspringframeworkormhibernateLocalSessionFactoryBean>

  <property   name=dataSource>

  <ref   bean=DataSource   />

  </property>

  <property   name=hibernateProperties>

  <props>

  <prop   key=hibernatedialect>orghibernatedialectOracleDialect</prop>

  <prop   key=hibernateshow_sql>true</prop>

  <!   CP連接池配置   >

  <prop   key=nnectionprovider_class>nnectionCPConnectionProvider</prop>

  <prop   key=hibernatecpmax_size></prop>

  <prop   key=hibernatecpmin_size></prop>

  <prop   key=hibernatecptimeout></prop>

  <prop   key=hibernatecpmax_statements></prop>

  <prop   key=hibernatecpidle_test_period></prop>

  <prop   key=hibernatecpacquire_increment></prop>

  <prop   key=nnectionprofile>hl</prop>

  </props>

  </property>

  <property   name=mappingResources>

  <list>

  <value>com/hl/DBLogic/POJO/PermUserAccounthbmxml</value>

  </list>

  </property>

  </bean>

  spring中配置獨立使用hibernate時使用jndi的配置

  hibernatedialect = netsfhibernatedialectMySQLDialect

  nnectiondatasource=java:comp/env/jdbc/SAMPLEDB

  hibernateshow_sql=true

  如果是使用不受管理的Servlet容器如Tomcat也可以使用jndi的方式配置需要在tomcat中配置數據源在serverxml中增改大致如下

  <Resource name=jdbc/testDb auth=Container

  type=javaxsqlDataSource/>

  <ResourceParams name=jdbc/testDB>\\數據源的名稱

  <parameter><name>username</name><value>root</value></parameter>數據庫的名稱

  <parameter><name>password</name><value>password</value></parameter>數據庫密碼

  <parameter><name>driverClassName</name>

  <value>orggjtmmmysqlDriver</value></parameter>\\要加載的驅動

  <parameter><name>url</name>

  <value>jdbc:mysql:///rk?</value></parameter>\\要連接的URL

  </ResourceParams>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28471.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.