-1

we have a jar library util that manage some of the logic of connection to db and store the data in memory. 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.

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 ?

Thanks :)

UPDATE:

I specifically want to do:

"2a. Shared resource configuration

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.

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

Well, i'm in the part of "Clarification would be appreciated here"...

Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51

2 Answers2

3

Create a JBoss Datasource

Add a datasource configuration (*-ds.xml file) into your $JBOSS_HOME/sever/<server-name>/deploy directory.

This StackOverflow answer has more details: How to create a DataSource in JBoss application server

The link is for JBoss 5, but I don't think the datasource configuration changed much between 4.2.3 and 5.

Configure the Tomcat Resource Reference

Configure a Tomcat resource reference to point to the JBoss datasource. This configuration will identify the datasource by JNDI name in order to retrieve connections from the JBoss datasource.

Step 1 of the accepted answer to this StackOverflow question has more details: JNDI path Tomcat vs. Jboss

Note that your Resource configuration is defining a new data source, not reusing the JBoss definition.

Look up the Data Source with 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.

Community
  • 1
  • 1
richj
  • 7,499
  • 3
  • 32
  • 50
  • This don't work, because i'm already configure the datasource *-ds.xml, but i have troubles to configure the others files (like $JBOSS_HOME/server/instance/deploy/jboss-web.deployer/server.xml or maybe some context.xml to include equivalent configuration) – Enrique San Martín Nov 13 '14 at 12:46
  • i vote down, because this answer don't works for deploy a datasource that can be see for all the webapps (or maybe in jboss 5 it's possible, but in 4.2.3 i try it and don't work) – Enrique San Martín Nov 13 '14 at 12:56
  • What @richj said should work, *-ds.xml datasources can be accessed through jndi by any app deployed in the server.. make sure your jndi names are correct – mendieta Nov 13 '14 at 15:28
  • @mendieta Well, but the question is another: "how i can configure a datasource to be GLOBAL to ALL apps deployed" (i repeat because i think that it's not understanded), so this answers is not useful to this question... – Enrique San Martín Nov 13 '14 at 16:19
  • what does it mean to be global to all apps? The solution provided by richj means that any webapp deployed in jboss can use the datasource.. Just declare java:/myDsName in each of your apps persistence.xml, and thats it.. If that is not what you need, then I don't understand the question – mendieta Nov 13 '14 at 16:34
  • global to all app it's mean that any app can access to that datasource, that's made easy in tomcat (only have to add the DS to conf/context.xml and voila), but in jboss is more tricky – Enrique San Martín Nov 13 '14 at 17:09
  • @mendieta about your question (what does it mean... to all apps), i'm trying to do the following: "Context versus GlobalNamingResources Please note that although the above instructions place the JNDI declarations in a Context element, it is possible and sometimes desirable to place these declarations in the GlobalNamingResources section of the server configuration file. A resource placed in the GlobalNamingResources section will be shared among the Contexts of the server." from: https://docs.jboss.org/jbossweb/2.1.x/jndi-datasource-examples-howto.html (at the bottom) – Enrique San Martín Nov 13 '14 at 19:23
  • @richj thx for the edit of the answer, now i think that is more usefull, i changed to vote up +1 – Enrique San Martín Nov 14 '14 at 12:51
  • Ok, this answer NOT resolv the question, but add some info to create and manipulate Datasources in JBOSS. See my answer for a solution. – Enrique San Martín Nov 17 '14 at 20:19
0

Ok, this toke me about a week of researching:

  1. Create a *-ds.xml file.

  2. In the datasource definition add the following tag:

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

  1. Then, in the java code we can call it like:

            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");
    
Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51