JNDI+Tomcat配置数据源的两种方式

非全局jndi配置步骤 :此种配置方式不需要在server.xml中配置数据源,而只在tomcat/conf/Catalina/localhost下的启动配置中配置即可。注意红色字体名称必须和相同。

0、需要在tomcat/common/lib下加入数据库连接的jar包

1、web.xml配置

?
<resource-ref>
      <description>my DB Connection</description>
      <res-ref-name>mydataSource </res-ref-name>  
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

2、applicationContext.xml下配置

?
<bean id= "dataSource"
     class = "org.springframework.jndi.JndiObjectFactoryBean" >
     <property name= "jndiName" value= "java:comp/env/mydataSource " />
</bean>

3、在tomcat的conf下的localhost下的配置如下

?
<?xml version= "1.0" encoding= "UTF-8" ?>
<Context docBase= "F:/workspace/cuapp/WebRoot" path= "cuapp" reloadable= "false" >
<Resource name= "mydataSource" auth= "Container" type= "javax.sql.DataSource"
                    url= "jdbc:oracle:thin:@192.168.2.104:1521:ora10g"
                 driverClassName= "oracle.jdbc.driver.OracleDriver"
                 password= "aa"
                 username= "aa"
                 initialSize= "2"
                 maxActive= "3"
                 maxIdle= "1"
                 minIdle= "1"
                 maxWait= "10000"
                 removeAbandoned= "true"
                 logAbandoned= "true"
                 removeAbandonedTimeout= "60"
                 timeBetweenEvictionRunsMillis= "900000"
                 minEvictableIdleTimeMillis= "1800000"
                 numTestsPerEvictionRun= "100"
                 validationQuery= "select count(0) from dual"
                 poolPreparedStatements= "true"
                 maxOpenPreparedStatements= "100" />
</Context>


全局jndi配置 :此种配置需要在server.xml中配置数据源。 

0、需要在tomcat下加入数据库连接的jar包

1、web.xml配置

?
<resource-ref>
     <description>my DB Connection</description>
     <res-ref-name>mydataSource </res-ref-name>   must be same as server.xml
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
</resource-ref>

2、applicationContext.xml下配置

?
<bean id= "dataSource"
     class = "org.springframework.jndi.JndiObjectFactoryBean" >
     <property name= "jndiName" value= "java:comp/env/mydataSource " />
</bean>

3、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 name= "mydataSource" auth= "Container" type= "javax.sql.DataSource"
                                url= "jdbc:oracle:thin:@192.168.2.104:1521:ora10g"
                            driverClassName= "oracle.jdbc.driver.OracleDriver"
                            password= "aa"
                            username= "aa"
                            initialSize= "5"
                            maxActive= "10"
                            maxIdle= "5"
                            minIdle= "2"
                            maxWait= "10000"
                            removeAbandoned= "true"
                            logAbandoned= "true"
                            removeAbandonedTimeout= "60"
                            timeBetweenEvictionRunsMillis= "900000"
                            minEvictableIdleTimeMillis= "1800000"
                            numTestsPerEvictionRun= "100"
                            validationQuery= "select count(0) from dual"
                            poolPreparedStatements= "true"
                            maxOpenPreparedStatements= "100" />
  </GlobalNamingResources>

4、tomcat/conf下localhost下的配置如下

?
<?xml version= "1.0" encoding= "UTF-8" ?>
<Context docBase= "F:/workspace/cuapp/WebRoot" path= "xj-adminportal" reloadable= "false" >
     <ResourceLink name= "mydataSource " global= "mydataSource " type= "javax.sql.DataSource" />
</Context>


Cannot create JDBC driver of class '' for connect URL 'null'问题解决方法


修改\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如test.xml)打开添加内容如下:
<?xml version="1.0" encoding="UTF-8"?>
< Context>
  <Resource
          name="jdbc/mysql"
          type="javax.sql.DataSource"
          password="123456"
          driverClassName="org.gjt.mm.mysql.Driver"
          maxIdle="2"
          maxWait="50"
          username="root"
          url="jdbc:mysql://localhost:3306/test"
          maxActive="4"/>

</Context>
内容同conf/server.xml中<GlobalNamingResources>
 <Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="123456"
      driverClassName="org.gjt.mm.mysql.Driver"
      maxIdle="2"
      maxWait="50"
      username="root"
      url="jdbc:mysql://localhost:3306/test"
      maxActive="4"/>
 < /GlobalNamingResources>

少了这一步会报错:Cannot create JDBC driver of class '' for connect URL 'null'

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值