JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Application Server » JBoss  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource
zhjjava





發文: 5
積分: 0
於 2005-10-09 10:20 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好,
jboss3.26+linux+jdk1.4.2,数据库oracle9i

项目中出现一点问题,就是我们应用启动之处一般都进行若干初始化吧。

datasource配置肯定是成功的,基本是按example/jca/oracle_ds.xml来调整

的。

初始化我尝试了几个途径都不能获取到datasource,通过JNDI来获取。

但是只要整个应用一部署完毕,就能正常通过JNDI获取到datasource了。很郁



以下就描述一种:
1、配置数据源:
<!--oracle_ds.xml-->
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/test</jndi-name>
....我也尝试在web.xml及jboss-web.xml配置了

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

===========

<jboss-web>
<!--<context-root>/</context-root>-->

<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<jndi-name>java:/jdbc/test</jndi-name>
</resource-ref>
</jboss-web>

2、java代码,

在init中
Context init = new javax.naming.InitialContext();
ctx = (Context) init.lookup("java:comp/env");
DataSource ds = (DataSource)ctx.lookup("jdbc/test");

或 Context ctx = new javax.naming.InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/jdbc/test");

或 Context ctx = new javax.naming.InitialContext();
DataSource ds =

(DataSource)ctx.lookup("java:comp/env/jdbc/test");

都是失败的,都是提示datasource 没有bound

类似的信息:javax.naming.NamingException: Could not dereference

object [Root exception is javax.naming.NameNotFoundException: jdbc

not bound]

而只要整个应用都部署完毕,程序中的获取数据源的代码全都正常工作,很郁

闷。

相关oracle jar已经拷入server/all/lib中

期待高手解决。谢谢


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
gary0718





發文: 64
積分: 0
於 2005-10-12 13:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
設定上我只設了ds.xml,沒有設定jboss.xml之下,以你的例子來說
1、配置数据源:
<!--oracle_ds.xml-->
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/test</jndi-name>

2、java代码,
Context ctx = new javax.naming.InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:jdbc/test");

這樣寫就可以了


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
zhjjava





發文: 5
積分: 0
於 2005-10-13 10:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
首先谢谢台胞的答复,我在jboss forum及theserverside上请求帮助,没有人搭理我,靠。

但按你的答复,我测试了一下,还是不行,实际上日志显示是bound到"java:/jdbc/test"的。

请注意我是在StartupServlet 的init方法中,我无法获取到datasource,请求大家再帮忙,我也在研读jboss源码,好找出问题所在。谢谢

错误信息:

2005-10-13 10:02:42,843 INFO [STDOUT] StartupServlet working!
2005-10-13 10:02:42,843 INFO [STDOUT] ************************
2005-10-13 10:02:42,859 INFO [STDOUT] javax.naming.NameNotFoundException: jdbc not bound
2005-10-13 10:02:42,859 INFO [STDOUT]   at org.jnp.server.NamingServer.getBinding(NamingServer.java:490)
2005-10-13 10:02:42,859 INFO [STDOUT]   at org.jnp.server.NamingServer.getBinding(NamingServer.java:498)
2005-10-13 10:02:42,859 INFO [STDOUT]   at org.jnp.server.NamingServer.getObject(NamingServer.java:504)
2005-10-13 10:02:42,859 INFO [STDOUT]   at org.jnp.server.NamingServer.lookup(NamingServer.java:248)
2005-10-13 10:02:42,859 INFO [STDOUT]   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:530)
2005-10-13 10:02:42,875 INFO [STDOUT]   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:509)
2005-10-13 10:02:42,875 INFO [STDOUT]   at javax.naming.InitialContext.lookup(InitialContext.java:347)
2005-10-13 10:02:42,875 INFO [STDOUT]   at com.test.helper.StartupServlet.init(StartupServlet.java:47)


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
gary0718





發文: 64
積分: 0
於 2005-10-13 10:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
兄台~ Take easy~不要太激動
以我的例子來說好了,用db2當資料來源,寫一個db2-ds.xml放在server的deploy下,內容如下
1:db2-ds.xml
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/RONS</jndi-name>
<connection-url>jdbc:db2:rons</connection-url>
<driver-class>com.ibm.db2.jcc.DB2Driver</driver-class>
<user-name>db2admin</user-name>
<password>db2admin</password>
<min-pool-size>0</min-pool-size>
<metadata>
<type-mapping>DB2</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

2:在server start的時候去看console中的訊息可以得知datasource是否有bound

10:40:22,312 INFO [WrapperDataSourceService] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=jdbc/RONS to JNDI name 'java:jdbc/RONS'

3:在EJB裡寫的方法如下
Context ctx = new javax.naming.InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:jdbc/RONS");

