This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
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???
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.
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.
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.
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.
@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.
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.