转 jboss 配置,貌似xxxx…

http://blog.csdn.net/vebasan/article/details/5346493

同weblogic不同,在jboss中配置数据源并没有可视化的控制台.一般.大家都是到%JBOSS_HOME%/docs/examples/jca目录下,找到自己所要配置的数据源的模板文件,然后修改jndi-name,connection-url,driver-class,user-name,password,等等,然后把修改后的配置文件丢到要使用的domain下的deploy目录下就可以了,但是这时住往会有个问题. 
你会发现,你在客户端查找不到你刚刚配置的数据源.

Xml代码 
  1. <</span>datasources>  
  2.   <</span>local-tx-datasource>  
  3.     <</span>jndi-name>MySqlDS</</span>jndi-name>  
  4.     <</span>connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</</span>connection-url>  
  5.     <</span>driver-class>com.mysql.jdbc.Driver</</span>driver-class>  
  6.     <</span>user-name>x</</span>user-name>  
  7.     <</span>password>y</</span>password>  
  8.     <</span>exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</</span>exception-sorter-class-name>  
  9.     <</span>metadata>  
  10.        <</span>type-mapping>mySQL</</span>type-mapping>  
  11.     </</span>metadata>  
  12.   </</span>local-tx-datasource>  
  13. </</span>datasources>  
  1. <</span>datasources>  
  2.   <</span>local-tx-datasource>  
  3.     <</span>jndi-name>MySqlDS</</span>jndi-name>  
  4.     <</span>connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</</span>connection-url>  
  5.     <</span>driver-class>com.mysql.jdbc.Driver</</span>driver-class>  
  6.     <</span>user-name>x</</span>user-name>  
  7.     <</span>password>y</</span>password>  
  8.     <</span>exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</</span>exception-sorter-class-name>  
  9.     <</span>metadata>  
  10.        <</span>type-mapping>mySQL</</span>type-mapping>  
  11.     </</span>metadata>  
  12.   </</span>local-tx-datasource>  
  13. </</span>datasources>  



上面是一个从%JBOSS_HOME%/docs/examples/jca目录下copy下来的配置文件. 
以它为例. 
我们看一下, 在客户端来查找这个新配置的数据源会出现什么情况呢. 


客户端代码如下: 
  

Java代码 
  1. Properties  prop=new Properties();   
  2.    prop.put(Context.INITIAL_CONTEXT_FACTORY,   
  3.    "org.jnp.interfaces.NamingContextFactory");   
  4.    prop.put(Context.PROVIDER_URL, "localhost:1099");   
  5.    Context  context=new InitialContext(prop);   
  6.    context.lookup("MySqlDS"));  
[java] view plaincopy
  1. Properties  prop=new Properties();  
  2.    prop.put(Context.INITIAL_CONTEXT_FACTORY,  
  3.    "org.jnp.interfaces.NamingContextFactory");  
  4.    prop.put(Context.PROVIDER_URL, "localhost:1099");  
  5.    Context  context=new InitialContext(prop);  
  6.    context.lookup("MySqlDS"));  


   

结果是一大堆异常:

Java代码 
  1. Exception in thread "main" javax.naming.NameNotFoundException: MySqlDS not bound   
  2.     at org.jnp.server.NamingServer.getBinding(NamingServer.java:542)   
  3.     at org.jnp.server.NamingServer.getBinding(NamingServer.java:550)   
  4.     at org.jnp.server.NamingServer.getObject(NamingServer.java:556)   
  5.     at org.jnp.server.NamingServer.lookup(NamingServer.java:296)   
  6.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   
  7.     
[java] view plaincopy
  1. Exception in thread "main" javax.naming.NameNotFoundException: MySqlDS not bound  
  2.     at org.jnp.server.NamingServer.getBinding(NamingServer.java:542 
  3.     at org.jnp.server.NamingServer.getBinding(NamingServer.java:550 
  4.     at org.jnp.server.NamingServer.getObject(NamingServer.java:556 
  5.     at org.jnp.server.NamingServer.lookup(NamingServer.java:296 
  6.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  7.     




JNDI查找不到"MySqlDS"这个名称.. 
为什么呢? 
我们去JBOSS的控制台去查看JNDI树结构. 

Other components with java:comp namespace 
java: Namespace 
  +- ClusteredXAConnectionFactory (class: org.jboss.jms.client.JBossConnectionFactory) 
  +- jaas (class: javax.naming.Context) 
   +- messaging (class: org.jboss.security.plugins.SecurityDomainContext) 
  +- TransactionPropagationContextImporter (class: com.arjuna.ats.internal.jbossatx.jta.PropagationContextManager) 
  +- MySqlDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource) 

很明显"MySqlDS"做为key被绑定在JNDI上了.那么为什么还会出错呢... 

: )我浪费了很长时间在找这个错误的过程中,中间甚至一度以为,JBOSS不支持在服务器外获取数据源.后来一个家伙好像在写EJB3.0的时候也遇到这个问题,并且找来了下面这个项文文档..让我惊为天人啊...

- 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: 

      GenericDS    false    ...... 
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. 

身为高级知识分子,我相信大家即使读不懂上面的英文,金山词霸总是会使用的. 
它说的大概意思就是. 
默认情况下,当你的程序运行在jboss 同一台JVM当中你可以通过JNDI.得到这个datasource...
而当你的程序运行在其它的JVM当中,你通过JNDI是查询不到这个datasource 的.
只有当你把设置成false时,你才可以在其它JVM上通过JNDI来得到DS.
 

其实吧,这个问题看起来很普通,解决起来也很简单只是加了一行代码,但是.JBOSS服务器的提供厂商是否有想过,一个example配置文件中,具然少了这种重要的配置信息. 
至少你把它配上,然后注掉也好啊. 

原文地址:https://www.cnblogs.com/wangduqiang/p/4180953.html