-
1. Re: Marking transactional boundaries
jamesstrachan Jul 7, 2004 7:46 AM (in response to gareth_mahon)Gareth,
The EJB Specification mandates that (a) ejbLoad() must be called before a business method in a transaction is executed, and that (b) ejbStore() must be called after a business method is executed.
a) is designed to prevent a business method in a transaction being executed on data that is out of synch. with the underlying database.
b) is designed to ensure that changed data is always written back to the underlying database. The container cannot "know" whether the method call changed data, so the safe option is to always write.
You can reduce the number of ejbLoad() calls by changing the transactional attribute for read methods to 'Supports'. This applies both to the Entity Beans and to read methods on the Session Beans.
You can't reduce the number of ejbStore() calls, but you can reduce their effect. If you are using CMP, the CMP layer should work out that nothing has changed and do no work.
If you are using BMP, you can use a dirty flag to identify when data has changed - and avoid the update if nothing has changed. See example below :-boolean dirty; public void ejbLoad() { // Load data dirty = false; } public void changeData( ... ) { // Change data dirty = true; } public void ejbStore() { logger.info( "In ejbStore " + dirty ); if ( dirty ) { doRealStore(); dirty = false; } } private void doRealStore() { // Update data. }
I posted a complete entity bean on this forum about two weeks ago that uses this technique.
James -
2. Re: Marking transactional boundaries
gareth_mahon Jul 7, 2004 9:31 PM (in response to gareth_mahon)James,
Thanks for you tips.
I managed to fix the problem in the following way:
1. My Jboss configuration file was wrong. Commit option A was not being used, commit option B was - I was using an old DTD.
2. I used the tag <sync-on-commit-only>true</sync-on-commit-only> in the JBoss file.
Thanks for your time,
Gareth