记录下数据连接池的简单配置
使用数据连接池即:比如使用mybatis给其传入的dataSource用池来给
相关配置:
c3p0
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="#{config.url}"></property>
<property name="driverClass" value="#{config.driverClassName}"></property>
<property name="user" value="#{config.username}"></property>
<property name="password" value="#{config.password}"></property>
<!-- 初始化获取3个连接池 -->
<property name="initialPoolSize" value="3"></property>
<!-- 最小连接池数 -->
<property name="minPoolSize" value="2"></property>
<!-- 最大连接池数 -->
<property name="maxPoolSize" value="15"></property>
<!-- 当连接池中连接耗尽时,c3p0一直同时获得的连接数 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</beans>
druid:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- druid 数据库连接池注册数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="#{config.url}"></property>
<property name="driverClassName" value="#{config.driverClassName}"></property>
<property name="username" value="#{config.username}"></property>
<property name="password" value="#{config.password}"></property>
<property name="initialSize" value="3"></property>
<property name="minIdle" value="2"></property>
<property name="maxActive" value="5"></property>
<property name="maxWait" value="10000"></property>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true"></property>
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20"></property>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,
执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle" value="true"></property>
<!-- 这里建议配置为TRUE,防止取到的连接不可用 ,申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 -->
<property name="testOnBorrow" value="true"></property>
<!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
<property name="testOnReturn" value="false"></property>
</bean>
直接加到数据库配置里 或 建新文件import下都可以
<!-- <import resource="c3p0.xml"/> -->
<import resource="druid.xml"/>
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> -->
<!-- <property name="driverClassName" value="#{config.driverClassName}" /> -->
<!-- <property name="url" value="#{config.url}" /> -->
<!-- <property name="username" value="#{config.username}" /> -->
<!-- <property name="password" value="#{config.password}" /> -->
<!-- </bean> -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<!-- mapper文件存储在
src/main/resources/mappers 文件夹 -->
<property name="mapperLocations"
value="classpath:mapper/*.xml"/>
</bean>
<!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> -->
<!-- <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> -->
<!-- </bean> -->
<bean id="mapperScanner"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory"
ref="sqlSessionFactory"/>
<property name="basePackage"
value="com.zzm.test.dao"/>
</bean>
3382

被折叠的 条评论
为什么被折叠?



