一,添加数据库驱动包加入classpath。
这里我用到了oracle和mysql。
所以由两个jar包:ojdbc14.jar和mysql-connector-java-5.1.13-bin.jar。
(有的也说需要添加commons-dbcp-1.4.jar,commons-pool-1.5.4.jar和commons-collections.jar,
我做了测试,不用的。
网上查了下,DBCP使用Jakarta-Commons Database Connection Pool,它依赖以下三个包:
Jakarta-Commons DBCP,Jakarta-Commons Collections,Jakarta-Commons Pool。
在Tomcat的安装目录提供了一个集成的jar包 $CATALINA_HOME/lib/tomcat-dbcp.jar。
所以有这个tomcat-dbcp.jar就可以了。
)
二,修改Tomcat_Home/conf/server.xml,在GlobalNamingResources中加入:
<Resource name="jdbc/cdbank" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@10.55.15.66:1521:cdbank" username="ccdb" password="ccdb" maxActive="10" maxIdle="5" maxWait="-1"/> <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/mydb" username="root" password="root" maxActive="10" maxIdle="5" maxWait="-1"/>
属性说明:
name:数据源的名称。
auth:指定管理Resource的Manager,由两个可选值:Container和Application。
Container表示由容器来创建和管理Resource。
Application表示由WEB应用来创建和管理Resource。
如果在web application deployment descriptor中使用<resource-ref>,这个属性是必需的。
如果使用<resource-env-ref>,这个属性是可选的。
type:指定Resource所属的java类名:javax.sql.DataSource。
maxActive: 指定数据库连接池中处于活动状态的数据库连接最大数目,0表示不受限制
maxIdle: 指定数据库连接池中处于空闲状态的数据库连接的最大数目,0表示不受限制
maxWait: 指定数据库连接池中的数据库连接处于空闲状态的最长时间(单位为毫秒),超过这一事件,
将会抛出异常。-1表示可以无限期等待。
username: 连接数据库的用户名
password: 连接数据库的密码
driverClassName: 指定连接数据库的JDBC驱动程序。
(oracle驱动:oracle.jdbc.OracleDriver
mysql驱动: com.mysql.jdbc.Driver)
url:连接数据库的URL
完整server.xml如下:
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="8006" shutdown="SHUTDOWN"> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users--> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <Resource name="jdbc/cdbank" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@10.55.15.66:1521:cdbank" username="ccdb" password="ccdb" maxActive="10" maxIdle="5" maxWait="-1"/> <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/mydb" username="root" password="root" maxActive="10" maxIdle="5" maxWait="-1"/> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8453" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8089" protocol="AJP/1.3" redirectPort="8453" /> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- The request dumper valve dumps useful debugging information about the request and response data received and sent by Tomcat. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.RequestDumperValve"/> --> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> <!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> --> </Host> </Engine> </Service> </Server>
三,再在context.xml中加入引用:
<ResourceLink name="jdbc/cdbank" global="jdbc/cdbank" type="javax.sql.DataSource"/> <ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource"/>
在这里要说明一下,我用的项目发布方式是一个xml文件发布一个工程。
所以我把ResourceLink写在context.xml中。
其实还可以配置到项目xml文件的context标签里。
如果你用的是server.xml方式发布的话,还可以在server.xml里面的host下的context标签里
配置。例如:
<Host appBase="webapps" name="localhost">
<Context docBase="D:\ChinaDevelopmentBankJBPM\workSpace\dataSourceApp\WebContent"
path="/dataSourceApp" reloadable="true" debug="0" crossContext="true">
<Resource name="jdbc/mysql" type="javax.sql.DataSource" password="123456" maxIdle="2"
maxWait="5000" username="root" maxActive="4" />
</Context>
</Host>
总之都是要配置到context标签里,并且能被Tomcat读取到就可以。至于那种方式更方便
那就要根据自己的选择了,实质其实都一样,即context.xml文件对应<Context>标签里的内容。
只要里面能被Tomcat读取到就可以。
完整context.xml如下:
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!-- The contents of this file will be loaded for each web application --> <Context> <ResourceLink name="jdbc/cdbank" global="jdbc/cdbank" type="javax.sql.DataSource"/> <ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource"/> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> </Context>
四,在web.xml中加入<resource-ref>。
<resource-ref> <description>tomcat datasource test,one oracle datasource</description> <res-ref-name>jdbc/cdbank</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <resource-ref> <description>tomcat datasource test,one mysql datasource</description> <res-ref-name>jdbc/mydb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
关于这一步,我做过测试,其实是不用的!
写上也不会报错。为什么会有这一步,大概是网上都是这么说的吧。
或者还有更深的意义。反正我这里做的实际测试结果就是没必要。
写上也不会起什么作用,也不报错。
我的完整web.xml如下;
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- tomcat数据源start --> <!-- <resource-ref> <description>tomcat datasource test,one oracle datasource</description> <res-ref-name>jdbc/cdbank</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <resource-ref> <description>tomcat datasource test,one mysql datasource</description> <res-ref-name>jdbc/mydb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> --> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class> org.test.TestServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> <!-- tomcat数据源 end --> </web-app>
#############################################################
写一个servlet来做测试TestServlet:
package org.test; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import org.test.util.DataSourceUtil; public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println(TestServlet.class.getName() + " is inited"); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet"); doPost(req, resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost"); // 编码设置 resp.setContentType("text/html"); resp.setHeader("Cache-Control", "no-cache"); resp.setCharacterEncoding("UTF-8"); oneEditCase(req,resp);//业务开始 resp.getWriter().println("success!"); } public void oneEditCase(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("oneEditCase start"); //oracle数据库对象 Connection conn=null; PreparedStatement pstmt=null; ResultSet rs =null; //mysql数据库对象 Connection conn_mysql=null; PreparedStatement pstmt_mysql=null; ResultSet rs_mysql =null; try { // conn = DataSourceUtil.getJNDIOracleDataSourceConnection(); conn = DataSourceUtil.getDataSourceOracleCdbankConnection(); conn_mysql=DataSourceUtil.getDataSourceMysqlMydbConnection(); conn.setAutoCommit(false);// 更改事务的提交方式 conn_mysql.setAutoCommit(false); String sql_select2 = " select t.dep_name,t.company from LSY_DEPARTMENT t where t.dep_id=‘11‘ "; pstmt = conn.prepareStatement(sql_select2);//查询 rs = pstmt.executeQuery(); while(rs.next()){ System.out.println("查到数据。。。"); String dep_name=rs.getString("dep_name"); String company=rs.getString("company"); System.out.println(dep_name+"--"+company); company+="haha"; String sql_update2 = " update LSY_DEPARTMENT a set a.company=‘"+company+"‘ where a.dep_id=‘11‘ "; pstmt = conn.prepareStatement(sql_update2);//更新oracle数据库字段 pstmt.execute(); //一下开始对另外的mysql数据库进行同步更新 String sql_mysql_select="select DEP_ID from lsy_department where dep_id=‘11‘ "; pstmt_mysql = conn_mysql.prepareStatement(sql_mysql_select);//查询是否存在该数据 rs_mysql = pstmt_mysql.executeQuery(); if(rs_mysql.next()){ System.out.println("存在相同的数据,就更新相关字段"); String sql_mysql_update="update mydb.lsy_department set " + " DEP_NAME = ‘"+dep_name+"‘ , " + " COMPANY = ‘"+company+"‘ " + " where DEP_ID = ‘11‘ "; pstmt_mysql = conn_mysql.prepareStatement(sql_mysql_update); pstmt_mysql.execute(); }else{ System.out.println("不存在相同的数据,就作为新数据插入"); String sql_mysql_insert="insert into lsy_department (DEP_ID, DEP_NAME, COMPANY) values(‘11‘, ‘"+dep_name+"‘, ‘"+company+"‘) "; pstmt_mysql = conn_mysql.prepareStatement(sql_mysql_insert); pstmt_mysql.execute(); } conn_mysql.commit(); conn_mysql.setAutoCommit(true); } conn.commit();//提交事务 conn.setAutoCommit(true);// 恢复事务的默认提交方式 }catch (SQLException e) { try { System.out.println("提交发生错误,开始回滚事务..."); conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); }finally{ try { if(rs != null){ rs.close(); } if(pstmt != null){ pstmt.close(); } if(conn != null){ conn.close(); } if(rs_mysql != null){ rs_mysql.close(); } if(pstmt_mysql != null){ pstmt_mysql.close(); } if(conn_mysql != null){ conn_mysql.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
里面需要一个工具类来管理连接DataSourceUtil:
package org.test.util; import java.sql.Connection; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class DataSourceUtil { private static DataSource dataSource_oracle_cdbank=null;//一个oracle数据源 private static DataSource dataSource_mysql_mydb=null;//一个mysql数据源 //若果只需要从一个数据源获取链接就可以向下面这样写。否则,把DataSource单独提出。 // public static Connection getJNDIOracleDataSourceConnection(){ // Connection conn=null; // InitialContext ctx=null; // try { // ctx = new InitialContext(); // if(dataSource_oracle_cdbank == null){ // System.out.println("dataSource_oracle_cdbank start connnectiong..."); // // //获取数据源,其中java:comp/env是Tomcat规定的,Tomcat提供的JNDI绑定都必须加该前缀。后面就是Resource的name属性 // dataSource_oracle_cdbank = (DataSource)ctx.lookup("java:comp/env/jdbc/cdbank");//context.xml 文件里 Resource 项下 name 的值 "jdbc/qwe" // // if(dataSource_oracle_cdbank != null){ // //获取链接(根据需要可以获取多个) // conn= dataSource_oracle_cdbank.getConnection(); // }else{ // System.out.println("连接发生异常..."); // } // } // }catch(NamingException e) { // e.printStackTrace(); // }catch (SQLException e) { // e.printStackTrace(); // } // // return conn; // } //获取oracle的DataSource public static DataSource getDataSourceOracleCdbank(){ InitialContext ctx=null; try { ctx = new InitialContext(); if(dataSource_oracle_cdbank == null){ System.out.println("dataSource_oracle_cdbank start connnectiong..."); //获取数据源,其中java:comp/env是Tomcat规定的,Tomcat提供的JNDI绑定都必须加该前缀。后面就是Resource的name属性 dataSource_oracle_cdbank = (DataSource)ctx.lookup("java:comp/env/jdbc/cdbank");//context.xml 文件里 Resource 项下 name 的值 "jdbc/qwe" } }catch(NamingException e) { e.printStackTrace(); } return dataSource_oracle_cdbank; } //获取DataSource对象 public static DataSource getDataSourceOracleCdbankInstance(){ if(dataSource_oracle_cdbank==null){ dataSource_oracle_cdbank=getDataSourceOracleCdbank(); } return dataSource_oracle_cdbank; } //获取Connection public static Connection getDataSourceOracleCdbankConnection(){ Connection conn=null; try { if(getDataSourceOracleCdbankInstance() != null){ //获取链接(根据需要可以获取多个) conn= getDataSourceOracleCdbankInstance().getConnection(); }else{ System.out.println("连接发生异常..."); } }catch (SQLException e) { e.printStackTrace(); } return conn; } /////////////////////////////////////////////////////////////////////////////////////////////// //获取mysql的DataSource public static DataSource getDataSourceMysqlMydb(){ InitialContext ctx=null; try { ctx = new InitialContext(); if(dataSource_mysql_mydb == null){ System.out.println("dataSource_mysql_mydb start connnectiong..."); //获取数据源,其中java:comp/env是Tomcat规定的,Tomcat提供的JNDI绑定都必须加该前缀。后面就是Resource的name属性 dataSource_mysql_mydb = (DataSource)ctx.lookup("java:comp/env/jdbc/mydb"); } }catch(NamingException e) { e.printStackTrace(); } return dataSource_mysql_mydb; } //获取DataSource对象 public static DataSource getDataSourceMysqlMydbInstance(){ if(dataSource_mysql_mydb==null){ dataSource_mysql_mydb=getDataSourceMysqlMydb(); } return dataSource_mysql_mydb; } //获取Connection public static Connection getDataSourceMysqlMydbConnection(){ Connection conn=null; try { if(getDataSourceMysqlMydbInstance() != null){ //获取链接(根据需要可以获取多个) conn= getDataSourceMysqlMydbInstance().getConnection(); }else{ System.out.println("连接发生异常..."); } }catch (SQLException e) { e.printStackTrace(); } return conn; } }
第一次访问http://localhost:8080/dataSourceApp/TestServlet
打印:
org.test.TestServlet is inited
doGet
doPost
oneEditCase start
dataSource_oracle_cdbank start connnectiong...
dataSource_mysql_mydb start connnectiong...
查到数据。。。
研发部--百度
不存在相同的数据,就作为新数据插入
第二次访问http://localhost:8080/dataSourceApp/TestServlet
打印:
doGet
doPost
oneEditCase start
查到数据。。。
研发部--百度haha
存在相同的数据,就更新相关字段
同时两个数据库的值都已经正确修改。
再说些废话,jndi的数据源对象也可以在spring注册。
大多数情况,应该不会用到。
为什么呢?如果你用到了spring,你完全可以把新的数据源配置到spring里,
当然不会是使用jndi了,完全可以使用c3p0,dbcp,或者jdbc。
同时迁移服务器的时候也好迁移。
如果已经使用了spring,再去配置JNDI数据源,感觉像杀鸡用牛刀。
JNDI的方式,我觉得大多数情况使用小型的应用,或者做数据处理,比如两个数据库
之间的数据传递(不是简简单单的倒库)。我的例子就是这种情况。
小型的应用,功能很少,这样配置数据源还是很方便的(jdbc也不错)
原文地址:https://www.cnblogs.com/zmwf/p/9108591.html