[prev in list] [next in list] [prev in thread] [next in thread] 

List:       slide-user
Subject:    Slide in multi-thread environment
From:       Ting Ning <ting_ning () yahoo ! com>
Date:       2001-09-26 2:24:39
[Download RAW message or body]

Hi,

I write a small test program to test slide in
multi-threads env. I enclose my test program here.
Please take look at it. The NamespaceAccessToken is
initialized in the main program. Then it is passed to
the different thread. Each thread just puts the files
into Slide, and gets it back into file. Each thread
works on different file.

I run the test program with Slide 1.0.14 with Oracle
database as descriptor and content stores. I get the
error like the transaction is not consistent, for
example, the Oracle error is "fetch out of sequence".
It is because that connection is shared between the
two thread. 

After doing some debug, I found out there is only one
database connection in this Slide test. This is why my
test fails.

Do I do the right thing in this test?
If not, how can I do it?

Any suggestions are welcome!

Thanks in Advance.

Ting

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com
["Test.java" (text/plain)]

import java.util.Enumeration;
import java.util.Vector;
import java.io.*;
import java.util.Date;
import org.apache.slide.structure.*;
import org.apache.slide.content.*;
import org.apache.slide.common.*;
import org.apache.slide.lock.*;
import org.apache.slide.security.*;
import org.apache.slide.authenticate.CredentialsToken;
import org.apache.slide.authenticate.SecurityToken;

public class Test 
{
  public static void main(String[] args) 
  {      
    NamespaceAccessToken token = null;
        
    try 
    {
      token = Domain.accessNamespace(new SecurityToken(new String()), 
                                     "slide");
      TestThread t1 = new TestThread(token, "ORACLE");
      new Thread(t1).start();
      
      TestThread t2 = new TestThread(token, "IBM");
      new Thread(t2).start();

      Thread.sleep(100000000);
    } 
    catch (Throwable e) 
    {
      e.printStackTrace();
    } 
    finally 
    {
      System.out.println("Closing data sources");
      Domain.closeNamespace(token);
    }
  }   
}

["TestThread.java" (text/plain)]

import java.util.Enumeration;
import java.util.Vector;
import java.io.*;
import java.util.Date;
import org.apache.slide.structure.*;
import org.apache.slide.content.*;
import org.apache.slide.common.*;
import org.apache.slide.lock.*;
import org.apache.slide.security.*;
import org.apache.slide.authenticate.CredentialsToken;
import org.apache.slide.authenticate.SecurityToken;

public class TestThread implements Runnable
{
  private NamespaceAccessToken _token = null;
  private String _key = null;

  public TestThread(NamespaceAccessToken token, String key)
  {
    _token = token;
    _key = key;
  }
        
  public void run()
  {
    try 
    {
      Structure structure = _token.getStructureHelper();
      Security security = _token.getSecurityHelper();
      Lock lock = _token.getLockHelper();
      Content content = _token.getContentHelper();
            
      CredentialsToken credToken = 
        new CredentialsToken(new String("root"));
      SlideToken slideToken = new SlideToken(credToken);
            
      SubjectNode subject = new SubjectNode();

      String myUri = null;

      NodeRevisionDescriptors revisionDescriptors = null;
      NodeRevisionDescriptor revisionDescriptor = null;

      for (int i=0; i<100; i++)
      {
        _token.begin();

        myUri="/"+_key+i;
        
        System.out.println(Thread.currentThread().getName()+
                           ": Put uri = " + myUri);

        structure.create(slideToken, subject, myUri);
            
        // Creating a versioned revision descriptors
        content.create(slideToken, myUri, true);
            
        revisionDescriptors = content.retrieve(slideToken, myUri);
            
        // NodeRevisionContent
        NodeRevisionContent nodeRevContent = new NodeRevisionContent();
        InputStream is = null;

        // Now creating some revisions
        revisionDescriptor = new NodeRevisionDescriptor(0);

        revisionDescriptor.setProperty("revision", "1");
        is = new FileInputStream("Test.in");
        nodeRevContent.setContent(is);
        content.create(slideToken, myUri, revisionDescriptor, 
                       nodeRevContent);
        _token.commit();
        is.close();
      }

      // Retrieving some revisions
      revisionDescriptor = 
        content.retrieve(slideToken, revisionDescriptors);

      for (int i=0; i<100; i++)
      {
        myUri = "/"+_key+i;
        
        System.out.println(Thread.currentThread().getName()+
                           ": Get uri = " + myUri);

        NodeRevisionContent newRevContent = 
          content.retrieve(slideToken, myUri, revisionDescriptor);
        InputStream newIS = newRevContent.streamContent();
        if (null != newIS)
        {
          byte[] buf = new byte[60];
          int num = -1;
          FileOutputStream fos = new FileOutputStream("out/Test"+i+".out");
          while ((num = newIS.read(buf)) > 0)
          {
            fos.write(buf, 0, num);
            fos.flush();
          }
          fos.close();
          newIS.close();
        }
      }
    } 
    catch (Throwable e) 
    {
      e.printStackTrace();
    } 
  }   
}





[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic