熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Tomcat配置多數據源

2022-06-13   來源: Java開源技術 

  測試的tomcat為apachetomcat 數據庫為mysql和oracle

  配置步驟如下

  把數據庫的JDBC驅動放入D:\apachetomcat\lib目錄下

  在D:\apachetomcat\conf\webxml文件中將下面代碼加入到webxml中

  <resourceref>

  <description>DB Connection</description>

  <resrefname>jdbc/mysql</resrefname>

  <restype>javaxsqlDataSource</restype>

  <resauth>Container</resauth>

  </resourceref>

  <resourceref>

  <description>DB Connection</description>

  <resrefname>jdbc/oracle</resrefname>

  <restype>javaxsqlDataSource</restype>

  <resauth>Container</resauth>

  </resourceref>

  在D:\apachetomcat\conf\serverxml文件中在Host節點下添加Context子節點配置如下

  <Context path=/ljqtest docBase=ljqtest debug= reloadable=true crossContext=true>

  <Resource name=jdbc/mysql

  type=javaxsqlDataSource

  username=root

  password=mysql

  driverClassName=orggjtmmmysqlDriver

  url=jdbc:mysql://localhost:/shop

  maxIdle=

  maxWait=

  maxActive=>

  <parameter>

  <name>removeAbandoned</name>

  <value>true</value>

  </parameter>

  </Resource>

  <Resource name=jdbc/oracle

  type=javaxsqlDataSource

  username=test

  password=test

  driverClassName=oraclejdbcdriverOracleDriver

  url=jdbc:oracle:thin:@localhost::ORCL

  maxIdle=

  maxWait=

  maxActive=>

  <parameter>

  <name>removeAbandoned</name>

  <value>true</value>

  </parameter>

  </Resource>

  </Context>

  </Host>

  或者

  <Context path=/uimcardprj docBase=uimcardprj debug= reloadable=true crossContext=true>

  <Resource name=jdbc/ycxkDB

  type=javaxsqlDataSource

  username=ycxk

  password=xmzh

  driverClassName=oraclejdbcdriverOracleDriver

  url=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)))

  maxIdle=

  maxWait=

  maxActive=>

  </Resource>

  </Context>

  </Host>

  注意path為D:\apachetomcat\webapps目錄下的工程名稱

  把web工程項目部署在D:\apachetomcat\webapps目錄下

  MysqlConn類獲取Mysql數據源

  package comljqtest;

  import javasqlConnection;

  import javasqlSQLException;

  import javaxnamingContext;

  import javaxnamingInitialContext;

  import javaxsqlDataSource;

  public final class MysqlConn {

  // 懶漢式單例(使用時才new)

  private static MysqlConn instance = null;

  MysqlConn() {

  }

  // 延遲初始化(用到的時候才加載)(推薦)

  // public static synchronized JdbcConn

  // getInstance(){}>這樣不好因為每調用一次就同步效率非常低

  public static MysqlConn getInstance() {

  if (instance == null) {

  synchronized (MysqlConnclass) {// 可能會產生並發的問題我們對他進行同步

  if (instance == null) {

  instance = new MysqlConn();

  }

  }

  }

  return instance;

  }

  private DataSource getDataSource() {

  DataSource ds = null;

  try {

  Context ctx = new InitialContext();

  ds = (DataSource) ctxlookup(java:comp/env/jdbc/mysql);

  } catch (Exception e) {

  Systemoutprintln(數據源獲取失敗);

  eprintStackTrace();

  }

  return ds;

  }

  public Connection getConn() {

  Connection conn = null;

  try {

  conn = getDataSource()getConnection();

  } catch (SQLException e) {

  Systemoutprintln(數據庫連接失敗);

  eprintStackTrace();

  }

  return conn;

  }

  }

  OraclelConn類獲取Oracle數據源

  package comljqtest;

  import javasqlConnection;

  import javasqlSQLException;

  import javaxnamingContext;

  import javaxnamingInitialContext;

  import javaxsqlDataSource;

  public final class OracleConn {

  // 懶漢式單例(使用時才new)

  private static OracleConn instance = null;

  OracleConn() {

  }

  // 延遲初始化(用到的時候才加載)(推薦)

  // public static synchronized JdbcConn

  // getInstance(){}>這樣不好因為每調用一次就同步效率非常低

  public static OracleConn getInstance() {

  if (instance == null) {

  synchronized (OracleConnclass) {// 可能會產生並發的問題我們對他進行同步

  if (instance == null) {

  instance = new OracleConn();

  }

  }

  }

  return instance;

  }

  private DataSource getDataSource() {

  DataSource ds = null;

  try {

  Context ctx = new InitialContext();

  ds = (DataSource) ctxlookup(java:comp/env/jdbc/mysql);

  } catch (Exception e) {

  Systemoutprintln(數據源獲取失敗);

  eprintStackTrace();

  }

  return ds;

  }

  public Connection getConn() {

  Connection conn = null;

  try {

  conn = getDataSource()getConnection();

  } catch (SQLException e) {

  Systemoutprintln(數據庫連接失敗);

  eprintStackTrace();

  }

  return conn;

  }

  }

  頁面indexjsp:打印數據庫連接對象

  <body>

  mysql連接對象為<%Connection conn=MysqlConngetInstance()getConn();%><%=conn %><%connclose();%><br/>

  oracle連接對象為<%Connection conn=MysqlConngetInstance()getConn();%><%=conn %><%connclose();%><br/>

  </body>

  啟動tomcat在浏覽器中輸入//localhost:/ljqtest/輸出如下

   


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28581.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.