首先,Data source可以做为Resource(JDNI方式)可以在$TOMCAT_HOME/conf/context.xml或server.xml的GlobalNamingResources元素里配置成全局资源或$project.war/META-INF/conext.xml配置成应用级资源,然后在web application context配置该JNDI, 如(假设JNDI名字为/jdbc/my-ds):
(context.xml)
<Context>
<Resource name="jdbc/my-ds" auth="Container" type="javax.sql.DataSource"
maxActive="80" maxIdle="50" maxWait="10000"
username="aaaa" password="****" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/DBNAME" />
</Context>
<Resource name="jdbc/my-ds" auth="Container" type="javax.sql.DataSource"
maxActive="80" maxIdle="50" maxWait="10000"
username="aaaa" password="****" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/DBNAME" />
</Context>
(appcontext.xml)
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/my-ds"/>
</bean>
也可以集成如spring框架直接配成bean。因为之前jboss就用了spring, 所以还是用spring bean来配置一个Apache common DBCP实例。
(Mysql, oracle只需要改driver为oracle.jdbc.driver.OracleDriver, url为connection string(如jdbc:oracle:thin:@$TNSNAME 或者jdbc:oracle:thin:@$TNSSTRING, username, password)
<?xml version=”1.0” encoding=”utf-8” ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="my-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value=”com.mysql.jdbc.Driver” />
<property name="url" value=”jdbc:mysql://localhost:3306/DBNAME” />
<property name="username" value=”aaaa” />
<property name="password" value=”***” / >
<property name="maxActive" value=”80” />
<property name="maxIdle" value=”50” />
</bean>
</beans>
如此的配置在Jboss/Tomcat上都是可以成功的,不过据http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html 里说到,Common DBCP是单线程模式的,复杂的(60+ classes),缓慢的方式了,Apache基金组织已经改写了变成Tomcat JDBC Connection Pool组件。Tomcat JDBC CP的spring配置如下(需要tomcat-jdbc.jar复制到project.war/WEB-INF/lib里,当然如果配置成global resource则需要放在$TOMCAT_HOME/lib里,该文件在build tomcat时可以得到):
<?xml version=”1.0” encoding=”utf-8” ?>
<!-- !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" http://www.springframework.org/dtd/spring-beans.dtd
旧版本用法,不再支持-->
<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="my-ds" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/DBNAME"
p:username="aaaa"
p:password="****"
p:initialSize="10"
p:minIdle="10"
p:maxIdle="50"
p:maxActive="80"
p:maxWait="10000"
p:jmxEnabled="true"
/>
本文转自 dannyy1026 51CTO博客,原文链接:
http://blog.51cto.com/dannyyuan/756004