简体   繁体   English

如何为在默认上下文中初始化连接的jar库配置数据源jboss 4.2.3?

[英]How to configure datasource jboss 4.2.3 for a jar library that initialize connection in the default context?

we have a jar library util that manage some of the logic of connection to db and store the data in memory. 我们有一个jar库util,它管理与db的连接的一些逻辑并将数据存储在内存中。 Well, this works fine in tomcat because we can configure the datasource in the $CATALINA_HOME/conf/context.xml and everything work's just fine. 好的,这在tomcat中可以正常工作,因为我们可以在$ CATALINA_HOME / conf / context.xml中配置数据源,并且一切正常。

How i can configure a datasource in jboss (4.2.3.GA) that's can be see by all the war, ear or apps deployed and of course this jar util that's it's deployed in the $JBOSS_HOME/server/< instance >/lib ? 我如何在jboss(4.2.3.GA)中配置数据源,所有部署的战争,耳朵或应用程序都可以看到,当然,这个jar util部署在$ JBOSS_HOME / server / < 实例 > / lib中?

Thanks :) 谢谢 :)

UPDATE: 更新:

I specifically want to do: 我特别想做:

"2a. Shared resource configuration “ 2a。共享资源配置

Use this option if you wish to define a datasource that is shared across multiple JBoss Web applications, or if you just prefer defining your datasource in this file. 如果您希望定义一个在多个JBoss Web应用程序之间共享的数据源,或者仅希望在此文件中定义数据源,请使用此选项。

This author has not had success here, although others have reported so. 尽管没有其他人报道,但该作者在这里没有取得成功。 Clarification would be appreciated here . 澄清在这里将不胜感激

<Resource name="jdbc/postgres" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://127.0.0.1:5432/mydb"
          username="myuser" password="mypasswd" maxActive="20" maxIdle="10" maxWait="-1"/>

source: https://docs.jboss.org/jbossweb/2.1.x/jndi-datasource-examples-howto.html 来源: https : //docs.jboss.org/jbossweb/2.1.x/jndi-datasource-examples-howto.html

Well, i'm in the part of " Clarification would be appreciated here "... 好吧,我参与了“ 澄清,请在这里进行 ” ...

Create a JBoss Datasource 创建一个JBoss数据源

Add a datasource configuration (*-ds.xml file) into your $JBOSS_HOME/sever/< server-name >/deploy directory. 将数据源配置(* -ds.xml文件)添加到$ JBOSS_HOME / sever / < 服务器名称 > / deploy目录中。

This StackOverflow answer has more details: How to create a DataSource in JBoss application server 这个StackOverflow答案有更多详细信息: 如何在JBoss应用程序服务器中创建数据源。

The link is for JBoss 5, but I don't think the datasource configuration changed much between 4.2.3 and 5. 该链接适用于JBoss 5,但我认为4.2.3和5之间的数据源配置没有太大变化。

Configure the Tomcat Resource Reference 配置Tomcat资源参考

Configure a Tomcat resource reference to point to the JBoss datasource. 配置Tomcat资源引用以指向JBoss数据源。 This configuration will identify the datasource by JNDI name in order to retrieve connections from the JBoss datasource. 此配置将通过JNDI名称标识数据源,以便从JBoss数据源检索连接。

Step 1 of the accepted answer to this StackOverflow question has more details: JNDI path Tomcat vs. Jboss 这个StackOverflow问题的可接受答案的步骤1有更多详细信息: JNDI路径Tomcat与Jboss

Note that your Resource configuration is defining a new data source, not reusing the JBoss definition. 请注意,您的Resource配置正在定义新的数据源,而不是重用JBoss定义。

Look up the Data Source with JNDI 使用JNDI查找数据源

The same answer explains how to do this, but note that the URI is slightly different depending on whether the lookup is done from within client code outside of the EJB container, or from code within the EJB container. 相同的答案解释了如何执行此操作,但是请注意,URI是根据是从EJB容器之外的客户端代码中还是从EJB容器中的代码完成查找而略有不同。

Ok, this toke me about a week of researching: 好的,这使我花了大约一个星期的研究时间:

  1. Create a *-ds.xml file. 创建一个* -ds.xml文件。

  2. In the datasource definition add the following tag: 在数据源定义中,添加以下标记:

< use-java-context >false< /use-java-context > < use-java-context > false < / use-java-context >

  1. Then, in the java code we can call it like: 然后,在Java代码中,我们可以这样称呼它:

      Properties env = new Properties(); env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); env.setProperty(Context.PROVIDER_URL, "localhost:1100"); initialContext = new InitialContext(env); DataSource datasource = (DataSource) initialContext.lookup("myCustomDs"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM