Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Savepoints, Checkpoints, Rollbacks?

There are no savepoints in JPA.  Please log this enhancement request in
EclipseLink.

The native EclipseLink API has something similar called nested UnitOfWork,
but these are entirely in-memory nested transactions.  Nested UnitOfWork
support has currently not been exposed in the EclipseLink JPA API.

With your code the main thing to be careful of is ensuring the consistency
of your object changes and database (the database will get rolled back, but
your objects will remain the same).  Clear will clear out all the objects,
so may be ok.  You also need to consider the shared cache, but by default a
flush() followed by a clear() will invalidate the changed objects, so should
be ok.



paulccarey wrote:
> 
> OK so it doesn't look like this is possible directly with JPA or
> Eclipselink as far as I can tell however I've managed to coble this
> together, in case its of any use to anyone hear goes....
> 
> first to get a savepoint...
> 
> EntityManager em;
> ...
> 
>     public Savepoint getSavepoint() throws SQLException {
>         em.flush();
>         //Get the SQL connection
>         Connection conn = ((UnitOfWorkImpl) ((JpaEntityManager)
> em).getActiveSession().acquireUnitOfWork()).getAccessor().getConnection();
> 
>         //Get a savepoint from the SQL connection
>         return conn.setSavepoint();
> 
>     }
> 
> next to make use of it...
>     public void commitToSave1(Savepoint sp) throws SQLException {
>         Connection conn = ((UnitOfWorkImpl) ((JpaEntityManager)
> em).getActiveSession().acquireUnitOfWork()).getAccessor().getConnection();
>         //roll the connection back to the savepoint
>         conn.rollback(sp);
>         //commit the current state of the transaction
>         conn.commit();
>         //clear the persistence context causing all managed entities to
>         //become detached
>         em.clear();
>         //commit the transaction to release locks and reset the entity
>         //manager
>         em.getTransaction().commit();
>     }
> 
> Hope that may be of use to someone:-D
> 
> 
> 
> paulccarey wrote:
>> 
>> Hi I am using eclipselink with JPA
>> 
>> Is it possible to create savepoints like JDBC can?
>> 
>> So I can rollback a transaction to a certain point without losing all
>> changes?
>> 
>> Thanks
>> 
>> P
>> 
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Savepoints%2C-Checkpoints%2C-Rollbacks--tp21980310p22059238.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top