aspose file tools*
The moose likes JDBC and the fly likes Deploying Commons DBCP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Deploying Commons DBCP " Watch "Deploying Commons DBCP " New topic
Author

Deploying Commons DBCP

Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
Hi all.
I'm new to this, so bear with me!
I have a web application which is for installing on multiple other users' servers (running Tomcat). I want to use connection pooling, but I don't want users to have to set up data source information in their Tomcat configuration files.

So, I was going to use something like this (found on usenet):


However, I'm not sure how best to deploy this. I don't want to create a new datasource everytime I need to connect to a database, do I?
Should I rewrite this class as a servlet, and run it on my webapp's startup, then use the same datasource in all the jsp pages in my application?

How about closing the datasource? Do I need to do that or will it happen automatically when I return all the datasource's connections?

Thanks for help!
M
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30512
    
150

Mikey,
Welcome to JavaRanch!

You could cache the datasource by making it a static variable. That would give you only one for the whole application.

You don't need to close a DataSource. As long as you close the Connection, Statement and ResultSet, all the resources are returned. The DataSource is like a factory for Connections.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
Hi Jeanne,
So make the datasource a static variable, and just instantiate it from the first jsp page of my webapp? No need to make the class a servlet?

Should I create it using a Singleton pattern, or am I getting over-complicated?

This is a newbie Java scope question, rather than a JDBC question, but does that mean that if one user is accessing the webapp, and the datasource is instantiated, then if a second user accesses it, the same datasource instance will be used?



Thanks!
Mike
[ August 10, 2005: Message edited by: Mikey Kelly ]
Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
This is what I tried - seems to be working OK:



then in the jsp pages:



Seems reasonable?
Thanks,
M
[ August 10, 2005: Message edited by: Mikey Kelly ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30512
    
150

Mikey,
Definitely reasonable! And yes, if a second user comes along, he/she will use the same DataSource. This is what you want. The DataSource has multiple connections in the pool and will handle not giving out more than one at a time.

Note that it is considered a best practice to avoid having database code in the JSP. Instead, you have the servlet (or a class that it calls) do the database stuff and put the result in a request attribute for the JSP to retrieve. Just something to keep in mind as you learn more.
Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
Thanks Jeanne.
Just one more clarification.

Should I be using a singleton pattern in DatabaseUtils?

Every time I call DatabaseUtils.getDataSource(), will the getDataSource() function go through the procedure of making a new BasicDataSource, etc. before returning the static DataSource? Or will it only go through the DataSource creation process if the DataSource doesn't exist?

Also, it would be really useful to see any examples of moving database queries out of the JSP page if you know of any.

Thanks for your help!
M
[ August 11, 2005: Message edited by: Mikey Kelly ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30512
    
150

Mikey,
A static variable is like a singleton pattern in that you only have one instance of it. So you could use either approach.

I don't have a good example of moving the code out of the JSP at the moment, but I recommend searching for MVC (model view controller) for some examples.
Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
I made a simple ammendment - in getDataSource() I now check whether the static dataSource variable has already been initialised. If so I return it. Otherwise I initialise it and return it.

Otherwise I presume the dataSource variable would be reset every time i call the getDataSource() function.

Best wishes,
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deploying Commons DBCP