1:在JBoss中配置一个名为“FirstDS”的数据源。
2:在JBoss中部署一个Web项目,在项目中通过JNDI查找FirstDS数据源,可以查找到数据源并
能成功访问数据库。
3:但是在Java Project中却无法查找FirstDS数据源。
代码如下:
public static void main(String[] args) { Context ctx = null; DataSource ds = null; Connection con = null; Statement stmt = null; ResultSet result = null; Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.setProperty(Context.PROVIDER_URL, "127.0.0.1:1099"); try { ctx = new InitialContext(props); ds = (DataSource) ctx.lookup("FirstDS"); con = ds.getConnection(); stmt = con.createStatement(); result = stmt.executeQuery("select * from student"); while (result.next()) { System.out.println(result.getInt(1) + " " + result.getString(2) + " " + result.getString(3)); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (result != null) result.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
异常信息如下:
Exception in thread "main" javax.naming.NameNotFoundException: FirstDS not bound at org.jnp.server.NamingServer.getBinding(NamingServer.java:542) at org.jnp.server.NamingServer.getBinding(NamingServer.java:550) at org.jnp.server.NamingServer.getObject(NamingServer.java:556) at org.jnp.server.NamingServer.lookup(NamingServer.java:296) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
解决方法:
<use-java-context> - A boolean indicating if the jndi-name should be prefixed with java: which causes the DataSource to only be accessible from within the jboss server vm. The default is true.
Configuring a DataSource for remote usage
As of jboss-4.0.0 there is support for accessing a DataSource from a remote client. The one change that is necessary for the client to be able to lookup the DataSource from JNDI is to specify use-java-context=false as shown here:
<datasources> <local-tx-datasource> <jndi-name>GenericDS</jndi-name> <use-java-context>false</use-java-context> <connection-url>...</connection-url>...
This results in the DataSource being bound under the JNDI name "GenericDS" instead of the default of "java:/GenericDS" which restricts the lookup to the same VM as the jboss server.
它说的大概意思就是:(该参数在数据源的配置文件中)
当你指定<use-java-context>的值为false时,你就可以在jboss运行的VM外的VM上查找到这个DataSource.
这个属性默认.为true
即,默认情况下你是不可以在JBOSS的VM外来查找这个数据源.
相关推荐
Jboss 优化配置 一. Jboss后台启动: 二. Jboss内存优化: 调整JVM 三. Jboss日志输出模式 去除不必要的日志输出 四. Jboss数据库连接池优化 五. Jboss部署目录优化 六. Jboss应用安全加固 七. 设置...
websphere安装配置(jvm,数据源还有集群) websphere安装配置(jvm,数据源还有集群) websphere安装配置(jvm,数据源还有集群)
JVM配置资料JVM配置资料JVM配置资料JVM配置资料
中文的JBoss7应用配置指南及是管理员手册,包括了JBoss所有的应用 1. jboss各主要版本特性 3 1.1. jboss4特性 3 1.2. jboss5特性 5 1.3. jboss6特性 6 1.4. jboss7特性 7 2. 为什么JBoss AS7 这么快 8 3. JBoss AS7...
jvm 配置jvm参数 配置jvm参数
JVM 开发环境配置,JAVA开发环境配置
4.4.5.1. 数据源 (Data sources) 74 4.4.5.1.1. JDBC驱动安装 74 4.4.5.1.2. 数据源定义 (Datasource Definitions) 75 4.4.5.1.3. 参考 78 4.4.5.2. 消息 (Messaging) 78 4.4.5.2.1. Connection Factories 78 4.4....
Jboss中间件下jvm参数调优配置 Jvm常见调优配置汇总
JBOSS\Tomcat最大连接数配置和jvm内存配置,解决内存溢出问题 Java中OutOfMemoryError(内存溢出)的情况 第一种OutOfMemoryError: PermGen space 第二种OutOfMemoryError: Java heap space
JVM运行时各种参数配置说明
websphere安装配置(jvm,数据源还有集群)
visualvm监控jvm及远程jvm监控方法
2.2.7配置数据源在APP应用加载前有效(各相关服务加载完毕) 16 2.2.8日志配置,配置log4j.xml 18 2.2.9配置contentType及charset,TOMCAT5.0下JSP统一配置 19 2.3 JVM最小、最大内存的设定 21 2.4 OS字符集问题 ...
JVM 源代码part1 (看我的上传记录 有1--9 个part)
如何配置jvm参数,并且调优,适合各路开发者,
JVM调优总结:典型配置举例
JVM内存结构,配置参数,JVM调优监控,待完善
jvm参数优化后,tomcat稳定可靠,附件为通过长时间在线测试的配置参数文件
JVM 源代码 part07
JVM 源代码 part06