openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From garpinc <garp...@yahoo.com>
Subject Re: Duplicate entry issue with multithreaded application
Date Wed, 26 Sep 2012 17:23:12 GMT
This should do it.. The 2nd transaction get's rolled back because of a unique
key violation..
In my cases the 2 entity manager is running in parallel so at the time it's
ascertained that the object doesn't exist and thus should be created both
threads get the same result.. so the behavior i want is that the 2nd em when
encountering the unique violation then treats the key as the same and
preferably does an update instead or at minimimum simply just ignores the
error and moves on. 

    @Test
    public void testUniqueKey() throws IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
        List<Class&lt;?>> classes = new ArrayList<Class&lt;?>>();
        classes.add(MasterClass.class);
        classes.add(DetailClass.class);
        classes.add(KeyClass.class);
        EntityManagerFactory emf = testClass(classes, false);
        EntityManager em1 = emf.createEntityManager();
        EntityManager em2 = emf.createEntityManager();
        Class<?> masterClassClass =
Thread.currentThread().getContextClassLoader().loadClass(MasterClass.class.getName());
        Class<?> keyClassClass =
Thread.currentThread().getContextClassLoader().loadClass(KeyClass.class.getName());

        EntityTransaction t1 = em1.getTransaction();
        EntityTransaction t2 = em1.getTransaction();
        IMasterClass mc1 = (IMasterClass) masterClassClass.newInstance();
        KeyInterface key1 = (KeyInterface) keyClassClass.newInstance();
        IMasterClass mc2 = (IMasterClass) masterClassClass.newInstance();
        KeyInterface key2 = (KeyInterface) keyClassClass.newInstance();
        t1.begin();
        key1.setUniqueField1("u1");
        mc1.setKey(key1);
        em1.persist(mc1);
        t1.commit();
        em1.close();
        t2.begin();
        key2.setUniqueField1("u2");
        mc2.setKey(key2);
        em2.persist(mc2);
        t2.commit();
        em2.close();

    }



--
View this message in context: http://openjpa.208410.n2.nabble.com/Duplicate-entry-issue-with-multithreaded-application-tp7580917p7581226.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message