tags:

views:

420

answers:

1

"java:comp/env/jdbc/MY_SQL_DS" does not work. I get a Naming Exception: NameNotFoundException. neither works "MY_SQL_DS" alone.name exception again.

I created another JNDI for a Mail Session named "MY_MailSession" and reference it like (javax.mail.Session) ctx.lookup( "MY_MailSession" ) which works...

what is the convention to refer the JDBC DataSource then?

A: 

I solved it the following way: hope this helps others having the same issue/problem afterwards...

protected Connection getConnection() {
            try {
                if (connection == null || connection.isClosed()) {
                    if (dataSource == null) {
                        // impliziter Initial Context von WebLogic ApplicationServer Environment
                        java.util.Hashtable environment = new java.util.Hashtable();
                        environment.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                        Context wlsic = new InitialContext(environment);
                        showJndiContext( wlsic, "", "");

                        // logischer JNDI Rootcontext der Serverkomponente, kann mehrfach verwendet werden
                        Context ctx = (Context) wlsic.lookup("java:comp/env"); 
                        showJndiContext( ctx, "", "");

                        // weiter mit Resourcenpfad
                        dataSource = (DataSource) ctx.lookup("MY_SQL_DS");
                    }
                    connection = dataSource.getConnection();
                }
            }
            catch (NamingException ne) {
                ne.printStackTrace();
                log.error(ne);
            }
            catch (SQLException sqlEx) {
                sqlEx.printStackTrace();
                log.error(sqlEx.getMessage());
            }
            return connection;
        }

        public static void showJndiContext(Context ctx, String name, String space) {
            if (null == name)
                name = "";
            if (null == space)
                space = "";

            try {
                NamingEnumeration en = ctx.list(name);
                while (en.hasMoreElements()) {
                    String delim = (null != name && 0 < name.length()) ? "/" : "";
                    NameClassPair nc = (NameClassPair) en.next();
                    System.out.println(space + name + delim + nc);
                    if (40 > space.length())
                        showJndiContext(ctx, nc.getName(), "    " + space);
                }
            }
            catch (javax.naming.NamingException ex) {
                //System.out.println( ex );
            }
        }
yli