openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Heath Thomann (JIRA)" <>
Subject [jira] [Resolved] (OPENJPA-1702) UnsupportedOperationException caused in BrokerImpl during transaction commit processing.
Date Wed, 05 Feb 2014 22:36:13 GMT


Heath Thomann resolved OPENJPA-1702.

       Resolution: Fixed
    Fix Version/s: 2.1.0

> UnsupportedOperationException caused in BrokerImpl during transaction commit processing.
> ----------------------------------------------------------------------------------------
>                 Key: OPENJPA-1702
>                 URL:
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: kernel
>    Affects Versions: 1.2.0, 2.0.0
>            Reporter: Heath Thomann
>            Assignee: Heath Thomann
>            Priority: Minor
>             Fix For: 2.1.0
>         Attachments: OPENJPA-1702-TEST.patch.txt
> For a given scenario, which will be described in detail below, an UnsupportedOperationException
occurs as follows:
> [main] openjpa.Runtime - An exception occurred while ending the transaction.  This exception
will be re-thrown.
> <openjpa-1.2.3-SNAPSHOT-r422266:955388M nonfatal store error> org.apache.openjpa.util.StoreException:
> 	at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(
> 	at org.apache.openjpa.kernel.LocalManagedRuntime.commit(
> 	at org.apache.openjpa.kernel.BrokerImpl.commit(
> 	at org.apache.openjpa.kernel.DelegatingBroker.commit(
> 	at org.apache.openjpa.persistence.EntityManagerImpl.commit(
> 	at hat.tests.TestUnsupportedOp.commitTx(
> 	at hat.tests.TestUnsupportedOp.test(
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> 	at java.lang.reflect.Method.invoke(
> 	at junit.framework.TestCase.runTest(
> 	at junit.framework.TestCase.runBare(
> 	at junit.framework.TestResult$1.protect(
> 	at junit.framework.TestResult.runProtected(
> 	at
> 	at
> 	at junit.framework.TestSuite.runTest(
> 	at
> 	at
> 	at
> 	at
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> 	at
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> Caused by: java.lang.UnsupportedOperationException
> 	at java.util.AbstractCollection.add(
> 	at java.util.AbstractCollection.addAll(
> 	at org.apache.openjpa.kernel.BrokerImpl.flushTransAdditions(
> 	at org.apache.openjpa.kernel.BrokerImpl.flushAdditions(
> 	at org.apache.openjpa.kernel.BrokerImpl.flush(
> 	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(
> 	at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(
> 	... 25 more
> Using the stack trace, and some particulars about the code path, I've been able to recreate
the UnsupportedOperationException.  Let me first summarize what my test does, and then let
me go into great details on how the issue occurs.  My test does the following:
> 1) My "main" code simply begins a tran, performs a query, and commits the tran.
> 2) I've created a 'tran listener' (i.e. an impl of org.apache.openjpa.event.TransactionListener)
and in that 'listener', method 'beforeCommit', I dirty the entity queried/found in #1.
> 3) After my 'beforeCommit' method returns, the UnsupportedOperationException is thrown.
> OK, that was the brief summary, for anyone else who cares to hear the gory details, lets
dig in.....first, the exception stack shows the exception is hit here:
> Caused by: java.lang.UnsupportedOperationException
>    at java.util.AbstractCollection.add(
>    at java.util.AbstractCollection.addAll(
>    at
> org.apache.openjpa.kernel.BrokerImpl.flushTransAdditions( 
>    at
> org.apache.openjpa.kernel.BrokerImpl.flushAdditions(
>    at org.apache.openjpa.kernel.BrokerImpl.flush( 
> So, lets look at the code around 'flush('.  To follow is line 2000
(the last line) and a number of lines proceeding it:
>                if ((_transEventManager.hasFlushListeners()
>                     || _transEventManager.hasEndListeners())
>                     && (flush || reason == FLUSH_COMMIT))
>                     // fire events
>                     mobjs = new ManagedObjectCollection(transactional);
>                     if (reason == FLUSH_COMMIT
>                         && _transEventManager.hasEndListeners())
>                         fireTransactionEvent(new TransactionEvent(this,

>                             TransactionEvent.BEFORE_COMMIT,
>                             _persistedClss, _updatedClss,
>                         flushAdditions(transactional, reason);   
<----- line 2000
> So, in order to get to this 'flushAdditions', you must have a 'listener' (i.e. an impl
of org.apache.openjpa.event.TransactionListener).  OK, with that said, keep this 'listener'
idea in mind as we will come back to it.
> Continue to dig into the stack and going up two levels, we see that 'flushTransAdditions('
looks like this:
>     private boolean flushTransAdditions(Collection transactional, int reason) {
>         if (_transAdditions == null || _transAdditions.isEmpty())
>             return false;
>         // keep local transactional list copy up to date
>         transactional.addAll(_transAdditions);   <----- line 2099
> There are two important things to note here:
> 1) 'transactional' is a 'Collection'.
> 2) the addAll will only be called depending on the state of '_transAdditions'.
> For #1, lets visit the javadoc for Collection.addAll and see why/when it throws the UnsupportedOperationException.....its
>     * @throws UnsupportedOperationException if this collection does not
>      *         support the <tt>addAll</tt> method.
> So, we know that the 'Collection' is of a type which must not support addAll.  This
offers a clue and we should look to see at which points 'transactional' could be defined as
a 'Collection' which doesn't support 'addAll'.  'transactional' is set in BrokerImpl at line
1946 which is here:
>         Collection transactional = getTransactionalStates();
> If we look at 'getTransactionalStates()', we can see that the method could return a Collections.EMPTY_SET
>     protected Collection getTransactionalStates() {
>         if (!hasTransactionalObjects())
>             return Collections.EMPTY_SET;
>         return _transCache.copy();
>     }
> An 'EmptySet.addAll' eventually calls 'AbstractCollection.add' which blatantly throws
an UnsupportedOperationException (plus, and Collections.EMPTY_SET is immutable, so we should
be adding to it anyway).  So, we know we must have a case where 'transactional' is an EmtpySet. 
One way this may occur is to only query objects as I've done in step #1 of my test (i.e. I
never dirty anything in step #1).
> Next, #2 offers another clue in that we need to look at the case where '_transAdditions'
is not null and not empty.  If we look in BorkerImpl at the places where '_transAdditions'
is set, we can see things are added to it in the 'setDirty' method.  But, as we previously
found, we are only querying objects, not making them dirty.  So, how can we have 'transactional'
be an EmptySet, yet '_transAdditions' not null or empty?  One way is to go back to the 'listener'
we discussed earlier and when the 'listener' is called, have it dirty an entity.  In so doing,
the 'setDirty' method will be called which will add elements to '_transAdditions' such that
conditions are met to cause 'transactional.addAll' to be called in 'flushTransAdditions'. 
The ordering is basically like this:
> 1) 'transactional' is set to an EmptySet and the beginning of flush.
> 2) The 'listener' is called later on in flush which dirties an entity.  This causes
'_transAdditions' to not be null or empty.
> 3) After the 'listener' is called, flushTransAdditions is called where at which time
'addAll', and then 'add', is called on an EmptySet/AbstractCollection which returns the exception.

This message was sent by Atlassian JIRA

View raw message