J2EE takes the hassle out of distributed programming, but there’s a catch. Although you no longer have to contend with such low-level issues as threading, synchronization, or persistence, you also relinquish control of key architectural considerations such as load balancing and scaling. Leaving these tasks?which strongly impact performance?to the EJB container can lead to performance problems, because ultimately no third-party software can know your application as well as you do.
Performance problems manifest themselves in many ways:
- JSPs take too long to load.
- Search screens take minutes to show results or never return at all.
- The user interface does not respond, and users click furiously but to no discernible effect.
- At peak times, the server fails unexpectedly due to “out of memory” errors, locked database tables, and connection timeouts.
- Your previously reliable nightly batch processes become strangely fragile and take longer to run.
The day-to-day operation of the business is being impacted and the extra hardware you want isn’t going to arrive anytime soon. You need to diagnose the problem and implement the fixes. A few basic techniques?DBMS optimization, caching, threading, fine-grained transaction control, deferred processing, and EJB restructuring?can help you take back control of your application to meet your performance targets.
Design with Performance in Mind
Unless you consider and factor performance into your user interfaces, designs, and development processes up front (including testing early and often), you’ll likely hit problems sooner or later. The key to good performance is to understand your performance goals up front. Understand the number of concurrent users, target response times, usage patterns, and data growth. Know how your application is going to be used: which parts of the system are session-based and which are non-session or batch-based. That way you can structure your architecture to meet your needs.
Focus on What’s Important to the Business
Sometimes the performance aspects of a system are left to the end of the project. Developers just don’t have time to consider response times when speed of delivery and functionality are the pressing goals. Performance bottlenecks must be systematically identified and solved, which is no light task. The later in the development life cycle you address them, the more expensive they become to fix.
Small optimizations (like turning logging down) can yield huge gains, whereas others can have minuscule effects. The key to tackling bottlenecks is to prioritize and focus on the areas of your code that will yield the biggest return for your time investment. Focus on frequently used parts of the code, the parts critical to your business. Gather accurate performance metrics and use repeatable test cases to validate against your performance targets.
Performance Patterns and Techniques
You can scale your system to satisfy an increased load by using vertical and horizontal scaling techniques (software and hardware), but not always. Sometimes you can’t use these techniques because either the budget is tight, time is limited, or your system has inherent architectural limitations. In lieu of adding more hardware, distributing your code, or adding more servers, you can use the following server-side strategies to improve the performance of a single J2EE/EJB application.
Database I/O Optimization
Consolidate your search queries into a single JDBC call using a DataAccessObject (DAO) or FastLaneReader (Marinescu/J2EE Blueprints).
Caching
Transaction Control
Threading
However, for batch processes, the goal is to achieve high throughput so the job finishes as quickly as possible. Parallel processing can aid this. Spawn threads outside the container and divvy up the load into small units of work that are capable of being executed concurrently. Use threads to spread the load. Don’t spawn more threads than the server can handle. Keep the number of client threads to fewer than the containers thread pool limit.
Deferred Processing
EJB Restructuring
Not every business component has to be a bean. Write your core components as plain old Java objects (POJOs). Design your core business logic and domain model independently of your session beans. That way, your core components are mobile, flexible, and reusable.
Database I/O Optimization Implementation?Pure JDBC for Read-only Access
Entity beans provide an abstraction layer between the higher-level business components and the database. When a developer makes a call to a method on a bean, he or she doesn’t see what happens under the covers because it’s transparent. The developer can assemble large objects or data structures using many entity calls. He or she can be oblivious to the expensive low-level database I/O taking place. One way to alleviate this bottleneck is to replace your critical sections with specialized pieces of pure JDBC code that are optimized for the task. DataAccessObjects (DAOs) and FastLaneReaders (Marinescu/J2EE Blueprints) are common techniques for accelerating reads. These patterns give you fine-grained control over your queries for efficient read access. A DAO can be used to consolidate many DBMS requests made by an entity bean (finder + loads) into a single JDBC call. DAOs are useful for supporting search screens or requests over data that span multiple beans and/or database tables.
The following are recommended practices for DAO:
- Use your DAO to query for data directly from the DBMS rather than via your entity beans. Session beans and other components can call your DAO directly.
- Design your DAO so that it can be tailored to return only what you need.
- Design your DAO so that it can limit the number of rows returned.
- Use finally clauses to ensure that all JDBC resources are closed when you’re finished with them. This ensures that you don’t hog or consume connections and cursors.
- Query against DBMS views to make your DAOs more reusable and portable. If you need to make a change to a query, make a change to the view instead.
With a DAO you can take advantage directly of the features provided by the JDBC API, including:
- PreparedStatements to pre-compile frequently executed SQL statements
- Batch methods on the Statement class (addBatch() and exectuteBatch()) to batch up multiple SQL calls into one hit to the database
- Row limiting (The Statement and ResultSet classes provide methods (setMaxRows() and setFetchSize()) to limit the number of rows returned and set hints for ResultSet fetch sizes.)
See Listing 1: A Simple DAO for executing SQL against a view and limiting the number of rows returned in a ResultSet.
As part of your DAO design, you must decide in which form the data should be returned and how it should be converted. In some cases, you may want to return loosely typed collections such as Lists or Maps, and in other cases you may want to immediately convert ResultSet data into strongly typed Java objects relevant to your system. One way to handle this at the DAO level is to use a helper ResultSetConverter interface. This interface is responsible for converting RowSets into strongly typed application object types or collections. The DAO uses it to automatically convert ResultSet data into your target object(s):
public interface ResultSetConverter { public Object toObject(ResultSet rs) throws Exception;}
Create a simple ResultSet to Map Converter class by implementing the ResultSetConverter interface. Inside the toObject() method, pull the data from the result set and place it into a map of column name/value pairs. The map is returned to the DAO to be passed back to the DAO caller:
class MapConverter implements ResultSetConverter {public Object toObject(ResultSet rs) throws Exception { Map map = new HashMap(); ResultSetMetaData meta = rs.getMetaData(); // Load ResultSet into map by column name int numberOfColumns = meta.getColumnCount(); for (int i = 1; i // place into map map.put(name, value); } return map;}}
To use your DAO and converter, acquire a DAO instance and invoke the query() method to execute your SQL. Use the rowLimit parameter to limit the number of rows returned and pass in the converter class for the DAO to use.
DAO dao = DAO.get(); // Create our own converter for getting the first column as a StringResultSetConverter myConverter = new ResultSetConverter() { public Object toObject(ResultSet rs) throws Exception { return rs.getString(1); }};// Execute a query against a VIEW,
limit the number of rows returned to 10 and use myConverter to convert the resultsList data = dao.query("myView", 10, myConverter);// Do something with the data, ship to the JSP etc.
The J2EE Performance-tuning Trade-off
Experienced practitioners know that when addressing J2EE application performance issues, there are no silver bullets. Performance tuning is a trade-off between architecture concerns, such as flexibility and maintainability. Performance increases are won by combining different techniques, patterns, and strategies.
And if all else fails, you can hope that that extra-fast machine you ordered turns up sooner rather than later. 🙂