這樣的設定在我的環境是work的,你可以在確認一下server start的message


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
gary0718





發文: 64
積分: 0
於 2005-10-13 11:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
記得還有一種寫法可以試試看
1
2
3
4
5
6
7
8
9
10
11
Hashtable evn = new Hashtable();
    evn.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    evn.put(Context.PROVIDER_URL, "localhost:1099");
    Context initCtx = null;
    DataSource ds = null;
    try {
      initCtx = new InitialContext(evn);
      ds = (DataSource)initCtx.lookup("java:jdbc/RONS");
    } catch (NamingException e) {
      e.printStackTrace();
    }


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
zhjjava





發文: 5
積分: 0
於 2005-10-13 11:20 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢你的倾力解答,应该是绑定到"java:/jdbc/test",当然"java:jdbc/test"估计也是行的。

有劳你帮我在你的环境里写个StartupServlet试一下谢谢。

The rather that when I read the log,I find the datasource has been bound to "java:/jdbc/test".

The state of datasource must be valid.
........
mbeans:
jboss.jca:service=LocalTxCM,name=jdbc/test state: Registered
jboss.jca:service=ManagedConnectionPool,name=jdbc/test state: Started
jboss.jca:service=ManagedConnectionFactory,name=jdbc/test state: Started
.....
.......
[org.jboss.web.tomcat.tc5.TomcatDeployer] Linking 'jdbc/test' to JNDI name: java:/jdbc/test

Thus once the web-app has deployed,it is normal for accessing the datasource! I'm worried and indignant.

相关JAVA CODE:

package com.test;

import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.naming.Context;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class StartupServlet extends HttpServlet {
/**
* initialize the system.
*/
public void init (ServletConfig config) throws ServletException {
super.init (config);
System.out.println("StartupServlet working!");
try{
// Hashtable ht = new Hashtable();
// ht.put(Context.INITIAL_CONTEXT_FACTORY ,"org.jnp.interfaces.NamingContextFactory");
// ht.put(Context.PROVIDER_URL,"jnp://localhost:1099");
// ht.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
System.out.println("************************");
Context init = new javax.naming.InitialContext();

DataSource ds = (DataSource)init.lookup("java:/jdbc/test");

Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement("select * from entity");
ResultSet rs = ps.executeQuery();
rs.next();
System.out.println("RRRRRRRRR"+rs.getString(1));

}catch(Throwable e){
e.printStackTrace();
}

}

/**
* Releases system wide objects , when the server is down
*/
public void destroy() {
}
}

<!---web.xml--->

<servlet>
<servlet-name>properties</servlet-name>
<servlet-class>com.test.StartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

有劳你帮我在你的环境里写个StartupServlet试一下谢谢。

jboss3.2.6,我在jboss4.0里测试好像也不行。

谢谢。


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
gary0718





發文: 64
積分: 0
於 2005-10-13 11:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
開發環境為JDK1.5.0_03 + Eclipse3.1 + WTP 0.7 + JBoss 4.0.2

你是否可以tryㄧ下JBoss4.0.2,也許在JBoss4.0.2就可以解決了


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:gary0718]
metavige

麥塔.米奇

版主

發文: 2133
積分: 10
於 2005-10-14 14:07 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我發現
你的描述中有一個部份,是提到"部署完畢之後,就可以lookup了"

那表示,這個問題的發生點,應該就是在"程式的執行時機"的問題
若你把 lookup jndi 的程式,寫在某個 servlet 的 service() 內
然後在 web.xml 內定義 load-on-startup

讓你在作 lookup 的時候, "確定" jndi 在 jboss 內已經存在,這樣就不會有 jdbc not bound 的問題了吧

我想這個問題,跟 jboss 是哪個版本,應該沒有甚麼關係


reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
zhjjava





發文: 5
積分: 0
於 2005-11-09 14:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈哈,我已搞定这个问题,我已把我在jboss/theserverside forum上发的帖子给回复了,感谢这么多的朋友答复我,谢谢,还是同胞好,老外都不理我,加油同胞们.

下面把解决方法贴出来,是蹩脚的英文,哈哈,大家不要怪我.

可以访问我的blog
http://spaces.msn.com/members/zhj-michael/

是11月9日的blog,the title is "JBOSS,datasource,But In the init() mothod couldn't lookup datasource."

my msn:zhj_michael@hotmail.com

the question and answer u can refer to

http://www.jboss.com/index.html?module=bb&op=viewtopic&t=70600

http://www.theserverside.com/discussions/thread.tss?thread_id=37016

I have written some words about question and answer there.

the next message is the full answer info.

==========================================================

