• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DataSource InitialContext vs Class.for

 
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is based on Wildfly 10.1 Standalone and Derby Remote client. I assume the same will be true for other environments.

I have deployed derbyclient.jar to the deployments folder and registered a DataSource in Wildfly named 'SimpleWebProjectDS', a JNDI-Name of 'java:jboss/SimpleWebProjectDS' and associated this with the deployed Derby driver. The server URL is set to 'jdbc:derby://localhost:1527/SimpleWebProjectDB;create=true'.

With Derby running on port 1527, the following causes a ClassNotFoundException:



The following executes without throwing an exception and produces the expected functionality:



Why does the first solution cause a ClassNotFoundError?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, can you show us how you configured JNDI?
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In standalone.xml:

 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although in the configuration posted I've used a database named Employee rather than SimpleWebProject, which would lead to a revised connection URL from that given in the first post.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, the difference is that with JNDI, you specified the name of the driver JAR, so JNDI knows where to find the class. When you use Class.forName(), it will only work if derbyclient.jar is on your class path, which you probably forgot.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, so I still need to include it on the classpath even if deployed to an App Server?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on whether the application server has that particular driver on its class path.
 
reply
    Bookmark Topic Watch Topic
  • New Topic