"جافا: شركات / الحياة الفطرية / جدبك / MY_SQL_DS" أو "MY_SQL_DS" أو ماذا مرجع مصدر البيانات من داخل جافا؟

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

  •  06-07-2019
  •  | 
  •  

سؤال

و"جافا: شركات / الحياة الفطرية / جدبك / MY_SQL_DS" لا تعمل. أحصل على استثناء التسمية: NameNotFoundException. لا يعمل "MY_SQL_DS" alone.name استثناء مرة أخرى.

وأنا خلقت JNDI آخر لجلسة بريد يدعى "MY_MailSession" ومرجع له مثل (javax.mail.Session) ctx.lookup ( "MY_MailSession") الذي يعمل ...

ما هي اتفاقية إحالة JDBC مصدر البيانات بعد ذلك؟

هل كانت مفيدة؟

المحلول

وأنا حلها على النحو التالي: نأمل أن يكون هذا يساعد الآخرين وجود نفس القضية / المشكلة بعد ذلك ...

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 );
            }
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top