Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

load DB connection class on tomcat startup

 
Eli Daum
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Report post to moderator
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!
 
Narsimha Manekar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
If anyone wants to reply, please post it over in the Tomcat forum...

http://www.coderanch.com/t/83329/Tomcat/connection-pooling-Tomcat
 
    Bookmark Topic Watch Topic
  • New Topic