openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fernando Padilla <>
Subject Re: should EntityManager.refresh require active transaction? or not?
Date Thu, 23 Oct 2008 19:11:59 GMT
hmm, maybe I don't know enough, I'm just getting confused.  I'm using 
Spring JpaTemplate, calling JpaTemplate.refresh().

And I have an active EntityManager, but no I have not opened a 
transaction.  I have NonTransactionalRead=true, but 
NonTransactionWrite=false.  I assumed that when the EntityManager is 
within a transaction, it would tie the refresh to be within a 
transaction, yet outside a transaction it would just work.. just like 
jdo, and kodo, etc..

I can't quite understand what you just wrote ( too many persistence 
context transaction context transaction-scoped, etc ), can you explain 
it to me a little more?

why would I label an EntityManager to require a Transaction, but yet 
never open one myself?  What is "transaction-scoped persistence 
context"?  How would you manage that? etc etc..

thank you!!


Kevin Sutter wrote:
> The JPA spec is pretty clear on the requirements.  First from the javadoc
> for the EM.refresh() method:
> /**
> * Refresh the state of the instance from the database,
> * overwriting changes made to the entity, if any.
> * @param entity
> * @throws IllegalArgumentException if not an entity
> * or entity is not managed
> ** @throws TransactionRequiredException if invoked on a*
> ** container-managed entity manager of type*
> ** PersistenceContextType.TRANSACTION and there is*
> ** no transaction.*
> * @throws EntityNotFoundException if the entity no longer
> * exists in the database
> */
> public void refresh(Object entity);
> And, then in the same section, the following explanation on when
> transactions are required or not:
> The persist, merge, remove, and *refresh* methods must be invoked within a
> transaction context when an entity manager with a transaction-scoped
> persistence context is used. If there is no transaction context, the
> javax.persistence.TransactionRequiredException is thrown.
> So, it would seem that if you are using EM.refresh() outside a
> container-managed EM with transaction-scoped persistence context, then you
> should not require a Transaction.  And, if you are getting an exception,
> then that sounds like a bug.  Unless I am not understanding your scenario...
> Kevin
> On Thu, Oct 23, 2008 at 12:17 PM, Fernando Padilla <>wrote:
>> oh.  that's interesting.  A slight difference in point of view.  I don't
>> see those "changes" as changes that are to be tracked within a transaction,
>> since those changes are not to be applied to the database ever.. they are
>> just refreshing your view of the authoritative version in the db..
>> do you think your decision is a technical one, or just a philosophical one?
>> Pinaki Poddar wrote:
>>> as refresh() may result into overwriting the state of the persistence
>>> instance, if invoked outside a transaction then following must be set
>>>   <property name="openjpa.NontransactionalWrite" value="true"/>

View raw message