关于jboss 5.0.0 上配置数据源要注意的一个问题 Java获取JBOSS数据源
2011-12-20 15:11
489 查看
同weblogic不同,在jboss中配置数据源并没有可视化的控制台.一般.大家都是到%JBOSS_HOME%\docs\examples\jca目录下,找到自己所要配置的数据源的模板文件,然后修改jndi-name,connection-url,driver-class,user-name,password,等等,然后把修改后的配置文件丢到要使用的domain下的deploy目录下就可以了,但是这时住往会有个问题.
你会发现,你在客户端查找不到你刚刚配置的数据源.
Xml代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>x</user-name>
<password>y</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
上面是一个从%JBOSS_HOME%\docs\examples\jca目录下copy下来的配置文件.
以它为例.
我们看一下, 在客户端来查找这个新配置的数据源会出现什么情况呢.
客户端代码如下:
Java代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
Properties prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
Context context=new InitialContext(prop);
context.lookup("MySqlDS"));
结果是一大堆异常:
Java代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
Exception in thread "main" javax.naming.NameNotFoundException: MySqlDS 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)
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的时候也遇到这个问题,并且找来了下面这个项文文档..让我惊为天人啊...
<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服务器的提供厂商是否有想过,一个example配置文件中,具然少了这种重要的配置信息.
至少你把它配上,然后注掉也好啊.
PS:上述数据源的配置如果不
填加 <use-java-context>false</use-java-context>
你基本上就会现,你在JBOSS上写的第一组EntityBean都会报找不到数据源的错误.
各位学习EJB3.0的小哥们留意...
你会发现,你在客户端查找不到你刚刚配置的数据源.
Xml代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>x</user-name>
<password>y</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
<datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>x</user-name> <password>y</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
上面是一个从%JBOSS_HOME%\docs\examples\jca目录下copy下来的配置文件.
以它为例.
我们看一下, 在客户端来查找这个新配置的数据源会出现什么情况呢.
客户端代码如下:
Java代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
Properties prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
Context context=new InitialContext(prop);
context.lookup("MySqlDS"));
Properties prop=new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); prop.put(Context.PROVIDER_URL, "localhost:1099"); Context context=new InitialContext(prop); context.lookup("MySqlDS"));
结果是一大堆异常:
Java代码
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
Exception in thread "main" javax.naming.NameNotFoundException: MySqlDS 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)
Exception in thread "main" javax.naming.NameNotFoundException: MySqlDS 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)
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的时候也遇到这个问题,并且找来了下面这个项文文档..让我惊为天人啊...
<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服务器的提供厂商是否有想过,一个example配置文件中,具然少了这种重要的配置信息.
至少你把它配上,然后注掉也好啊.
PS:上述数据源的配置如果不
填加 <use-java-context>false</use-java-context>
你基本上就会现,你在JBOSS上写的第一组EntityBean都会报找不到数据源的错误.
各位学习EJB3.0的小哥们留意...
相关文章推荐
- 关于jboss 5.0.0 上配置数据源要注意的一个问题
- ComboBox配置数据源需要注意的一个问题
- 一个小的知识点关于java获取系统时间的问题
- 一个关于oracle和java环境配置的问题
- 关于java的io一个需要注意的问题
- solr中一个问题——顺便讨论java中关于中包括main函数以及涉及资源(变量)操作中的类
- 一个通过JNDI配置birt数据源的出现的诡异问题及解决方法
- Java JSON解析中一个关于双引号的问题
- 关于【apache- tomcat- 5.5.15/conf /Catalina/localhost配置虚拟目录】时的一些问题。(配置web项目的方式不止一种,虚拟目录就是一个)
- java使用js引擎时,关于时间的问题getTime()获取为NAN的问题
- 关于一个具有配置文件的 Java Project 项目(非 web 项目)在服务器上的一个便捷部署
- 关于DataGridView的数据源绑定字符串两个值得注意的问题
- 关于java内部类的继承的一个小问题
- 获得jboss数据源 <use-java-context>false</use-java-context>的问题
- Javaweb-------mybatis+springmvc关于实体类使用注解的一个问题
- 【Java\杂谈】关于Eclipse导入Java项目后Tomcat的配置问题
- 关于《Orange'S》第二章bochs配置文件的一个小问题
- Java 关于获取webapp路径问题
- java中使用堆外内存,关于内存回收需要注意的事和没有解决的遗留问题(等大神解答)
- 关于DataGridView的数据源两个值得注意的小问题