It's not a secret anymore!*
The moose likes Tomcat and the fly likes How to initialize connection pool withou restarting Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "How to initialize connection pool withou restarting Tomcat" Watch "How to initialize connection pool withou restarting Tomcat" New topic
Author

How to initialize connection pool withou restarting Tomcat

Tomas Sara
Greenhorn

Joined: Dec 16, 2008
Posts: 5
Hi everybody,

I'm using Tomcat 5.5, Spring framework and HSQLDB database. My goal is to install application on new computer to ($CATALINA_HOME/webapps) and after running it I would like the install page to show. On thi page, user would be able to set path to the database. This information would go into properties file as well as username and password for the database. After leaving the install page I would like the application to work normally withou restarting Tomcat.

Does anybody have an idea how to do it. I need to initialize connection pool without restarting Tomcat? Any help would be very appritiated.

Thanks, Tom.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

I don't know if you can.
Tomcat's integral connection pool is apparently DBCP registered with JNDI. That would be fairly easy to replicate in your own code and you'd have complete control over it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tomas Sara
Greenhorn

Joined: Dec 16, 2008
Posts: 5
Thanks a lot Joe,

I tried to do it using DBCP page and I made my own implementation of DataSource. After this I tried to use it in Tomcat but I was not successful at this.

As I wrote at the begining we are using Spring so I put into the context.xml file the following


Then I copied jar file with my class to "$CATALINA_HOME/commons/lib" directory so Tomcat should be now able to use it. After redeploying the project I got java.sql.SQLException: No suitable driver exception.

Does anybody have any idea how to solve my problem??

Thanks in advance,
Tom.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Originally posted by Tomas Sara:
and I made my own implementation of DataSource.

Why would you do such a thing if DBCP has one? I don't know how to integrate it with Spring, but using DBCP should be this simple:

DBCP Package Summary

Originally posted by Tomas Sara:
Then I copied jar file with my class to "$CATALINA_HOME/commons/lib" directory so Tomcat should be now able to use it.


Since you are creating a connection pool in your application for your application's own use, Tomcat doesn't need to know about it. And if you don't want to restart Tomcat, it shouldn't know about your pool.
Furthermore, if you are getting an "java.sql.SQLException: No suitable driver" exception, the problem is that the JVM can't find your database driver. That is probably the root of your problems. Where do you have the HSQLDB jar file?
Tomas Sara
Greenhorn

Joined: Dec 16, 2008
Posts: 5
Thanks for your quick answer.

When I was writing about the new implementation I did the same you suggested.

After making the class you should make jar file of it and copy it to Tomcat. Otherwise Tomcat can't use your class to make the pool.

I have HSQLDB on the local harddisk in the directory I have full access to. I'm using connection string like this jdbc:hsqldb:hsql://localhost/dbName.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Originally posted by Tomas Sara:

After making the class you should make jar file of it and copy it to Tomcat. Otherwise Tomcat can't use your class to make the pool.

What I'm saying is that you don't want Tomcat to manage your pool at all. If you do that, you'll have to restart it after configuring it.
If you create the pool yourself in your application, you have full reign over it.

Originally posted by Tomas Sara:

I have HSQLDB on the local harddisk in the directory I have full access to.

Where is the driver JAR file? It has to be somewhere your application classloader can access it, usually WEB-INF/lib
Tomas Sara
Greenhorn

Joined: Dec 16, 2008
Posts: 5
What I'm saying is that you don't want Tomcat to manage your pool at all. If you do that, you'll have to restart it after configuring it.
If you create the pool yourself in your application, you have full reign over it.


Thanks a lot for this. I will try to do it. It looks like you understand it a lot. Could you push me to the right direction how to do it?

Where is the driver JAR file? It has to be somewhere your application classloader can access it, usually WEB-INF/lib.


I have the driver exactly where you wrote - WEB-INF/lib.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Originally posted by Tomas Sara:

Could you push me to the right direction how to do it?


I gave you the code on the 18th. The only other detail left is registering the pool instance with JNDI or putting it in a utility class so you can access it anywhere in your program.

Originally posted by Tomas Sara:

I have the driver exactly where you wrote - WEB-INF/lib.


Do you register the HSQLDB driver in your application code as detailed in the documentation (Example 1.1)?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to initialize connection pool withou restarting Tomcat