hahaha,I have solved the problem.
We maybe should offer the answer if we knew,so that it can save many people's time!
I need thank the jboss-wiki,
http://wiki.jboss.org/wiki/Wiki.jsp?page=DependOnDataSource .

That gives me the inspiration!

Thus in admin-doc,JBoss Admin Development Guide(jboss3.2.6), Figure 3.4,didn't offer the depends element.

I can find some info in ...docs/dtd/jboss-web_3_2.dtd.
But when I wrote this message, I found the Figure 9.1----"The complete jboss-web.xml descriptor DTD", ah... ah... ah....

Reference http://wiki.jboss.org/wiki/Wiki.jsp?page=DependOnDataSource .

! Depending on a DataSource
To specify a dependency on a DataSource deployment, you need to depend on the service which binds the DataSource into JNDI.
The DataSource is not bound into JNDI until the service jboss.jca:service=DataSourceBinding,name=(jndi-name) has been
started. The (jndi-name) is the value of the jndi-name attribute for your DataSource as defined in the *-ds.xml.
The associated connection manager service is a function of the type of DataSource and its jndi-name attribute value. The
MBean names for the types of DataSource deployments are:

no-tx-datasource
jboss.jca:service=NoTxCM?,name=(jndi-name)
local-tx-datasource
jboss.jca:service=LocalTxCM?,name=(jndi-name)
xa-datasource
jboss.jca:service=XATxCM?,name=(jndi-name)
no-tx-connection-factory
jboss.jca:service=NoTxCM?,name=(jndi-name)
tx-connection-factory
jboss.jca:service=TxCM?,name=(jndi-name)


