簡體   English   中英

如何在Spring中使用Tomcat提供的JNDI DataSource?

[英]How to use JNDI DataSource provided by Tomcat in Spring?

據說在關於DriverManagerDataSource類的Spring javadoc文章中,這個類非常簡單,建議使用它

使用容器提供的JNDI DataSource。 這樣的DataSource可以通過JndiObjectFactoryBean在Spring ApplicationContext中作為DataSource bean公開

問題是: 我該如何做到這一點?

例如,如果我希望DataSource bean訪問我的自定義MySQL數據庫,那么我需要什么呢? 我應該在上下文配置等中寫什么?

如果使用Spring的基於XML模式的配置,請在Spring上下文中進行設置,如下所示:

<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配置進行設置:

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

您可以使用以下內容在tomcat的server.xml中聲明JNDI資源:

<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資源,如下所示:

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

參考文件:

編輯:已針對Tomcat 8和Spring 4更新了此答案。對Tomcat的默認數據源資源池設置進行了一些屬性名稱更改。

使用Spring的JavaConfig機制,您可以這樣做:

@Configuration
public class MainConfig {

    ...

    @Bean
    DataSource dataSource() {
        DataSource dataSource = null;
        JndiTemplate jndi = new JndiTemplate();
        try {
            dataSource = jndi.lookup("java:comp/env/jdbc/yourname", DataSource.class);
        } catch (NamingException e) {
            logger.error("NamingException for java:comp/env/jdbc/yourname", e);
        }
        return dataSource;
    }

}

假設您的tomcat配置中有“sampleDS”數據源定義,您可以在applicationContext.xml添加以下行以使用JNDI訪問數據源。

<jee:jndi-lookup expected-type="javax.sql.DataSource" id="springBeanIdForSampleDS" jndi-name="sampleDS"/>

您必須使用以下內容定義jee前綴的命名空間和架構位置:

xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"

文檔: C.2.3.1 <jee:jndi-lookup/> (簡單)

例:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

您只需要找出您的appserver綁定數據源的JNDI名稱。 這完全是服務器特定的,請查閱服務器上的文檔以了解具體方法。

請記住在bean文件的頂部聲明jee命名空間,如C.2.3 jee架構中所述

另一個特性:您可以在其中添加“Resource”標簽,而不是server.xml
your_application / META-INF / Context.xml(根據tomcat docs )是這樣的:

<Context>
<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
  username="dbUsername" password="dbPasswd"
  url="jdbc:postgresql://localhost/dbname"
  driverClassName="org.postgresql.Driver"
  initialSize="5" maxWait="5000"
  maxActive="120" maxIdle="5"
  validationQuery="select 1"
  poolPreparedStatements="true"/>
</Context>

根據Apache Tomcat 7 JNDI數據源HOW-TO頁面 ,web.xml中必須有資源配置:

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/TestDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>

這對我行得通

在你的spring類中,你可以注入一個注釋為bean的bean

@Autowired
@Qualifier("dbDataSource")
private DataSource dataSource;

並在context.xml中添加它

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

您可以使用在tomcat的server.xml中聲明JNDI資源

<Resource name="jdbc/TestDB" 
  global="jdbc/TestDB" 
  auth="Container" 
  type="javax.sql.DataSource" 
  driverClassName="com.mysql.jdbc.Driver" 
  url="jdbc:mysql://localhost:3306/TestDB" 
  username="pankaj" 
  password="pankaj123" 

  maxActive="100" 
  maxIdle="20" 
  minIdle="5" 
  maxWait="10000"/>

回到context.xml de spring添加這個

<ResourceLink name="jdbc/MyLocalDB"
                global="jdbc/TestDB"
                auth="Container"
                type="javax.sql.DataSource" />

如果像這樣的例子注入數據庫連接,請確保tomcat lib目錄中存在MySQL jar,否則tomcat將無法創建MySQL數據庫連接池。

我發現這個解決方案非常有用,可以完全刪除xml配置。

請使用JNDI和spring框架檢查此db配置。 http://www.unotions.com/design/how-to-create-oracleothersql-db-configuration-using-spring-and-maven/

通過本文,它解釋了基於數據庫jndi(db / test)配置創建數據庫配置是多么容易。 完成配置后,使用此jndi加載所有db存儲庫。 我覺得很有用。 如果@Pierre對此有疑問,請告訴我。 這是編寫db配置的完整解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM