zj2092 发表于 2017-2-4 13:13:52

Tomcat下的war包数据源配置

  做了如下三种配置,都有效:
  方法一:将数据源声明在项目下的META-INF下的context.xml中(推荐)
,如下
  在项目中的META-INF下创建context.xml,添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/imei" reloadable="false" allowLinking="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
logAbandoned="true" maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/data1" password="password" removeAbandoned="true"
removeAbandonedTimeout="300" type="javax.sql.DataSource"
url="jdbc:oracle:thin:@192.168.0.X:1521:orcl1" username="username" />
<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
logAbandoned="true" maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/data2" password="password" removeAbandoned="true"
removeAbandonedTimeout="300" type="javax.sql.DataSource"
url="jdbc:oracle:thin:@192.168.0.X:1521:orcl2" username="username" />
</Context>

  方法二:将数据源声明在tomcat的server.xml中,然后在
项目下的
META-INF下的context.xml中进行关联
,如下:
  在tomcat的server.xml添加数据源,如下:

<!-- Global JNDI resources -->
<GlobalNamingResources>
<!-- Test entry for demonstration purposes -->
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<!-- 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 driverClassName="oracle.jdbc.driver.OracleDriver"
logAbandoned="true" maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/data" password="password" removeAbandoned="true"
removeAbandonedTimeout="300" type="javax.sql.DataSource"
url="jdbc:oracle:thin:@192.168.0.X:1521:orcl" username="username" />

</GlobalNamingResources>
  然后在项目中的META-INF下创建context.xml,添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/imei" reloadable="false" allowLinking="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<ResourceLink global="jdbc/data" name="jdbc/data1"
type="javax.sql.DataSource" />

</Context>

  
 
方法三:将数据源声明在tomcat的context.xml中,
如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context>

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
logAbandoned="true" maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/data" password="password" removeAbandoned="true"
removeAbandonedTimeout="300" type="javax.sql.DataSource"
url="jdbc:oracle:thin:@192.168.0.X:1521:orcl" username="username" />
</Context>
  
 
当然,此处使用类似方法二中的配置,将数据源声明在server.xml中,然后在context.xml中使用如下方式也可以:

<ResourceLink global="jdbc/data" name="jdbc/data1"
type="javax.sql.DataSource" />
  说明:
  1.方法一和方法二类似,都是到项目下的META-INF/context.xml中读取数据源,部署时会将该context.xml拷贝到tomcat下/conf/Catalina/localhost下,并将xml文件以项目名称命名;
  2.如果同时声明了方法一(或者方法二)和方法三,则优先查找方法三中的数据源,如果没有找到,再去查找方法一(或者方法二)中的数据源配置。
页: [1]
查看完整版本: Tomcat下的war包数据源配置