You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Fernando Padilla <fe...@alum.mit.edu> on 2008/10/23 02:28:12 UTC

should EntityManager.refresh require active transaction? or not?

I am trying to use EM.refresh outside of a transaction.  But OpenJPA 
throws an exception, saying that it wants it to be within a transaction. 
  Any reason for this??


<openjpa-1.2.0-r422266:683325 nonfatal user error> 
org.apache.openjpa.persistence.TransactionRequiredException: To perform 
this operation, it must be written within a transaction, or your 
settings must allow nontransactional writes and must not detach all 
nontransactional reads.
	at 
org.apache.openjpa.kernel.BrokerImpl.assertWriteOperation(BrokerImpl.java:4400)
	at 
org.apache.openjpa.kernel.DelegatingBroker.assertWriteOperation(DelegatingBroker.java:1346)
	at 
org.apache.openjpa.persistence.EntityManagerImpl.refresh(EntityManagerImpl.java:687)

Re: should EntityManager.refresh require active transaction? or not?

Posted by Fernando Padilla <fe...@alum.mit.edu>.
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!!

fernando




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 <fe...@alum.mit.edu>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"/>
>>>
>>>
>>>
>>>
> 

Re: should EntityManager.refresh require active transaction? or not?

Posted by Kevin Sutter <kw...@gmail.com>.
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 <fe...@alum.mit.edu>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"/>
>>
>>
>>
>>

Re: should EntityManager.refresh require active transaction? or not?

Posted by Fernando Padilla <fe...@alum.mit.edu>.
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"/>
> 
> 
> 

Re: should EntityManager.refresh require active transaction? or not?

Posted by Pinaki Poddar <pp...@apache.org>.
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 this message in context: http://n2.nabble.com/should-EntityManager.refresh-require-active-transaction--or-not--tp1366353p1366848.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.