“Java: comp / env / jdbc / MY_SQL_DS” ou “MY_SQL_DS” ou quoi d'autre pour référencer le DataSource à partir de Java?

StackOverflow https://stackoverflow.com/questions/626314

  •  06-07-2019
  •  | 
  •  

Question

" java: comp / env / jdbc / MY_SQL_DS " ne marche pas. Je reçois une exception de dénomination: NameNotFoundException. ni fonctionne " MY_SQL_DS " alone.name exception à nouveau.

J'ai créé un autre JNDI pour une session de messagerie nommé "MY_MailSession". et référencez-le comme (javax.mail.Session) ctx.lookup ("MY_MailSession") qui fonctionne ...

quelle est la convention pour faire référence au JDBC DataSource alors?

Était-ce utile?

La solution

Je l'ai résolu de la manière suivante: espérons que cela aidera les autres personnes ayant le même problème / problème plus tard ...

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 );
            }
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top