Forcing a method to a transactionless transaction in JPA (Eclipselink)
I am developing an application using Eclipselink and as part of an application I need to manage some objects that are related to data modification without storing them in the database (i merge / modify objects for some package generation processes).
I do not want to change the data in the Entity objects, as there is a risk that although I have not marked the methods as @Transactional
, this method in the future may be called unintentionally called from a transactional method, and these changes may be saved.
So my question is, is there a way to get around this? Such as coercion should always be non-transactional; terminate any transaction immediately after starting the method; and etc.
I know there is a .detach () method that can detach objects from the Entity Manager, however there are many objects and this looks like a potential failover prone bug in my code.
a source to share
If it is spring you have
@Transactional(propagation=Propagation.NOT_SUPPORTED)
Execute a transaction without transactions, suspend the current transaction if it exists. Same as the EJB transaction attribute of the same name.
If it's an EJB, you have a transaction attribute with the same name.
Anyway, instead of playing with transactions, you can simply clone your object and modify the data you need. Deep cloning can be achieved via commons-lang SerializationUtils
.
a source to share
I'm not sure to understand your use case, but I would not interfere with the transaction here: either
- Not
merge
changes made to entities ~ or ~ -
refresh
any changes made to objects beforemerge
(but why are you callingmerge
?) ~ or ~ - Working with non-managed copies of objects ~ or ~
-
Declare some read-only classes (EclipseLink specific, see Declaring read-only classes and How to use @ReadOnly Annotation ). From the doc:
Any changes made to a managed instance in a transaction or a separate instance and merged will have no effect in the context of the read-only entity class.
a source to share