2

Spring MVC 3 と MySQL サーバーを使用しています。JDBC 接続に JNDI を使用しようとしていますが、NULL DataSource が返されます。これは、null ポインター例外をスローするコードの一部です。

server.xml以下を含むファイル:

  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

context.xmlファイルの内容

    <Resource name="jdbc/Test" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/test"/>

web.xmlファイルの内容:

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

despatcher-servlet.xmlファイル:

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

    <bean name="dbConfiguration" class="com.biztree.springtest.database.DataBaseConfiguration" >
        <property name="dataSource" ref="myDataSourceInJndi" />
    </bean>

DataBaseConfiguration.java

package com.biztree.springtest.database;

import javax.sql.DataSource;

public class DataBaseConfiguration {

    DataSource dataSource;

    public DataBaseConfiguration() {
        // TODO Auto-generated constructor stub
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DataSource getDataSource() {
        return dataSource;
    }
}

そして聞くのは接続するためのコードです

          /*    this code work throw NullPointerException */
        try {

            DataBaseConfiguration baseConfiguration = new DataBaseConfiguration();
            DataSource ds = baseConfiguration.getDataSource();
            System.out.println("ds Object : " + ds);
            connection = ds.getConnection();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

しかし、それdsはヌルです。

次のコードを使用すると、正常に動作します

          /*    this code work fine */
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)  envCtx.lookup("jdbc/Test");

            System.out.println("ds Object : " + ds);
            connection = ds.getConnection();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
4

1 に答える 1

0

spring のデバッグ ロギングをオンにするか、spring をデバッグして実際に検索しているものを確認し、それを直接の JNDI コードが行っていることと比較する必要があります。

このようなものもあなたを捨てている可能性があります:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jndi/JndiLocatorSupport.html#setResourceRef(boolean )

(このプロパティを JndiObjectFactoryBean に設定すると、自動的に comp/env 部分が追加されます... デフォルト値が何であるかを確認し、正しく設定されていることを確認してください)

いずれにせよ、Spring をデバッグして何をしているかを確認すると確認できます。

于 2012-04-30T19:20:07.247 に答える