【问题标题】:"java:comp/env/jdbc/MY_SQL_DS" or "MY_SQL_DS" or what else to reference the DataSource from within Java?“java:comp/env/jdbc/MY_SQL_DS”或“MY_SQL_DS”或其他什么可以从 Java 中引用 DataSource?
【发布时间】:2010-10-12 04:43:29
【问题描述】:

“java:comp/env/jdbc/MY_SQL_DS”不起作用。我得到一个命名异常:NameNotFoundException。单独的“MY_SQL_DS”都不起作用。再次命名异常。

我为名为“MY_MailSession”的邮件会话创建了另一个 JNDI,并像 (javax.mail.Session) ctx.lookup("MY_MailSession") 一样引用它...

那么引用 JDBC 数据源的约定是什么?

【问题讨论】:

    标签: jndi


    【解决方案1】:

    我通过以下方式解决了它: 希望这可以帮助其他人之后遇到同样的问题/问题...

    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 );
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 2012-07-22
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      相关资源
      最近更新 更多