Dimitar Nenchev Blog

Icon

Dimitar Nenchev Blog

Performaces TIPS

Some J2EE Performance Tips

You need to plan for performance and scalability through out your application development process from architecture to implementation to testing. Like security this is not something that can easily be stuck on at the last minute. During design, coding and testing you need to pay attention to resource consumption:

  • Network: watch out for network traffic. Design coarse-grained services with Session or MDB Facades and always use local interfaces for other EJBs (see the Session Facade and DTO design patterns below).
  • Database: use connection pooling and statement caching. Make sure your queries are efficient. See JDBC tips below.

The following J2EE design patterns can improve performance:

  • Data Transfer Object to encapsulate and pass a business object’s data attributes to the presentation tier. Reduces remote network traffic and helps to keep a clean separation between JSPs and EJBs.
  • Service Locator to cache results of JNDI lookups
  • Value List Handler for querying, filtering, large amounts of data
  • Data Access Object to provide a simplified interface for performing complex JDBC operations and to encapsulate data-access logic. Allows decoupling of data-access optimizations
  • Session Facade:
    • To group calls to EJBs in a Coarse Grained Interface: to execute complex use cases in a single network call.
    • Use local interfaces for entity beans and any session EJBs that will be collocated within the same VM. If EJBs need to be called remotely wrap them with the session facade design pattern to combine methods in order to reduce remote invocations.
    • To group related entity or DB updates into 1 container managed transaction

For more on J2EE Patterns see Core J2EE Patterns and J2EE Blueprints

Servlet tips: Don’t store a lot of data in the HTTP Session because memory consumption affects performance and scalability. Remove servlet sessions when they are no longer needed: call session.invalidate()

Session EJB tips: Again because memory consumption affects performance and scalability remove stateful session beans when they are no longer needed (for example when a user logs out) by calling Bean.remove(). Limit the size of objects stored in session beans due to memory cost and performance cost for  I/O  activation and passivation. For high scalability try to use Stateless session EJBs. You can sometimes convert Stateful EJBs to Stateless by adding parameters containing state information to the Stateless bean methods.

Entity Container Managed Persistence: Use CMP Entity Beans for Object oriented transactional persistence. Tuned CMP entity beans can usually offer better performance then BMP entity beans (for large queries, tabular type access for browsing data use the DAO pattern) With EJB 2.0 CMP optimization is possible because persistent fields are only accessible via get and set methods. Containers can optimize based on whether or not a field has been modified, the entity is in a transaction, relation, cache.. CMP optimizations include:

  • Lazy Loading: Deferring loading of any data until it is accessed
  • Dirty Writes:Only updating data which has been changed in the database

For more information on CMP performance see: Why choose CMP?

Consistency Levels in CMP: Consistency levels are tunable in the EJB container, which can improve performance depending on the application. There are two general approaches to ensure consistency. The first is pessimistic locking, which serializes access to an Entity EJB during a transaction and may lead to decreased concurrency. The second approach is optimistic locking, and may result in a larger number of database round trips since it requires a caller to re-try if a consistency check fails at store. Optimistic locking gives better performance when entity contention is not high.

Database Isolation Modes a lower database transaction isolation levels reduces work in the database tier, and can lead to better application performance. Choose the lowest cost database transaction isolation level that avoids corrupting the application data. Use a READ_COMMITTED isolation level with optimistic locking if there is a low likelihood of concurrent transactions updating the same rows. Updates will only fail if there has been a collision.

Getting the most out of your Application Server:
You need to set a sufficient size for the JVM’s heap, Data Base connection pools, and EJB pools and caches. Failure to provide enough resources results in contention and degrades application performance. However too high resource settings can degrade performance as well by using too much memory, which will cause more frequent and longer full Garbage Collection cycles.

Tuning the caching and pooling of EJBs and JDBC can have a significant affect on performance.

Stateless Session beans, Message Driven Beans and Entity beans are pooled to improve server performance. Pooled EJB instances are all equivalent and indistinguishable from other pooled instances of the same class type. Pooling beans reduces the overhead associated with creating and initializing bean instances. You want to monitor and tune the pool to avoid excessive creation or deletion of instances, but also avoid accumulating unused instances. Increase the bean’s pool size when observing excessive creation and deletion of bean instances. Decrease the bean’s pool size when accumulating a large number of instances in the pool, as this will cause more frequent and longer full Garbage Collection cycles.

For example the Sun Java Application Server has the following EJB Container PoolTunables:

  • steady-pool-size (not for message-driven)
  • max-pool-size (0 = unbounded)
  • pool-idle-timeout-in-seconds
  • (0 = indefinitely)

These can be set per bean type and/or as global defaults.

