Senin, 03 Desember 2012

JNDI look-up JBOSS 7

I used to access JNDI lookup for datasource in JBOSS AS 5 as well as JBOSS AS 6 by using this way:

Unfortunately, it seems doesn't work in AS 7, as it produces error:
javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.omega.war:main" from Service Module Loader

Thanks to this link, the solution is quite simple, only put the INITIAL_CONTEXT_FACTORY to org.jboss.as.naming.InitialContextFactory.

One additional note, put the JNDI name with prefix java:/ or java:jboss/.

2 komentar:

  1. Hi , i want to lookup jdbc datasource from java standalone app

    //

    public static void main(String[] args) throws SQLException {
    DataSource cname = null;
    Context context = null;

    try {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
    context = new InitialContext(properties);
    cname = (DataSource) context.lookup("java:jboss/datasources/dream");
    Connection connection = cname.getConnection();
    System.out.println("connection-----masum : "+ connection.isClosed());
    } catch (NamingException e) {
    e.printStackTrace();
    }

    }



    Error :
    //

    public static void main(String[] args) throws SQLException {
    DataSource cname = null;
    Context context = null;

    try {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
    context = new InitialContext(properties);
    cname = (DataSource) context.lookup("java:jboss/datasources/dream");
    Connection connection = cname.getConnection();
    System.out.println("connection-----masum : "+ connection.isClosed());
    } catch (NamingException e) {
    e.printStackTrace();
    }

    }

    BalasHapus
    Balasan
    1. Do you mean your standalone app need to lookup the datasource in jboss using JNDI? Or do you want to create a datasource in your standalone app?

      Hapus