Jboss迁移到Tomcat之手记(三)-JDBC

  1. 云栖社区>
  2. 博客>
  3. 正文

Jboss迁移到Tomcat之手记(三)-JDBC

科技小能手 2017-11-23 01:03:00 浏览795
展开阅读全文

首先,Data source可以做为Resource(JDNI方式)可以在$TOMCAT_HOME/conf/context.xmlserver.xmlGlobalNamingResources元素里配置成全局资源或$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>
(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只需要改driveroracle.jdbc.driver.OracleDriver, urlconnection 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 CPspring配置如下(需要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


网友评论

登录后评论
0/500
评论
科技小能手
+ 关注