KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > torque > om > MailboxRowPeerTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19 package org.apache.james.mailboxmanager.torque.om;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import org.apache.james.mailboxmanager.torque.AbstractMailboxRowTestCase;
25 import org.apache.torque.TorqueException;
26 import org.apache.torque.util.Transaction;
27
28 public class MailboxRowPeerTest extends AbstractMailboxRowTestCase {
29
30     public MailboxRowPeerTest() throws TorqueException {
31         super();
32     }
33
34     private Thread JavaDoc runThread(final long id, final int no) {
35         Thread JavaDoc t = new Thread JavaDoc() {
36             public void run() {
37                 System.out.println("Thread " + no + " started.");
38                 Connection JavaDoc c = null;
39                 boolean retry;
40                 do {
41                     retry = false;
42                     try {
43                         c = Transaction.begin("mailboxmanager");
44                         c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
45                         c.setAutoCommit(false);
46                         MailboxRow m = MailboxRowPeer.retrieveByPK(id, c);
47                         System.out.println("Thread " + no + " sleeps.");
48                         long uid = m.getLastUid() + 1;
49                         m.setLastUid(uid);
50                         m.save(c);
51                         Transaction.commit(c);
52                         System.out.println("Thread " + no + " has set to "
53                                 + uid);
54                     } catch (TorqueException e) {
55
56                         int errorCode = -1;
57                         String JavaDoc state ="";
58                         Transaction.safeRollback(c);
59                         Throwable JavaDoc t = e.getCause();
60                         if (t instanceof SQLException JavaDoc) {
61                             errorCode=((SQLException JavaDoc) t).getErrorCode();
62                             if (errorCode==1213) retry=true;
63                             state = ((SQLException JavaDoc) t).getSQLState();
64                         }
65                         System.out.println("Thread " + no + " State: "+state+ " SQLERROR"+errorCode + " " + e);
66                     } catch (SQLException JavaDoc e) {
67                         System.out.println("SQLException!");
68                         e.printStackTrace();
69                     }
70 // catch (InterruptedException e) {
71
// e.printStackTrace();
72
// }
73
} while (retry);
74
75             }
76
77         };
78         t.start();
79         return t;
80     }
81
82     public void testConcurrentSerializableMailboxRowUidUpdate() throws Exception JavaDoc {
83          MailboxRow m=new MailboxRow();
84          m.setName("#users.joachim.INBOX");
85          m.save();
86          m=MailboxRowPeer.retrieveByName("#users.joachim.INBOX");
87          long id=m.getMailboxId();
88
89         Thread JavaDoc t[] = new Thread JavaDoc[16];
90         for (int i = 0; i < t.length; i++) {
91             t[i] = runThread(id, i);
92         }
93         for (int i = 0; i < t.length; i++) {
94             System.out.println("Warte auf Thread " + i);
95             t[i].join();
96         }
97
98     }
99
100     public void runBare() throws Throwable JavaDoc {
101         // This avoid unittest to be ran because it doesn't work with derby, don't know why.
102
// TODO fix and enable (removing this empty method)
103
}
104
105 }
106
Popular Tags