Stateful Session beans and Entity beans are cached to improve performance. Cached Stateful Session EJBs are associated with a specific user’s conversational state. Cached Entity EJBs are associated with a specific primary key. Caching beans gives better performance by reducing activations and passivations, especially since the data associated with an instance must be re-loaded for activation. You want to monitor and tune the cache to minimize the number of activations and passivations, but avoid the accumulation of unused instances in the cache. For Entity beans increase the cache size for beans with concurrent or iterative access patterns. For Stateful session beans set the cache size to the maximum estimated  ~ number of concurrent users. In general Increase the cache until a good cache hit rate is reached.

Entity Bean Commit options and the cache.
In the EJB spec there are 3 commit options for entity beans:

  • Commit Option A: At the end of the transaction, the instance stays in the ready state (cached) and the instance state is valid (ejbLoad not called)
  • Commit Option B: At the end of the transaction, the instance stays in the ready state (cached) but the instance state is NOT valid (ejbLoad will be called)
  • Commit Option C: At the end of the transaction, neither the instance nor its state is valid (instance will be passivated and returned to the pool)

Comit option B performs the best if the Entity bean will be accessed again. If the Entity bean is rarely reused then commit option C is better.  Do profiling with your application on your application server to determine what works best.

For example the Sun Java Application Server has the following EJB Container Cache Tunables:

  • commit-option (B|C) (entity beans)
  • Max-cache-size (0 = unbounded)
  • cache-idle-timeout-in-seconds
  • (0 = indefinitely)

These can be set per bean type and/or as global defaults.

For more on tuning the EJB pool and cache see:
http://docs.sun.com/source/817-2180-10/pt_chap3.html#wp57083

JDBC tips:

  • Test and Select a good JDBC driver for your application
  • Tune the connection pool size
  • Obtain a connection as close to the resource operation as possible and close as soon as completed in order to return the connection to the pool
    • Close JDBC connections in the finally block
  • Index the columns in your WHERE clauses
  • Find out what is happening with your SQL code: use your database engine’s command-line utility and run the SQL through the EXPLAIN SELECT command (or whatever your vendor provides to analyze queries)
    • Is the query utilizing indexes fully?
    • Avoid n-way joins
    • Avoid bringing back thousands of rows of data
  • Turn off Auto-Commit, Group updates into a transaction and/or batch updates
  • To improve performance of database queries, use PreparedStatements and JDBC statement caching, this saves on SQL parsing

For example with the Sun Java Application Server you can tune JDBC statement caching as follows:
<jdbc-connection-pool datasource-classname= “oracle.jdbc.pool.OracleDataSource” …>
<property name=”ImplicitCachingEnabled”  value=”true”/>
<property name=”MaxStatements” value=”200″/>
</jdbc-connection-pool>

For more information on JDBC performance see
http://docs.sun.com/source/817-2180-10/pt_chap4.html#wp65523
http://www.onjava.com/lpt/a/1370

JMS tips:
Use JMS for

  • Asynchronous Concurrent processing
  • Batch processing
  • Scalability
  • Broadcasting messages to multiple recipients
  • Reliable messaging
  • Loose Coupling

XML and Messaging:
Use XML mainly for messages between systems, inside your Application don’t overuse XML because parsing and processing  can cause a performance hit.
for more JMS tips see:
http://www.precisejava.com/javaperf/j2ee/JMS.htm

Filed under: Uncategorized

Диета за орелефяване

Ден 1

Закуска:
-100 гр. препечен пълнозърнест хляб.
-4 белтъка + 1 жълтък
-чаша фреш от грейпфрут (или цял плод)
10 часа - 50 гр. ядки (по възможност сурови)
Обяд:
-200 гр. ориз (варен)
-300 гр. зеленчуци
-150 гр. печено (варено) пиле
16 часа - 50 гр. ядки (по възможност сурови)
Вечеря:
-салата (голяма)
-150 гр. месо

Ден 2

Закуска:
-порция макарони
-100 гр. сирене
-чаша фреш от грейпфрут (или цял плод)
10 часа – кофичка нискомаслено кисело мляко
Обяд:
- 200 гр. пиле (печено или варено)
- 200 гр. зеленчуци
- 1 препечена филийка пълнозърнест хляб
16 часа – кофичка кисело мляко
Вечеря:
-салата  зеле с лимон
-200 гр. печена риба

Ден 3

Закуска:
-100 гр. препечен пълнозърнест хляб
-50 гр. кашкавал
-чаша фреш от грейпфрут (или цял плод)
-1 яйце
10 часа – плод (банан, ябълка, цитрус)
Обяд:
-200 гр. картофи (печени или варени)
-200 гр. месо ( без колбас)
-чаша фреш от грейпфрут (или цял плод)
16 часа -50 гр. ядки (по възможност сурови)
Вечеря:
-салата
-купичка кисело мляко

