001: package org.apache.ojb.odmg;
002:
003: import java.util.Collection;
004:
005: import org.apache.ojb.broker.TestHelper;
006: import org.apache.ojb.odmg.shared.Article;
007: import org.odmg.Database;
008: import org.odmg.Implementation;
009: import org.odmg.OQLQuery;
010: import org.odmg.Transaction;
011:
012: /**
013: * put your documentation comment here
014: */
015: public class TestThreadsNLocks extends Thread {
016:
017: private static String databaseName;
018: private static Implementation odmg;
019: private static Database db;
020:
021: static {
022: databaseName = TestHelper.DEF_DATABASE_NAME;
023: }
024:
025: /**
026: * put your documentation comment here
027: * @param args
028: */
029: public static void main(String[] args) {
030: try {
031:
032: // get odmg facade instance
033: odmg = OJB.getInstance();
034: db = odmg.newDatabase();
035: //open database
036:
037: db.open(databaseName, Database.OPEN_READ_WRITE);
038:
039: TestThreadsNLocks test = new TestThreadsNLocks();
040: test.start();
041: TestThreadsNLocks test2 = new TestThreadsNLocks();
042: test2.start();
043: } catch (Exception except) {
044: System.out.println(except);
045: except.printStackTrace(System.out);
046: }
047: }
048:
049: /**
050: * put your documentation comment here
051: * @param PrintWriter writer
052: */
053: public TestThreadsNLocks() throws Exception {
054:
055: }
056:
057: /**
058: * put your documentation comment here
059: */
060: public void run() {
061:
062: //System.out.println("The list of available products:");
063: try {
064: // 1. open a transaction
065: Transaction tx = odmg.newTransaction();
066: tx.begin();
067: // 2. get an OQLQuery object from the ODMG facade
068: OQLQuery query = odmg.newOQLQuery();
069: // 3. set the OQL select statement
070: query.create("select all from " + Article.class.getName());
071: // 4. perform the query and store the result in a persistent Collection
072: Collection allArticles = (Collection) query.execute();
073: // 5. now iterate over the result to print each product
074: java.util.Iterator iter = allArticles.iterator();
075: Article a = null;
076: while (iter.hasNext()) {
077: a = (Article) iter.next();
078:
079: if (tx.tryLock(a, Transaction.WRITE)) {
080:
081: //db.makePersistent(a);
082: //System.out.println(super.getName() + " ---- Lock erhalten & warten"+ a);
083: Thread.sleep(1000);
084: a
085: .setArticleName(super .getName()
086: + a.getArticleId());
087: } else {
088: //System.out.println(super.getName() + " ---- Konnte lock nicht bekommen" + a);
089: }
090:
091: }
092:
093: tx.commit();
094: db.close();
095: //System.out.println("DB-close");
096: } catch (Throwable t) {
097: t.printStackTrace();
098: // db.close();
099: }
100: }
101: }
|