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

04-05-16 skyyjl
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,帮忙解决下咯??

skyyjl
2004-05-18 22:56
顶,那位大侠解决下?

banq
2004-05-21 09:07
JNDI这样的问题比较麻烦,你逐步确认JNDI name: ntsky 调用过程。

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

廉价劳力
2004-05-26 08:04
Datasource的jndi非全局的,也就是说在Container范围外,是不能访问的。你用JNDI View看一下就知道了。它一定在Java: Namespace中,而不是Global: Namespace中。

skyyjl
2004-05-26 17:30
那么请问该大侠,我该怎样才能解决这样的问题。

廉价劳力
2004-05-27 01:05
好像不行阿

廉价劳力
2004-05-27 01:08
这个是jboss的注释

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

wangyuan
2007-12-19 09:32
<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>

猜你喜欢