The Internet is a great resource, but it is sometimes too much to wade through hundreds of web pages looking for tips about using Tomcat with a database. So we at Monitis have done it for you! Below are some of the more useful tips found in various places on the Internet.
Tip #1 – Don’t Ignore Database Performance
Webapps typically use databases. So, you have to tune the webapp along with the database if you want optimal performance. Take our advice. Performance test the database and define an appropriate database connection pool (maxActive, maxIdle, and maxWait in the Resource element).
Tip #2 – Determine the Cause of Database Connection Timeouts
Simply increasing maxWait in the resource element may not be the best way to deal with database connection timeouts. The timeouts may be the result of garbage collection.
Garbage collection requires that all running processes in the Java virtual machine be temporarily suspended. If garbage collection takes too long, the database connections will time out. The timeout here is not because of some problem in the application. It is simply because garbage collection is taking too long.
The more frequently garbage is collected, the less garbage there will be to collect each time. Configure garbage collection so it does not take more than one second.
Tip #3 – Share Database Connections
If every application opens and closes its own database connections, all applications run slower. Instead, if applications share connections by returning them to a common pool rather than closing them, all applications run faster.
Tomcat can be configured for this type of connection pooling. Simply set maxActive and maxIdle to appropriate values in the resource element.
Tip #4 – Return Database Connections to the Pool Properly
Do not close database connections twice. When using connection pooling, the first time you close you return the connection to the pool, and the second time you close, you could inadvertently close the connection after another application has started using it. If you are experiencing “Connection Closed” exceptions, this may be the cause. So, don’t do that!
Tip #5 – Configure Your Database as a Global Naming Resource or For a Single Context
Identify the Java Database Connectivity (JDBC) database in a resource element in the context.xml file if you want the resource to be available to one application only. Identify the JDBC resource in a resource element in the server.xml file if you want the resource to be available to all applications.
If multiple applications need access to a database, the database resource can be created globally in the server.xml file or each application can create its own resource in its own context.xml file. You need to decide which is best.
Making databases available to one application only (in context.xml) creates the resource when the application starts. Making databases available globally (in server.xml) creates the resource when the server starts, which means restarting the server every time you make changes to the resource configuration.
Configuring the database resource in the context.xml file once for each application is recommended because it makes the application more portable.
If different applications have different access levels and those access levels are determined by the database user id, then the database resource must be configured in each application’s context.xml file.
Tip #6 – Do Not Use the JDBC-ODBC Bridge
The JDBC-ODBC (Open Database Connectivity) Bridge bundled with Tomcat’s Java Development Kit (JDK) was never intended for a production environment. Use a third-party driver instead.
Using Tomcat with a database is easy. All you have to know are some good tips to keep your database performing trouble-free. Monitis is happy to provide you with this knowledge and advice. Read more about Tomcat here!
More about Apache Tomcat performance optimization tips here.