java

位置:IT落伍者 >> java >> 浏览文章

spring中连接池的配置


发布日期:2021年09月20日
 
spring中连接池的配置

在默认通过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>

上一篇:使用 Eclipse V3.3 保持代码干净

下一篇:Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序