My datasource config is:
1
2
3
4
5
6
<!--in .../deploy/*-ds.xml-->
<datasources>
  <local-tx-datasource>
    <jndi-name>jdbc/test</jndi-name>
    <connection-url>jdbc:oracle:thin:@192.168.1.10:1521:ora</connection-url>
........

So the related service name is "jboss.jca:service=LocalTxCM,name=jdbc/test".

I modified ...sar/...war/WEB-INF/jboss-web.xml(if not exist,create it!),then run jboss,I have looked up the datasource!!!

1
2
3
<jboss-web>
  <depends>jboss.jca:service=LocalTxCM,name=jdbc/test</depends>
</jboss-web>  


In the log,I can find these info:

2005-11-08 19:12:49,859 DEBUG [org.jboss.system.ServiceController] waiting in start
jboss.web.deployment:war=Crm.war,id=843359960 on jboss.jca:service=LocalTxCM,name=jdbc/test
...........
2005-11-08 19:12:51,046 DEBUG [org.jboss.system.ServiceController] Starting dependent components for:
jboss.jca:service=LocalTxCM,name=jdbc/test dependent components: [ObjectName: jboss.web.deployment:war=Crm.war,id=843359960
state: CREATED
I Depend On: jboss.jca:service=LocalTxCM,name=jdbc/test
Depends On Me: ]
2005-11-08 19:12:51,046 DEBUG [org.jboss.system.ServiceController] starting service
jboss.web.deployment:war=Crm.war,id=843359960

In more detail,I will offer later on. Thank u jboss.Inc!!!


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
zhjjava





發文: 5
積分: 0
於 2005-11-11 14:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
中文版:

更好一些或是更完整的答案信息:

我的blog: http://spaces.msn.com/members/zhj-michael/

my msn:zhj_michael@hotmail.com

英文版:

http://spaces.msn.com/members/zhj-michael/Blog/cns!1pygmarxwX748Rh107mOyeGQ!180.entry

中文版:
http://spaces.msn.com/members/zhj-michael/Blog/cns!1pygmarxwX748Rh107mOyeGQ!181.entry

下面的信息是全部的答案信息

==============================================

我已经解决了此问题
如果一个问题我们知道答案,也许应该尽可能的提供出来,这样能节约很多人的时间。互相帮助吧,希望大家都能
给别人提供些有益的答案或信息,不能只是去从internet上获取帮助。

我有必要感谢 jboss-wiki,
http://wiki.jboss.org/wiki/Wiki.jsp?page=DependOnDataSource .

这一页的内容给了我灵感。

但是在admin-doc,JBoss Admin Development Guide(jboss3.2.6),从jboss上下载的文档,在插图 Figure 3.4,并没有提供"depends"这个元素.

我就在 ...docs/dtd/jboss-web_3_2.dtd这个描述文件中找到了相关说明,各位需要注意的是depends是jboss的扩展,并不是web2.3标准应用.

但就在我写这篇文章时,我发现了插图Figure 9.1----"The complete jboss-web.xml descriptor DTD", 看到了"depends",气死我了。

透过http://wiki.jboss.org/wiki/Wiki.jsp?page=DependOnDataSource . 我们发现:

======原文引用==============
! Depending on a DataSource
To specify a dependency on a DataSource deployment, you need to depend on the service which binds the DataSource into JNDI.
The DataSource is not bound into JNDI until the service jboss.jca:service=DataSourceBinding,name=(jndi-name) has been
started. The (jndi-name) is the value of the jndi-name attribute for your DataSource as defined in the *-ds.xml.
The associated connection manager service is a function of the type of DataSource and its jndi-name attribute value. The
MBean names for the types of DataSource deployments are:


1
2
3
4
5
6
7
8
9
10
no-tx-datasource 
jboss.jca:service=NoTxCM?,name=(jndi-name) 
local-tx-datasource 
jboss.jca:service=LocalTxCM?,name=(jndi-name) 
xa-datasource 
jboss.jca:service=XATxCM?,name=(jndi-name) 
no-tx-connection-factory 
jboss.jca:service=NoTxCM?,name=(jndi-name) 
tx-connection-factory 
jboss.jca:service=TxCM?,name=(jndi-name) 


通过上述描述可以知道,你的数据源配置不同,则对应的依赖服务名信息也不同。

我的数据源配置是:

1
2
3
4
5
6
 <!--in .../deploy/*-ds.xml-->
<datasources>
  <local-tx-datasource>
    <jndi-name>jdbc/test</jndi-name>
    <connection-url>jdbc:oracle:thin:@192.168.1.10:1521:ora</connection-url>
........


因此相关的服务名就是:"jboss.jca:service=LocalTxCM,name=jdbc/test".

我修改了 ...sar/...war/WEB-INF/jboss-web.xml(如果不存在该文件,就创建它),然后运行jboss,就可以在初始化时找到datasource,并正确获取数据了!!!

配置文件如下:

1
2
3
 <jboss-web>
  <depends>jboss.jca:service=LocalTxCM,name=jdbc/test</depends>
</jboss-web>  



在日志文件中,我们可以看到如下信息,信息显示是我们的应用Crm.war在等待它所依赖的服务,就是我们在上述文件中设定的那个服务,再接下去,等我们所设定那个依赖服务启动后,jboss去启动依赖于该服务那些服务,如Crm.war:

2005-11-08 19:12:49,859 DEBUG [org.jboss.system.ServiceController] waiting in start
jboss.web.deployment:war=Crm.war,id=843359960 on jboss.jca:service=LocalTxCM,name=jdbc/test
...........
2005-11-08 19:12:51,046 DEBUG [org.jboss.system.ServiceController] Starting dependent components for:
jboss.jca:service=LocalTxCM,name=jdbc/test dependent components: [ObjectName: jboss.web.deployment:war=Crm.war,id=843359960
state: CREATED
I Depend On: jboss.jca:service=LocalTxCM,name=jdbc/test
Depends On Me: ]
2005-11-08 19:12:51,046 DEBUG [org.jboss.system.ServiceController] starting service
jboss.web.deployment:war=Crm.war,id=843359960


测试结果正常。

Thank u jboss.Inc!!!

附初始工作的一点说明:

初始化数据工作也可以配置listener来完成
配置../WEB-INF/web.xml,这是web2.3中的标准应用

1
2
3
4
5
6
7
<web-app>
........
  <listener>
    <listener-class>com.bs.listener.ApplicationListener</listener-class>
  </listener>
.......
</web-app>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//code
....
public class ApplicationListener
  implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
....
  public void contextInitialized(ServletContextEvent servletContextEvent) {
....
        try{
        Context init = new javax.naming.InitialContext();
        DataSource ds = (DataSource)init.lookup("java:jdbc/test");
        Connection con = ds.getConnection();
        }catch(Throwable e){
          ... 
          e.printStackTrace();
        }
.....
  }


我所提问题中的初始化方法是StartupServlet
配置../WEB-INF/web.xml

1
2
3
4
5
6
7
8
9
<web-app>
...
  <servlet>
    <servlet-name>properties</servlet-name>
    <servlet-class>com.bs.helper.StartupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>  
...
</web-app>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//code
......
public class StartupServlet extends HttpServlet {
    /**
     * initialize the system.
     */
    public void init (ServletConfig config) throws ServletException {
        super.init (config);
        try{
 
        Context init = new javax.naming.InitialContext();
        DataSource ds = (DataSource)init.lookup("java:jdbc/test");
        Connection con = ds.getConnection();
...
        }catch(Throwable e){
          e.printStackTrace();
        }
 
    }
....
}


reply to postreply to post
作者 Re:JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource [Re:zhjjava]
abcwps





發文: 17
積分: 0
於 2007-03-26 16:59 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢zhjjava

reply to postreply to post
» JWorld@TW »  Application Server » JBoss

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8