0

I am porting a suite of related applications from WebLogic to JBoss EAP v6.2.

I have set up a data source connection using the JBoss command line interface and hooked it to an oracle database. This database has a name of "mydatasource" and a JNDI name of "java:jboss/datasources/mydatasource" as per JBoss standards. I can test and validate this database connection.

However, when I try to port the code and run it, the connection doesn't work. The code that worked in WebLogic was simply:

    InitialContext ic = new InitialContext() ;
    DataSource ds = (DataSource)ic.lookup(dataSource) ;

with a value in dataSource of "mydatasource".

This worked in Web Logic but in JBoss it throws a NameNotFoundException

javax.naming.NameNotFoundException: mydatasource-- service jboss.naming.context.java.mydatasource

Clearly there is a difference in how the InitialContext is set up between the two servers.

But this port involves a large number of small applications, all of which connect to the datasource via code like that above. I don't want to rewrite all that code.

Is there a way through configuration (InitialContextFactory, maybe) to define the initial context such that code like that above will work without rewriting, or perhaps is there another way of naming the datasource that JBoss will accept that would allow code like that above to work without rewriting?

Or must we bite the bullet and accept that this code needs a rewrite?

Update: Yes, I know that simply passing "java:jboss/datasources/mydatasource" to the InitialContext lookup solves the problem, but I am looking for a solution via configuration, rather than via coding if there is such a solution.

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • What is the value of `dataSource`? It needs to be `java:jboss/datasources/mydatasource` since that's the name you gave it. On the server start up you should see a list of bound data source names in the logs. – James R. Perkins Aug 19 '14 at 21:26
  • Yes, that is the issue. somehow, in Web Logic, the InitialContext was able to resolve just "mydatasource" without any of the prelimary boilerplate. I'd rather not change the code, but I may have to. Making it be "java:jboss/datasources/mydatasource" solves the problem easily enough, but I was looking for some way to handle this through configuration rather than coding, since I may have to make the same mod in hundreds of places. – Steve Cohen Aug 19 '14 at 21:31

1 Answers1

1

The way to do this correctly through configuration is to use

java:comp/env/jdbc/myDataSource

then use resource-ref in web.xml to map it to the declare datasource and use weblogic.xml or jboss-web.xml to actually map it to the real one

in weblogic admin console, when you define datasource it can be jdbc/realDataSource

JNDI path Tomcat vs. Jboss

For weblogic http://docs.oracle.com/cd/E13222_01/wls/docs103/jdbc_admin/packagedjdbc.html

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • @Kalpesh_Soni: Thanks. I am intrigued by your "JNDI path Tomcat vs. Jboss" link in particular "option 2", but one question I have is that that page doesn't mention what version of JBoss it applies to. Is there anything there that won't work with JBoss EAP 6.2? – Steve Cohen Aug 20 '14 at 14:24
  • I should have mentioned annotations, yes, while migrating old apps, you should use annotations as much as possible and use xml less and less, as much as possible, jboss eap 6+ is java ee 6 certified and @ Resource @ EJB @ WebServlet would work - here is the big picture http://mgreau.com/posts/2013/12/10/jboss-eap-62-51-43-javaee-supported.html – Kalpesh Soni Aug 20 '14 at 15:44
  • different kind of resources can be injected http://docs.oracle.com/javaee/6/tutorial/doc/bncjk.html – Kalpesh Soni Aug 20 '14 at 15:45
  • 1
    @Kalpesh_Soni: You answer was correct, although ultimately we are NOT using annotations since much of the code we are porting allows a dynamic runtime lookup of a variable datasource name, which would not be compatible with annotation. While this code could and probably should be rewritten, that is not in the cards. The part we will use is to use jboss-web.xml to manage the different naming conventions between WebLogic and JBoss, which was the motivation behind my question. Thanks. – Steve Cohen Aug 21 '14 at 20:51