• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Struts 1.1 database connection exception

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm using Struts 1.1 and Oracle 10g and attempting to establish a connection between the two. I have successfully done so by hard-coding the datasource info in my action class but am running into a problem when placing that info in the struts-config.xml. Here's the data-source entry:



I call it with this:



I have determined that it is stumbling on the DataSource line, giving me a java.lang.NullPointerException. If you can see what's going wrong with the code, please let me know.

Thanks
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting datasource object from struts-config.xml

There is a method called getDataSource(HttpServletRequest, String) defined in Action
that gets inherited when you extend your class to Action.

call this method and you will get DataSource object...

DataSource dataSource=getDataSource(request, "TESTDB");


Naseem
[ July 13, 2006: Message edited by: Naseem Khan ]
 
Trevar Pearce
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for the reply. I've implemented the code but unfortunately it's still giving me the java.lang.NullPointerException

When I remove the try/catch block from the database access action class, it produces this:



It's thus still the DataSource line that's breaking. Any ideas?
 
Trevar Pearce
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again, so I've narrowed it down a bit. Looks like the getDataSource is calling Action.class and it's stumbling on the following line:



I've commented all my db access code out and just entered that (and imported javax.servlet.ServletContext) and I get the same NullPointerException. Anybody know what's going on here?

Thanks, Trevar
 
Trevar Pearce
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I've in fact more or less solved the problem. The solution confuses me however, so if anyone can help me understand, I would very much appreciate it.

My jsp forwards to an action class which calls a class that does the database access. The database access class returns the result to the action class. The action class populates a bean with the results and sets a request attribute with the bean. Finally it forwards back to the jsp, which displays the results from the bean. As you can tell, it's a tiny web-app at this point, but I believe it is structured properly (if you have different information about where the db access should happen, please let me know, I really want this app to be structured to the standards).

The thing that I got hung up on was accessing the application scope attributes (from the ServletContext) in the database access method. I'll post it here:



As I said, I'm calling this class and its one method rather than forwarding to it. As such, I think it's not being passed the ServletContext unless I do it explicitly(as I do). This is what confuses me. I assumed that because the ServletContext was application scope that it would automatically be available in any class I called in my web-app. I guess that was a poor assumption. Can anyone explain what I'm missing here? There's some bit of theory that hasn't gotten through to me and I'd really appreciate it if someone could explain briefly.

Thanks, Trevar
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trevar,

I realize you've already spent a good deal of time on this, but my recommendation is to go a completely different route. I suggest you abandon the use of the Stuts datasource entirely.

My reasons for recommending this are:

1-It's deprecated in the latest release of Struts
2-It was included in Struts only as a "last resort" if your Application server doesn't have it's own connection pooling mechanism
3-For a long time, the authors of Struts have recommended you use your Application Server's connection pooling mechanism instead of the one in Struts

I'd suggest you look in your application server documentation for information on how to set up an Oracle Connection pool. You can then access the DataSource in any of your classes by doing a JNDI lookup.

Example:

 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic