简体   繁体   中英

NameNotFoundException while opening connection from Datasource, JNDI

i had an application working with JSF and i oppened the connection manually, but now i have to change it and open all the connections from a Datasource, so i have an .xml file in the directory (Jboss 7)

<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
    <datasource
        jndi-name="java:jboss/datasources/MyForum"
        enabled="true"
        use-java-context="true"
        pool-name="MyForum">
            <connection-url>jdbc:hsqldb:hsql://localhost</connection-url>
            <driver>hsqldb.jar</driver>
            <pool></pool>
            <security>
                <user-name>sa</user-name>
                <password></password>
            </security>
    </datasource>
</datasources>

Then i have the code to acces the JNDI name in a class "JdbcHelper"

public Connection createConnection() {

    try {
        String jndiKey = getProperty("JNDI_DATASOURCE");

        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup(jndiKey);
        return ds.getConnection();

    } catch (NamingException e) {
        throw new RuntimeException("Can't open JDBC conection from JNDI", e);
    } catch (SQLException e) {
        throw new RuntimeException("Can't open JDBC conection", e);
    }
}

The line which is launching the exception is this one DataSource ds = (DataSource) ctx.lookup(jndiKey); I don't know if you need more information, but if you need just ask, i really need to do this and I can't find the solution :(

Update, here's the detail-message of the exception

datasources/MyForum -- service jboss.naming.context.java.jboss.datasources.MyForum

Thank you so much in advance. :)

我的猜测是您没有正确初始化InitialContext ,这可能会帮助您: 在JBoss AS 7.1.1中创建初始Context。

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