1 19 package org.apache.james.mailboxmanager.torque.om; 20 21 import java.sql.Connection ; 22 import java.sql.SQLException ; 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 runThread(final long id, final int no) { 35 Thread t = new Thread () { 36 public void run() { 37 System.out.println("Thread " + no + " started."); 38 Connection 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 state =""; 58 Transaction.safeRollback(c); 59 Throwable t = e.getCause(); 60 if (t instanceof SQLException ) { 61 errorCode=((SQLException ) t).getErrorCode(); 62 if (errorCode==1213) retry=true; 63 state = ((SQLException ) t).getSQLState(); 64 } 65 System.out.println("Thread " + no + " State: "+state+ " SQLERROR"+errorCode + " " + e); 66 } catch (SQLException e) { 67 System.out.println("SQLException!"); 68 e.printStackTrace(); 69 } 70 } while (retry); 74 75 } 76 77 }; 78 t.start(); 79 return t; 80 } 81 82 public void testConcurrentSerializableMailboxRowUidUpdate() throws Exception { 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 t[] = new Thread [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 { 101 } 104 105 } 106 | Popular Tags |