Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Doing all my transactions in one transaction(Simulating changing a primary key)
Doing all my transactions in one transaction [message #1629019] Sun, 22 February 2015 12:20 Go to next message
Bill Damage is currently offline Bill DamageFriend
Messages: 5
Registered: February 2015
Junior Member
EclipseLink 2.4.0 under Resin 4.0.42
Basically I want to know a better way to achieve something which works but I feel I'm doing via a kludge. I have an entity for which I effectively want to change a primary key. I know that's a no-no so my approach is to create the new key, copy the existing fields over then delete the old one. The problem is I can't figure out how to do it in 1 transaction, I get various errors along the lines of my entities not being managed etc. I want to to it in the same transaction to guarantee atomicity. So, the kludge is to clone the key, close and persist the trans, then do another one just for the delete. Sorry for the verbosity. You can see the delete code is in the "is nameChanged(...)" section.

Thanks very much in advance, and please assume skilllevel=newbie Wink

Here we go:

[font=Courier]@WebServlet(value="/register", name="register-servlet")
public class RegServlet extends HttpServlet {

    @PersistenceUnit(unitName="custom")
    private EntityManagerFactory emf;
    private EntityManager em;

    @Inject
    private UserTransaction ut;

        ...

    private void writeUser(User userToPersist) {
        User userWithSameRegId = null;
        boolean nameChanged = false;

        try {
            ut.begin();
            em = emf.createEntityManager();

            Query query = em.createQuery("select u from User u where u.name = ?1");
            query.setParameter(1, userToPersist.getName());

            User user = null;
            try {
                user = (User) query.getSingleResult();
            } catch (NoResultException nrx) {

                userWithSameRegId = findUserFromRegId(userToPersist.get_Dev_Reg_Id());
                if (userWithSameRegId != null) {

                    log.fine(s+"RegID Existed as: " + userWithSameRegId.getName());

                    user = new User();
                    user.setName(userToPersist.getName());
                    ...

                    nameChanged = true;
                } else {

                    log.fine(s+"New user");

                    user = userToPersist;
                    user.set_Reg_Count(1);
                }
            }

            user.set_Reg_Date(new Date());
            em.persist(user);
            em.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ut.commit();
            } catch (Exception commitEx) {
                log.fine(commitEx.toString());
            }

            if (em != null && em.isOpen()) {
                em.close();
            }
        }

        if (nameChanged) {
            try {
                ut.begin();
                em = emf.createEntityManager();

                Query query = em.createQuery("select u from User u where u.name = ?1");
                query.setParameter(1, userWithSameRegId.getName());

                User user = null;
                try {
                    user = (User) query.getSingleResult();
                    em.remove(user);
                } catch (NoResultException nrx) {

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    ut.commit();
                } catch (Exception commitEx) {
                    log.fine(commitEx.toString());
                }

                if (em != null && em.isOpen()) {
                    em.close();
                }
            }
        }
    }
[/font]
Re: Doing all my transactions in one transaction [message #1633197 is a reply to message #1629019] Tue, 24 February 2015 18:44 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
I see no reason why you cannot create a new entity and delete another in the same transaction, especially as you state you are NOT using the same primary key. The only issues you should encounter are when you are using the same entity instance.

Can you show the code you use and the exceptions you get?
Re: Doing all my transactions in one transaction [message #1639291 is a reply to message #1633197] Fri, 27 February 2015 15:09 Go to previous message
Bill Damage is currently offline Bill DamageFriend
Messages: 5
Registered: February 2015
Junior Member
Thanks. Your comment gave me the confidence to continue refactoring, and it now works with basically the contents of the "is nameChanged()" test moved to the place it was originally invoked.
Previous Topic:oneToMany with composite key using eclipselink
Next Topic:JPQL to SQL Translation Bug?
Goto Forum:
  


Current Time: Sat Apr 20 20:19:52 GMT 2024

Powered by FUDForum. Page generated in 0.03445 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top