db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/otm OtmExamples.java
Date Sun, 18 May 2003 18:15:07 GMT
olegnitz    2003/05/18 11:15:07

  Modified:    src/test/org/apache/ojb/otm OtmExamples.java
  Log:
  Added test for deadlock detection
  
  Revision  Changes    Path
  1.8       +74 -7     db-ojb/src/test/org/apache/ojb/otm/OtmExamples.java
  
  Index: OtmExamples.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/otm/OtmExamples.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- OtmExamples.java	18 May 2003 15:11:58 -0000	1.7
  +++ OtmExamples.java	18 May 2003 18:15:06 -0000	1.8
  @@ -10,6 +10,7 @@
   import org.apache.ojb.otm.core.Transaction;
   import org.apache.ojb.otm.lock.LockType;
   import org.apache.ojb.otm.lock.LockingException;
  +import org.apache.ojb.otm.lock.wait.DeadlockException;
   import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
   import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
   
  @@ -208,6 +209,8 @@
           Transaction tx = null;
           Transaction tx2 = null;
           OTMConnection conn2;
  +        ProductGroup pg;
  +        ProductGroup pg2;
   
           conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
   
  @@ -215,14 +218,19 @@
           {
               tx = _kit.getTransaction(_conn);
               tx.begin();
  -
  -            ProductGroup pg = new ProductGroup();
  +            pg = new ProductGroup();
               pg.setId(77777);
               pg.setName("1");
               _conn.makePersistent(pg);
  +            pg2 = new ProductGroup();
  +            pg2.setId(77778);
  +            pg2.setName("1");
  +            _conn.makePersistent(pg2);
               tx.commit();
   
  -            Identity pgOid = _conn.getIdentity(pg);
  +            final Identity pgOid = _conn.getIdentity(pg);
  +            final Identity pgOid2 = _conn.getIdentity(pg2);
  +
   
               final Transaction tx3 = _kit.getTransaction(_conn);
               tx3.begin();
  @@ -241,21 +249,80 @@
               _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
               tx2 = _kit.getTransaction(conn2);
               tx2.begin();
  -            (new Thread() {
  -                public void run() {
  -                    try {
  +            (new Thread() 
  +            {
  +                public void run() 
  +                {
  +                    try 
  +                    {
                           Thread.sleep(1000);
                           tx3.commit();
  -                    } catch (InterruptedException ex) {
  +                    }
  +                    catch (InterruptedException ex)
  +                    {
                       }
                   }
               }).start();
               conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
               tx2.commit();
   
  +            // Third test for the TimeoutStrategy:
  +            // test deadlock detection
  +            _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
  +            tx = _kit.getTransaction(_conn);
  +            tx.begin();
  +            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
  +            tx2 = _kit.getTransaction(conn2);
  +            tx2.begin();
  +            conn2.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
  +            (new Thread()
  +            {
  +                public void run()
  +                {
  +                    try
  +                    {
  +                        _conn.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
  +                    }
  +                    catch (LockingException ex)
  +                    {
  +                        ex.printStackTrace();
  +                    }
  +                }
  +            }).start();
  +
  +            try
  +            {
  +                Thread.sleep(1000);
  +            }
  +            catch (InterruptedException ex)
  +            {
  +            }
  +
  +            try
  +            {
  +                conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
  +                fail("DeadlockException was not thrown");
  +            }
  +            catch (DeadlockException ex)
  +            {
  +                // ok, deadlock was detected
  +            }
  +            
  +            tx2.rollback();
  +            try
  +            {
  +                Thread.sleep(1000);
  +            }
  +            catch (InterruptedException ex)
  +            {
  +            }
  +
  +            tx.commit();
  +
               tx = _kit.getTransaction(_conn);
               tx.begin();
               _conn.deletePersistent(pg);
  +            _conn.deletePersistent(pg2);
               tx.commit();
           }
           catch (Exception ex)
  
  
  

Mime
View raw message