sonyet 发表于 2017-1-22 07:24:33

配置Tomcat全局数据源

  1.修改tomcat_home/conf/server.xml文件信息
  例如:
  <Resource name="linyDataSource" 
  type="javax.sql.DataSource" 
  driverClassName="oracle.jdbc.driver.OracleDriver" 
  password="test1" 
  maxIdle="2" 
  maxWait="5000" 
  username="test1" 
  url="jdbc:oracle:thin:@localhost:1521:orcl" 
  maxActive="4"/>
  
  2.配置应用下的web.xml文件
  例如:
  <resource-ref>
  <res-ref-name>linyDataSource</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>
  3.新建myApp/META-INF/context.xml文件
  <Context path="/MyAppName" docBase="/MyAppName" debug="5"
  reloadable="true" crossContext="true">
  <ResourceLink name="linyDataSource" global="linyDataSource"
  type="javax.sql.DataSource" />
  </Context>
  注:第三步,可以换成
  在tomcat_home/conf/server.xml的<host>节点下添加如下节点:
  <Context
  docBase="/MyAppName"
  path="/MyAppName"
  reloadable="true"
  debug="5" 
  crossContext="true">
    <Resource name="linyDataSource" 
type="javax.sql.DataSource" 
driverClassName="oracle.jdbc.driver.OracleDriver" 
password="test1" 
maxIdle="2" 
maxWait="5000" 
username="test1" 
url="jdbc:oracle:thin:@localhost:1521:orcl" 
maxActive="4"/>
  </Context>
  使用以上方式要注意:如果MyAppName工程从tomcat中卸载掉了,这个配置节点一定要移除,否则就会引发异常:Tomcat_home/webapp/MyAppName directory not find
  4.一定要将驱动jar包加入到commn/lib下
  5.通过在web容器中,使用
  Context ctx = new InitialContext();
  DataSource ds =  (DataSource)ctx.lookup("java:/comp/env/linyDataSource");
  就能获取到数据源了
  
  注:第三步如果使用新建context.xml的方式,在实例化上下文中如果抛出Exception:Cannot create JDBC driver of class         '' for connect URL 'null'
  那么得手动将classes/META-INF/context.xml复制到myWebApp/META-INF下面
页: [1]
查看完整版本: 配置Tomcat全局数据源