aspose file tools*
The moose likes Servlets and the fly likes load DB connection class on tomcat startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

load DB connection class on tomcat startup

Eli Daum
Greenhorn

Joined: Jul 29, 2002
Posts: 12
I need a Database Connection Pooling class loaded upon startup of Tomcat so that my app can get get a connection for DB queries?? please help
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you make the relevant methods of your connection pool static, and use 'lazy instantiation' of the pool, you wouldn't need to pre-load the pool. Whichever servlet first calls the "getConnection()" method would experience a short delay while the pool was loaded, and then all other requests from any other servlet would use the existing pool. That would be one way around the problem.

If you don't like the idea of a wait, then you can specify that a servlet be loaded on startup, and in the init() method of the servlet, do a simple call to "getConnection()" to 'prime' the pool, so to speak. This connection would immediately be returned through the "returnConnection()" (or similar) method, but this way, your pool is ready to go for the first "real" request.

Loading a servlet on startup is accomplished through web.xml

Other, more robust connection pools can be configured as a DataSource. This is container-specific, and for Tomcat, there are docs here on configuring two differnet db connection pool packages.
[ June 11, 2003: Message edited by: Mike Curwen ]
Max Haroot
Greenhorn

Joined: Jun 11, 2003
Posts: 3
The ServletContextListener interface containing the methods:
void contextInitialized(ServletContextEvent iEvent)
void contextDestroyed(ServletContextEvent iEvent)
are the prefered way to start processes at startup. This will allow you to set parameters in the web.xml file, such as url, username, password, pool size...
You need only add
<listener>
<listener-class>my.package.name.ConnectionPoolListener</listener-class>
</listener>
to your web.xml file. The container does the rest using introspection to determine which type of listener it is.
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Eli,
It sounds like you are referring to the JNDI emulation that Tomcat provides. You should check the documentation for whatever version you are using, but essentially you are adding a few lines to the server.xml file in $TOMCAT_HOME/conf.
If you are using Tomcat 4.1, this link will take you to the relevant documentation.
Basically, you're adding a datasource to the Tomcat runtime environment, and your application does a JNDI lookup of a name like "env/java/name/of/datasource" to get a handle to the datasource. It then does a getConnection() to pull a Connection object from the pool (Tomcat is now using the Jakarta Commons DBCP package to manage the connection pool).
Hope this helps!


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
Originally posted by Philip Shanks:
Eli,
It sounds like you are referring to the JNDI emulation that Tomcat provides. You should check the documentation for whatever version you are using, but essentially you are adding a few lines to the server.xml file in $TOMCAT_HOME/conf.
If you are using Tomcat 4.1, this link will take you to the relevant documentation.
Basically, you're adding a datasource to the Tomcat runtime environment, and your application does a JNDI lookup of a name like "env/java/name/of/datasource" to get a handle to the datasource. It then does a getConnection() to pull a Connection object from the pool (Tomcat is now using the Jakarta Commons DBCP package to manage the connection pool).
Hope this helps!

Hi
Here is my Working Environment:
Windows 2000 Professional
J2SDK 1.4
Tomcat 4.0.6
mysql server version: 4.1.0-alpha ( on linux server)
JDBCdriver mysql-connector-java-3.0.8-stable-bin.jar
I am trying to implement connection pooling as described in
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example
After modifying server.xml, web.xml and creating connection class and jsp, I start Tomcat through the short-cut which executes startup.bat file. This pops-up tomcat console with all error messages and when I execute shutdown.bat file this window doesn't even close.
Could you successfully implement connection pooling as described in above link, if so I would like to know what I am missing?
Thank you in advance.
Rao Manekar
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If anyone wants to reply, please post it over in the Tomcat forum...

http://www.coderanch.com/t/83329/Tomcat/connection-pooling-Tomcat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: load DB connection class on tomcat startup
 
Similar Threads
i am using orcale, mysql databases , if i use context.xml i can get datasource for only one db
Connection creation @ server startup
Driver Loading
Database Connection
database connections