首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

配备Tomcat全局数据源

2012-08-29 
配置Tomcat全局数据源?1.修改tomcat_home/conf/server.xml文件信息??例如:??Resource namelinyDataSour

配置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下面

?

热点排行