• 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

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

 
Ranch Hand
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam, thanks for your reply. I will do some research on ThreadLocal variables and be back if I have questions.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Srikanth Madasu
Ranch Hand
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What server are you using? Connection pools are usually configured as container controlled resources.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've used connection pool and its working like a champ!!!

thanks guys.. you are such a wonderful people... thank you so much..
 
reply
    Bookmark Topic Watch Topic
  • New Topic