人气旺 发表于 2014-9-28 09:25:15

Tomcat中配置JNDI数据源

在工作中用的都是企业web服务器,如weblogic,配置JNDI数据源都很方便,在家里闲来没事就想使用tomcat配置JNDI,便于搭建系统使用,发现tomcat6.x以上的版本都没有界面操作的。    记得以前在Tomcat5.x 的版本上配置过JNDI,还是界面可视化,操作起来比较方便,现tomcat6.x以上版本只能通过配置文件配置JNDI。

下面笔记一下,方便以后查看。

在Tomcat6.x 以上版本配置JNDI
第一步:把对应数据库jdbc的驱动jar包放入tomcat的lib下;
                我这里使用的是oracle 10g版本数据库,使用ojdbc14.jar,需要注意不同版本的Oracle对应的jdbc驱动jar包不一样,需使用匹配的jar文件。

第二步:
把 tomcat 下 conf文件夹 下 server.xml中加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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/oracle"
       global="jdbc/oracle"
       auth="Container"
       type="javax.sql.DataSource"
       driverClassName="oracle.jdbc.driver.OracleDriver"
       url="jdbc:oracle:thin:@192.168.1.105:1521:orcl"
       username="scott"         
       password="qweqwe123"
       maxActive="20"
       maxIdle="2"
       maxWait="5000"
      />
      
</GlobalNamingResources>





第二步:
把 tomcat 下 conf文件夹 下 context.xml中加入:

1
2
3
4
5
6
7
8
9
<Context>
<ResourceLink
   name="jdbc/oracle"
      type="javax.sql.DataSource"
      global="jdbc/oracle"
    />
   
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>





第四步:
在spring的配置文件中注入数据源bean:

1
2
3
4
5
6
7
<!-- 使用JNDI配置数据源 -->
<bean id="dataSource"   
         class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
   <value>java:comp/env/jdbc/oracle</value>
</property>
</bean>





以上配置即可完成tomcat6.x下配置JNDI数据源

页: [1]
查看完整版本: Tomcat中配置JNDI数据源