Issue Details (XML | Word | Printable)

Key: OPENJPA-161
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Assignee: Unassigned
Reporter: Patrick Linskey
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
OpenJPA

Overuse of synchronization in AbstractBrokerFactory

Created: 22/Feb/07 09:57 PM   Updated: 01/Mar/07 02:13 AM
Return to search
Component/s: kernel
Affects Version/s: None
Fix Version/s: 0.9.7

Time Tracking:
Not Specified

File Attachments:
  Size
Text File openjpa-161-patch.txt 2007-02-23 04:27 PM Patrick Linskey 6 kB
Text File Licensed for inclusion in ASF works openjpa-161-patch.txt 2007-02-22 10:08 PM Patrick Linskey 6 kB

Resolution Date: 23/Feb/07 06:34 PM


 Description  « Hide
AbstractBrokerFactory maintains a Map<Transaction,List<Broker>> that is guarded by synchronized blocks. These synchronized blocks should be removed if possible.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Patrick Linskey added a comment - 22/Feb/07 10:08 PM
This patch removes the synchronization on _transactional, replacing it with a ConcurrentHashMap and using the ConcurrentHashMap.putIfAbsent() call to safely handle concurrent access. My analysis is that we do not need to synchronize on the lists inside _transactional, since the key that we look up the collection with is a JTA transaction, and JTA does not allow a transaction to be used concurrently in multiple threads.

Barring any objections, I plan to commit this change in the next day or so.

Abe White added a comment - 23/Feb/07 03:49 PM
I think the FIXME part of the patch should be removed/simplified. The patch itself contains comments like the following:

// we don't need to synchronize on brokers since one JTA transaction
// can never be active on multiple concurrent threads.

Yet the FIXME part explicitly guards against having the same transaction on concurrent threads. If guards like this are necessary, then they need to be consistent throughout the patch. But if we agree that the comment above is correct and the same transaction can't be active on multiple threads, then the FIXME part is useless.

Patrick Linskey added a comment - 23/Feb/07 03:58 PM
Oops, FIXME is stale -- the comment is no longer applicable. Disregard. (In an earlier revision, I was guarding 'brokers' with a synchronized block, but analyzed further and decided that indeed, it was not necessary.)

Abe White added a comment - 23/Feb/07 04:05 PM
The code in question (just after the FIXME) is still guarding against the same transaction in concurrent threads. See the "someone beat us to it" putIfAbsent call. If we agree that transactions are thread bound -- as must be the case for the rest of the patch to be correct -- no one can "beat us to it", because we're the only thread with access to that transaction.

Patrick Linskey added a comment - 23/Feb/07 04:13 PM
Good point -- I guess we can switch to just using a put() call instead. I'll change accordingly.

Patrick Linskey added a comment - 23/Feb/07 04:27 PM
New patch that doesn't use putIfAbsent()