Search code examples
jbossdatasourceshared-resource

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


Solution

  • 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");