This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts 1.1 database connection exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1.1 database connection exception" Watch "Struts 1.1 database connection exception" New topic
Author

Struts 1.1 database connection exception

Trevar Pearce
Greenhorn

Joined: Jul 13, 2006
Posts: 6
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
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Trevar Pearce
Greenhorn

Joined: Jul 13, 2006
Posts: 6
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

Joined: Jul 13, 2006
Posts: 6
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

Joined: Jul 13, 2006
Posts: 6
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
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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:



Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 1.1 database connection exception
 
Similar Threads
Connecting MySQL thru Struts
Database Pooling in Struts
Hi i am trying to connect to postgresql database using struts 1.2..i have used datasource
logic:iterate pls any body help me
Struts Error