aspose file tools*
The moose likes Struts and the fly likes Can I get the datasource without referencing the actionServlet??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can I get the datasource without referencing the actionServlet???" Watch "Can I get the datasource without referencing the actionServlet???" New topic
Author

Can I get the datasource without referencing the actionServlet???

Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

Hello all, I configured the database in struts-config file, and I could get the datasource in my action class using the following code




I am then passing this datasource to my DAO classes to access the database. I now want to action classes to be unaware of the database, I mean I want all the back end related stuff in a separate package. Now my question is can i get my hands on the datasource configured in the struts-config from my DAO classes directly???

Also i read about the connection pool, but could not get it . could connection pool help me in this regard???


If it's easy.. then everybody would do it!
Sam Wilson
Greenhorn

Joined: Apr 26, 2005
Posts: 1
Do some research on ThreadLocal variables. They are excellent for using when you have what I call pervasive data. It is data which essentially defines the context of some part of your application and should be freely availble to those parts when needed. They (ThreadLocal variables) can be easily abused and should not replace careful OO design. But they were designed to solve this type of problem and I was using my own variation of ThreadLocal before it was a part of the Java language. Identify the lifecylce of the ThreadLocal variable and make sure that you initialize and clean it up at the appropriate points. In your case, it could be at the beginning and end of a specific Struts Action or it could be done for all Actions.

I will be happy to help you with whatever designs that you come up with after learning about ThreadLocal.
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

Sam, thanks for your reply. I will do some research on ThreadLocal variables and be back if I have questions.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

While ThreadLocals will certainly work, I'm generally wary of their use under most circumstances, particularly when better solutions exist.

In your case I'd seriously consider using Spring (or similar) and removing the datasource configuration from Struts--that was deprecated a *long* time ago and isn't considered a particularly good practice.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Since Struts is a Front Controller, it really shouldn't have any knowledge of what's going on in the back end. As you've figured out, configuring your database connection in the front end necessitates passing it around to where it can actually do the work. This is unnecessary. As a matter of fact, datasource has been removed from more recent versions of Struts.
The Struts Databse FAQ suggests having the DAO contact the database so you don't tie your front and back end together. It also suggests you use a connection pool.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

thanks for your replies.. I've done some research on this issue, and as David pointed I am trying to use spring for this issue.

But still I have connection pool in my mind. could anyone tell me how to use a connection pool or atleast tell me where can I find some good stuff about it. I tried googling, ended up with the stuff which i am really unable to understand.

thanks for your time and valuable suggestions!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

What server are you using? Connection pools are usually configured as container controlled resources.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Depends--they can also be configured via Spring.

@Srik: The Spring docs have a fair amount of connection pooling info. You could also try in a database-related forum.
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

@Joe : I am using Tomcat 6.x

@David : the reason I am thinking of connection pooling is I don't to add another framework to my application. But I've read that its very easy to configure spring with struts and use it. And I am planning to use spring if the connection pooling doesn't work or if it requires lot of work.

I will hit back if get stranded

thanks!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

To configure a connection pool in Tomcat, look at the JNDI resources how-to for the general idea then the JDBC how-to for examples of particular database servers.
I haven't used Spring, but there's probably similar documentation available.
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

I've used connection pool and its working like a champ!!!

thanks guys.. you are such a wonderful people... thank you so much..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I get the datasource without referencing the actionServlet???