The moose likes Tomcat and the fly likes Connection Pooling Error in Tomcat 7 server.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Connection Pooling Error in Tomcat 7 server.xml" Watch "Connection Pooling Error in Tomcat 7 server.xml" New topic

Connection Pooling Error in Tomcat 7 server.xml

Jay Tai
Ranch Hand

Joined: Apr 25, 2012
Posts: 154

I'm trying to implement connection pooling for a Struts2 application using Tomcat 7.041. Here I'm presenting the two main files of the application which are causing the error.

The connection pooling is implemented in the server.xml I add the <Context> tag below the <Host> (line 44 below of server.xml). Then there is a listener class called AppListener. When trying to run the application I get the following error. I'd really appreciate some advice on where I'm going wrong. Please let me know if I needto provide further information and thanks in advance!

SEVERE: Exception sending context initialized event to listener instance of class app11a.listener.AppListener
at app11a.listener.AppListener.contextInitialized(AppListener.java:21)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

My code is:



Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964

You should NOT insert a Context element into the server.xml file this way. That sort of behavior has been strongly discouraged by the Tomcat people since at least Tomcat 4. Contexts defined within server.xml hold the entire Tomcat system hostage in ways that externally-supplied Contexts do not.

Put your Context into a file named TOMCAT_HOME/conf/Catalina/localhost/app11a.xml instead.

Your stack trace looks incomplete. I would expect to have seen one or more "Caused By" sub-traces below the main traceback. As it is, the message is so vague I can't even guess.

One thing I do wonder about is the way you defined your connection pool. I haven't seen one done in that form for a long time now. Mine look more like this:

Note that it is not recommended for user "root" to be the database user ID for applications (in fact, the PostgreSQL DBMS flatly forbids it). Good practice is that you construct an alternative userid/password combination and grant the necessary rights to that user. Using the root user too freely is a security risk.

Customer surveys are for companies who didn't pay proper attention to begin with.
Jay Tai
Ranch Hand

Joined: Apr 25, 2012
Posts: 154


Thanks a lot for your response. In hindsight I started reading a lot about what a bad idea it was to put Context in server.xml. This came from a book recommendation which also includes definotion of the connection pool. Can I put my context in a context.xml file instead?

On the userid for the database, my current system is in test mode. I will of course change both userid and pw
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964

Your book is probably antique. One of the primary reasons why you should put Context definitions in their own files is that if you change server.xml you have to restart Tomcat. Changing a context file only restarts the web application that goes with that Context.

Your connection pool definition is very likely antique as well. As I said, I haven't seen one like that in a long time.

Context files can be located in the conf/Catalina/localhost directory or inside the WAR itself as a file named META-INF/context.xml. If no context was explicitly defined Tomcat would synthesize one. But it wouldn't have a Connection Pool definition, which is why you need a Context file (in strict JEE terms, this file is known as the server-specific Deployment Descriptor, and WEB-INF/web.xml is the server-independent Deployment Descriptor).

The Tomcat documentation does a fairly good job of describing Context elements and the rules that they operate under, and a very good job of documenting the creation and care of connection pools. Tomcat 7 added a new pooling mechanism, so it's best to read the Tomcat 7 docs on it.
Jay Tai
Ranch Hand

Joined: Apr 25, 2012
Posts: 154

The book is Struts2 Design & Programming by Budi Karniawan - Second Edition. Doesn't say when it was published. First Edition was 2008. I'd agree it's fairly antique. I'll read the Tomcat documentation and adjust my files according to what I learn from those. Thanks again!
I agree. Here's the link: http://aspose.com/file-tools
subject: Connection Pooling Error in Tomcat 7 server.xml
Similar Threads
server.xml doesn't have this element
[Update - Success] Adding SSL certificate to Server.xml
server.xml quirk with startup servlet
Servlet reload Problem
Cannot Connect to database using datasource realm