如何在Spring中使用Tomcat提供的JNDI DataSource? [英] How to use JNDI DataSource provided by Tomcat in Spring?

查看:216
本文介绍了如何在Spring中使用Tomcat提供的JNDI DataSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring javadoc文章中有关 DriverManagerDataSource 类的说法,这个类很简单,建议

It is said in Spring javadoc article about DriverManagerDataSource class, that this class is very simple and that it is recommended


使用容器提供的JNDI DataSource。这样的数据源可以经由的JndiObjectFactoryBean 数据源豆code>

to use a JNDI DataSource provided by the container. Such a DataSource can be exposed as a DataSource bean in a Spring ApplicationContext via JndiObjectFactoryBean

问题是:如何做到这一点?

The question is: how to accomplish this?

例如,如果我希望DataSource bean访问我的custo mysql数据库,那么我需要什么呢?在上下文配置等中写什么?

For example if I wish to have DataSource bean to access my custo mysql database, what I require then? What to write in context configuration etc?

推荐答案

如果使用Spring的基于XML模式的配置,请在Spring上下文中设置如下: / p>

If using Spring's XML schema based configuration, setup in the Spring context like this:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
...
<jee:jndi-lookup id="dbDataSource"
   jndi-name="jdbc/DatabaseName"
   expected-type="javax.sql.DataSource" />

或者,使用这样的简单bean配置进行设置:

Alternatively, setup using simple bean configuration like this:

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

您可以使用以下内容在tomcat的server.xml中声明JNDI资源:

You can declare the JNDI resource in tomcat's server.xml using something like this:

<GlobalNamingResources>
    <Resource name="jdbc/DatabaseName"
              auth="Container"
              type="javax.sql.DataSource"
              username="dbUser"
              password="dbPassword"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="20"
              maxWaitMillis="15000"
              maxTotal="75"
              maxIdle="20"
              maxAge="7200000"
              testOnBorrow="true"
              validationQuery="select 1"
              />
</GlobalNamingResources>

并从Tomcat的web context.xml引用JNDI资源,如下所示:

And reference the JNDI resource from Tomcat's web context.xml like this:

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

参考文档:

  • Tomcat 8 JNDI Datasource HOW-TO
  • Tomcat 8 Context Resource Links Reference
  • Spring 4 JEE JNDI Lookup XML Schema Reference
  • Spring 4 JndiObjectFactoryBean Javadoc

编辑:这个答案已针对Tomcat 8和Spring 4进行了更新。对Tomcat的默认值进行了一些属性名称更改数据源资源池etup。

This answer has been updated for Tomcat 8 and Spring 4. There have been a few property name changes for Tomcat's default datasource resource pool setup.

这篇关于如何在Spring中使用Tomcat提供的JNDI DataSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