1.获取
可以在tomcatLibrary中找到,也可以用Maven。
<dependency>
<span style="white-space:pre"> </span><groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.29</version>
</dependency>
2.常用类
org.apache.tomcat.jdbc.pool.
DataSource
此类用于配置数据库连接的基本信息。
Connection org.apache.tomcat.jdbc.pool.DataSourceProxy.
getConnection()
此方法返回 java.sql.Connection。然后就可以正常使用啦。
3.配置
在spring中配置的例子:
<?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:p="http://www.springframework.org/schema/p"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:annotation-config />
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan base-package="com.likeyichu.qing" />
<bean id="jdbc"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="/WEB-INF/jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 默认值是true,当从连接池取连接时,验证这个连接是否有效-->
<property name="testOnBorrow" value="true" />
<!--一条sql语句,用来验证数据库连接是否正常。这条语句必须是一个查询模式,并至少返回一条数据。可以为任何可以验证数据库连接是否正常的sql-->
<property name="validationQuery" value="select 1"/>
<!-- 是否自动回收超时连接-->
<property name="removeAbandoned" value="true"/>
<!-- 空闲时测试连接,必须配置validationQuery才有效-->
<property name="testWhileIdle" value="true" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="8" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="10" />
<!-- #连接的超时时间,默认为半小时。 -->
<property name="minEvictableIdleTimeMillis" value="3600000" />
</bean>
</beans>
select 1 就会返回1,数据库特性,跟表无关。
此配置经过检测,用了10天还是能正常使用。