如何在jboss之外通过jndi访问在其中的datasource

private Context getInitialContext() throws NamingException {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
return new InitialContext(environment);
}
public Connection getConnection(String dataSourceName) {
DataSource ds = null;
Connection conn = null;
try {
Context ctx = getInitialContext();
ds = (DataSource) ctx.lookup(dataSourceName);
conn = ds.getConnection();
return conn;
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}
我这样获取连接,在web访问这个bean可以得到数据,可是在jbuilder里直接运行就提示错误: ntsky not bound这样的错误。

我在mysql-ds里配制connection-url的时候,为什么只能用localhost或是127.0.0.1,不能用本地IP,很郁闷。

banq,帮忙解决下咯??

顶,那位大侠解决下?

JNDI这样的问题比较麻烦,你逐步确认JNDI name: ntsky 调用过程。

我是那样的,但就是不能从本地直接运行,后来看了资料也有很多人问怎样的的问题,不过都没有解决方案,不知道是不是真的不行。

Datasource的jndi非全局的,也就是说在Container范围外,是不能访问的。你用JNDI View看一下就知道了。它一定在Java: Namespace中,而不是Global: Namespace中。

那么请问该大侠,我该怎样才能解决这样的问题。

好像不行阿

这个是jboss的注释


<!-- The jndi name of the DataSource, it is prefixed with java:/ -->
<!-- Datasources are not available outside the virtual machine -->

<datasources>
<local-tx-datasource>
<jndi-name>SybaseDB</jndi-name>
<use-java-context>false</use-java-context>
<!-- Sybase jConnect URL for the database.
NOTE: The hostname and port are made up values. The optional
database name is provided, as well as some additinal Driver
parameters.
-->
<connection-url>jdbc:sybase:Tds:192.168.0.75:5000/wgtest?charset=eucgb</connection-url>
<driver-class>com.sybase.jdbc3.jdbc.SybDriver</driver-class>
<user-name>sa</user-name>
<password></password>
<min-pool-size>10</min-pool-size> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.SybaseExceptionSorter</exception-sorter-class-name>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->

<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>Sybase</type-mapping>
</metadata>
</local-tx-datasource>

</datasources>