简体   繁体   中英

NameNotFoundException in Jboss when connecting to database through JNDI

I am using JBoss 4.3.0. I am trying to create a datasource in the server to connect to MS SQL server.

I created a file myapp-mssql-ds.xml and placed it inside C:\\jboss-4.3.0\\server\\myapps\\deploy directory.

The following are the contents of the file :

   <datasources>
  <local-tx-datasource>
<jndi-name>jdbc/MyDatasource</jndi-name>
    <connection-url>jdbc:jtds:sqlserver://urlhere</connection-url>

<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<user-name>myuser</user-name>
<password>mypassword</password>


<min-pool-size>2</min-pool-size>
    <max-pool-size>70</max-pool-size>
    <blocking-timeout-millis>5000</blocking-timeout-millis>
    <idle-timeout-minutes>15</idle-timeout-minutes>

 </local-tx-datasource>
  </datasources>

and in the Java code I access it the following way

     DataSource dataSource;
     DAOFactory instance = null;

    String url ="jdbc/MyDatasource";
    try {
        dataSource = (DataSource) new InitialContext().lookup(url);
    } catch (NamingException e) {
        throw new DAOConfigurationException(
            "DataSource '" + url + "' is missing in JNDI.", e);
    }

      instance = new DataSourceDAOFactory(dataSource);

Finally, I get this exception,

javax.naming.NameNotFoundException: MyDatasource not bound

Am I missing something ? Do I have to make any other entries elsewhere ?

The JNDI name for the JDBC data source is bound relative to the java:/ context ( see the docs here ), so you just need to change String url = "jdbc/MyDatasource" to String url = "java:jdbc/MyDatasource" .

Alternatively, you can configure your data source with the use-java-context property set to false and not need to worry about prefixing the JNDI name with java: .

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