2012-04-26 4 views
2

私はSpring MVC 3とMySQLサーバーを使用しています。 JDBC接続にJNDIを使用しようとしていますが、NULL DataSourceを返します。ここでは、NULLポインタ例外をスローするコードがあります。含まspring mvc3 integrationを使用したJNDI

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ファイルcantain:

 <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はnullです。

それが適切に

  /* 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(); 
     } 
+0

データソースの「」要素はどこにありますか? http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html – axtavt

+0

の要素がserver.xmlのにあります –

+0

申し訳ありませんがaxtavt、質問に要素を書き忘れていますcontext.xmlの私のリソース –

答えて

0

作業よりも、私は次のコードを使用している場合は、春のデバッグログをオンにし、および/またはデバッグ春、それは実際に見上げていますか見て、そしてあなたの直接のものとすることをを比較する必要がありますJNDIコードが実行しています。このような

何かが同様にあなたを投げすることができます

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

(あなたがJndiObjectFactoryBeanにこのプロパティを設定し、それが自動的にコンプ/ envを一部を追加します...何をチェック

とにかく、あなたが行っていることを確認するためには、春をデバッグすることができます。

関連する問題