Sun Microsystems 820434310 manual JDBC and Database Access, Use JDBC Directly, Close Connections

Models: 820434310

1 128
Download 128 pages 34.03 Kb
Page 46
Image 46
JDBC and Database Access

EJB Performance Tuning

Pre-fetching generally improves performance because it reduces the number of database accesses. However, if the business logic often uses Orders without referencing their OrderLines, then this can have a performance penalty, that is, the system has spent the effort to pre-fetch the OrderLines that are not actually needed.

Avoid pre-fetching for specific finder methods; this can often avoid that penalty. For example, consider an order bean has two finder methods: a findByPrimaryKey method that uses the orderlines, and a findByCustomerId method that returns only order information and hence doesn’t use the orderlines. If you’ve enabled CMR pre-fetching for the orderlines, both finder methods will pre-fetch the orderlines. However, you can prevent pre-fetching for the findByCustomerId method by including this information in the sun-ejb-jar.xml descriptor:

<ejb> <ejb-name>OrderBean</ejb-name>

...

<cmp> <prefetch-disabled>

<query-method> <method-name>findByCustomerId</method-name>

</query-method> </prefetch-disabled>

</cmp>

</ejb>

JDBC and Database Access

Here are some tips to improve the performance of database access.

Use JDBC Directly

When dealing with large amounts of data, such as searching a large database, use JDBC directly rather than using Entity EJB components.

Encapsulate Business Logic in Entity EJB Components

Combine business logic with the Entity EJB component that holds the data needed for that logic to process.

Close Connections

To ensure that connections are returned to the pool, always close the connections after use.

Minimize the Database Transaction Isolation Level

Use the default isolation level provided by the JDBC driver rather than calling setTransactionIsolationLevel(), unless you are certain that your application behaves correctly and performs better at a different isolation level.

46

Sun GlassFish Enterprise Server 2.1 Performance Tuning Guide • January 2009

Page 46
Image 46
Sun Microsystems 820434310 manual JDBC and Database Access, Use JDBC Directly, Close Connections