1 package org.apache.ojb.odmg; 2 3 import java.util.Collection ; 4 5 import org.apache.ojb.broker.TestHelper; 6 import org.apache.ojb.odmg.shared.Article; 7 import org.odmg.Database; 8 import org.odmg.Implementation; 9 import org.odmg.OQLQuery; 10 import org.odmg.Transaction; 11 12 13 16 public class TestThreadsNLocks extends Thread 17 { 18 19 private static String databaseName; 20 private static Implementation odmg; 21 private static Database db; 22 23 static 24 { 25 databaseName = TestHelper.DEF_DATABASE_NAME; 26 } 27 28 32 public static void main(String [] args) 33 { 34 try 35 { 36 37 odmg = OJB.getInstance(); 39 db = odmg.newDatabase(); 40 42 db.open(databaseName, Database.OPEN_READ_WRITE); 43 44 45 TestThreadsNLocks test = new TestThreadsNLocks(); 46 test.start(); 47 TestThreadsNLocks test2 = new TestThreadsNLocks(); 48 test2.start(); 49 } 50 catch (Exception except) 51 { 52 System.out.println(except); 53 except.printStackTrace(System.out); 54 } 55 } 56 57 61 public TestThreadsNLocks() throws Exception 62 { 63 64 } 65 66 69 public void run() 70 { 71 72 try 74 { 75 Transaction tx = odmg.newTransaction(); 77 tx.begin(); 78 OQLQuery query = odmg.newOQLQuery(); 80 query.create("select all from " + Article.class.getName()); 82 Collection allArticles = (Collection ) query.execute(); 84 java.util.Iterator iter = allArticles.iterator(); 86 Article a = null; 87 while (iter.hasNext()) 88 { 89 a = (Article) iter.next(); 90 91 if (tx.tryLock(a, Transaction.WRITE)) 92 { 93 94 Thread.sleep(1000); 97 a.setArticleName(super.getName() + a.getArticleId()); 98 } 99 else 100 { 101 } 103 104 } 105 106 tx.commit(); 107 db.close(); 108 } 110 catch (Throwable t) 111 { 112 t.printStackTrace(); 113 } 115 } 116 } 117 118 119 120 121 122 | Popular Tags |