简体   繁体   中英

JNDI with spring mvc3 integration

I am using Spring MVC 3 and MySQL server. I am trying to use JNDI for the JDBC connectivity, but it returns NULL DataSource. Here is the piece of code which throws null pointer exception.

server.xml file that contain:

  <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 file content

    <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 file 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 file:

    <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;
    }
}

and hear is the code to connect

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

but it ds is null.

If I use following code than it working properly

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

you need to turn on debug logging for spring, and/or debug spring to see what it's actually looking up, and compare that with what your direct JNDI code is doing.

something like this could be throwing you off as well:

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

( you set this property on the JndiObjectFactoryBean and it will automatically add the comp/env part... check for what the default value is and make sure it's set correctly)

anyway you will be able to confirm once you debug spring to see what it's doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM