java - Difference in rolling back a statement and a transaction in JPA -
jpa query javadoc (see http://docs.oracle.com/javaee/6/api/javax/persistence/query.html#executeupdate()) says
int executeupdate() execute update or delete statement. returns: number of entities updated or deleted throws: illegalstateexception - if called java persistence query language select statement or criteria query transactionrequiredexception - if there no transaction querytimeoutexception - if statement execution exceeds query timeout value set , statement rolled persistenceexception - if query execution exceeds query timeout value set , transaction rolled
what's difference between rolling statemente , transaction? mean, rolling transaction pretty obvious, set transaction rollback , operations undone. if statement rolled (since it's update/delete/insert operation), won't whole transaction rolled in situation?
was querytimeoutexception designed caught , allow user retry on timeout without affecting transaction?
[a querytimeoutexception is] thrown persistence provider when query times out , statement rolled back. current transaction, if 1 active, not marked rollback. [querytimeoutexception]
the querytimeoutexception specialization of persistenceexception.
[a persistenceexception is] thrown persistence provider when problem occurs. instances of persistenceexception except instances of noresultexception, nonuniqueresultexception, locktimeoutexception, , querytimeoutexception cause current transaction, if 1 active , persistence context has been joined it, marked rollback. [persistenceexception]
therefore if query times out don't matter not cause rollback of transaction default. that's why have explicitly. example if want rollback transaction regardless persistenceexception
occurs.
catch(persistenceexception e) { ... tx.rollback(); ... }
but makes sense continue transaction statement not successful , querytimeoutexception
occurs.
an example scenario time out during execution of statement persist additional log record. depending on use case time out of executing log statement not critical otherwise it's critical if core business process example persisting order times out. therefore don't want failed log statement affect persisting of order. on other hand if persisting of order fails persisting of log record should roll backed. can decide query time out should cause rollback.
a schematically example be
... try { ... querynoncritical.execute(...); } catch(querytimeoutexception e) { // not critical move on ... } ... try { ... querycritical.execute(...); } catch(querytimeoutexception e) { ... tx.rollback(); ... } ...
Comments
Post a Comment