hibernate - Spring with JPA - fail if no transactional context defined -
i want require every jpa call occurs inside @transactional context , if forgot annotation, jpa should throw exception instead of creating implicit transaction each call. how can achieve that?
there's part of question easier answer "jpa should throw exception instead of creating implicit transaction each call". you're aware of transaction propagation levels
mandatory support current transaction, throw exception if none exists. nested execute within nested transaction if current transaction exists, behave propagation_required else. never execute non-transactionally, throw exception if transaction exists. not_supported execute non-transactionally, suspend current transaction if 1 exists. required support current transaction, create new 1 if none exists. requires_new create new transaction, suspend current transaction if 1 exists. supports support current transaction, execute non-transactionally if none exists.
required default, , semantic search fits mandatory. configurable using @transactional(propagation = propagation.mandatory)
on class level, beans exhibit behaviour (dao layer beans being usual suspects, should not transaction owners anyway, rather execute in larger context).
the tricky part answer how enforce when omit @transactional. omitting annotation doesn't guarantee anything, transaction semantic added via aop. or class doesn't manage transactions @ all.
i advise keep required default , fine tune propagation level find fitting declaring @transactional proper propagation level.
however, try answer ommitting bit, can change default propagation level globally, in spring configuration, like
<tx:advice id="txadvice"> <tx:attributes> <tx:method name="*" propagation="mandatory"/> </tx:attributes> </tx:advice>
and doing flip coin, , exception methods didn't mark @transactional(propagation = propagation.required);
Comments
Post a Comment