我尝试对 Weblogic 10.3.5 中的数据源进行 JNDI 查找。
我有以下代码:
try {
//jbInit();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(javax.naming.Context.PROVIDER_URL,"t3://localhost:7001");
try {
Context ctx = new InitialContext(env);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/sandboxDS");
conn = ds.getConnection();
} catch(Exception e){
e.printStackTrace();
}
if(conn != null){
System.out.println("Got connection...");
String colDescQuery =
"select column1 from my_table where table_name = 'your_table' order by col_order_no";
Statement colDescStmt = conn.createStatement();
ResultSet colDescRS = colDescStmt.executeQuery(colDescQuery);
while (colDescRS.next()) {
System.out.println(colDescRS.getString(1));
}
} else {
System.out.println("No connection...");
}
} catch (Exception e) {
e.printStackTrace();
}
当我将这段代码作为独立的 Java 程序运行时,它工作得很好。找到连接并且查询返回预期结果。
当我在小程序中使用相同的代码并从 JDeveloper 小程序查看器运行它时,它会卡在实例化 InitialContext 的位置。不会抛出任何异常,它只是挂起永远不会返回。
对于这里发生的事情有什么想法吗?对于两次运行,我的类路径中都有 weblogic.jar 和 wlthint3client.jar。
谢谢...
最佳答案
小程序有沙箱限制,因此它们无法连接到下载源以外的服务器。
确保您的小程序也部署到 JNDI 数据源所在的同一 WebLogic Server。
关于java - 为什么我无法在小程序中获取远程数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13846727/