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

5k views Asked by At

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"...

2

There are 2 answers

0
Enrique San Martín On BEST ANSWER

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");
    
9
richj On

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.