Ден 4

Закуска:
-500 гр. прясно мляко с овесени ядки (мюсли)
-чаша фреш от грейпфрут (или цял плод)
10 часа – цитрусов плод
Обяд:
-100 гр. кашкавал
-200 гр. ориз със зеленчуци
16 часа – 50 гр. ядки (по възможност сурови)
Вечеря:
- салата зеле, краставици, домати с лимон, без сол (на корем)

Ден 5

Закуска:
- 50 гр. препечен пълнозърнест хляб
- 5 белтъка + 1 жълтък
- чаша фреш от грейпфрут (или цял плод)
10 часа – кисело мляко
Обяд:
- 200 гр. пиле (печено или варено)
- 200 гр. картофи ( печени или варени)
16 часа – чаша фреш от грейпфрут (или цял плод)
Вечеря:
-салата с домати и краставици
-150 гр. месо

Ден 6

Закуска:
- купичка кисело мляко с овесени ядки (мюсли)
- чаша фреш от грейпфрут (или цял плод)
10 часа – 1 банан
Обяд:
- 200 гр. печено или варено месо
- салата от зеле и моркови
- 2 варени картофа
16 часа – 50 гр. ядки (по възможност сурови)
Вечеря:
-200 гр. риба
- зелена салата с лимон

Ден 7

Закуска:
- 100 гр. макарони на фурна
- 100 гр. обезмаслено сирене
- чаша фреш от грейпфрут (или цял плод)
10 часа – 50 гр. ядки (по възможност сурови)
Обяд:
- 200 гр. пиле
- салата с домати и краставици
- 2 препечени филийки пълнозърнест хляб
16 часа – плод
Вечеря:
-салата от зеле ,домати , краставици с лимон

Ден 8

Закуска:
- 500 гр. прясно мляко с овесени ядки (мюсли)
- чаша фреш от грейпфрут (или цял плод)
10 часа - плод
Обяд:
- 200 гр. картофи
- чаша фреш от цитрус (или цял плод)
- 200 гр. риба печена (да не е много солена)
16 часа - кисело мляко
Вечеря:
- салата и 200 гр. месо (варено или печено)

Ден 9

Закуска:
-2 препечени филийки пълнозърнест хляб
-чаша фреш от грейпфрут (или цял плод)
-4 белтъка +1 жълтък
10 часа – плод
Обяд:
-200 гр. пиле с гарнитура от картофи
-чаша фреш
16 часа – 50 гр. ядки (по възможност сурови)
Вечеря:
-салата от пресни зеленчуци (краставици, домати, зеле и др.) с лимон

Ден 10
Почивка. Можете да се хранете по ваше усмотрение.

Filed under: Uncategorized

Петте грешки на Господ или има ли нещо общо между всички Golf-аджии ?

Кои са петте най-големи грешки на Господ и има ли нещо общо между шофьорите управляващи Фолксфаген Голф, било то единичка, двойка, тройка, четворка или петица? За начало нека ви споделя една загадка, която наскоро мой познат ми разкри. Попита ме знам ли кои са най-големите пет грешки на Господ. Естествено всички мои предположения бяха отхвърлени. След няколко десетки мои налучквания той склони да ми ги разкрие :) . Какви мислите, че бяха тези големи грешки? Ето ги и тях – Най-големите грешки били че, Господ е създал: 1. Wolkswagen Golf 1 2. Wolkswagen Golf 2 3. Wolkswagen Golf 3 4. Wolkswagen Golf 4 5. Wolkswagen Golf 5 :) . Отначало много ме развеселиха неговите отговори, но те ме накараха да се замисля. В съзнанието ми се бяха запечатали десетки ако не и стотици изцепки на шофьори упавляваши въпросният автомобил, като в повечето от тях те са били предпоствка за ПТП (няма да споменавам конкретни случаи). В градски условия определено могат да избиват комплески ( то кой не може да кара със 70 или 80 км/час, както правят те ), но нормалните хора си шофират разумно а те не. А когато излезнат на магистралата стават най-невзрачния автомобил, който се мъчи да се вмести в движението (както беше едно време трабантчето), но в града са мъже :) :) . Имам чувството, че всички, които карат Голф са или от една майка или поне от един баща правени :) . Движението и поведението им по пътищата е почти съвсем еднакво, като че ли са го наследили от някой роднина. Може да споделите дали вашите виждания и наблюдения съвпадат с моите в коментарите си. Поздрави

Filed under: Uncategorized

Pages

Archives