LockExample.java
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1997, 1998, 1999, 2000
  5.  * Sleepycat Software.  All rights reserved.
  6.  *
  7.  * $Id: LockExample.java,v 11.5 2001/01/04 14:23:30 dda Exp $
  8.  */
  9. package com.sleepycat.examples;
  10. import com.sleepycat.db.*;
  11. import java.io.FileNotFoundException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.IOException;
  15. import java.io.PrintStream;
  16. import java.util.Vector;
  17. //
  18. // An example of a program using DbLock and related classes.
  19. //
  20. class LockExample extends DbEnv
  21. {
  22.     private static final String progname = "LockExample";
  23.     private static final String LOCK_HOME = "TESTDIR";
  24.     public LockExample(String home, int maxlocks, boolean do_unlink)
  25.          throws DbException, FileNotFoundException
  26.     {
  27.         super(0);
  28.         if (do_unlink) {
  29.             remove(home, Db.DB_FORCE);
  30.         }
  31.         else {
  32.             set_error_stream(System.err);
  33.             set_errpfx("LockExample");
  34.             if (maxlocks != 0)
  35.                 set_lk_max_locks(maxlocks);
  36.             open(home, Db.DB_CREATE|Db.DB_INIT_LOCK, 0);
  37.         }
  38.     }
  39.     // Prompts for a line, and keeps prompting until a non blank
  40.     // line is returned.  Returns null on error.
  41.     //
  42.     static public String askForLine(InputStreamReader reader,
  43.                                     PrintStream out, String prompt)
  44.     {
  45.         String result = "";
  46.         while (result != null && result.length() == 0) {
  47.             out.print(prompt);
  48.             out.flush();
  49.             result = getLine(reader);
  50.         }
  51.         return result;
  52.     }
  53.     // Not terribly efficient, but does the job.
  54.     // Works for reading a line from stdin or a file.
  55.     // Returns null on EOF.  If EOF appears in the middle
  56.     // of a line, returns that line, then null on next call.
  57.     //
  58.     static public String getLine(InputStreamReader reader)
  59.     {
  60.         StringBuffer b = new StringBuffer();
  61.         int c;
  62.         try {
  63.             while ((c = reader.read()) != -1 && c != 'n') {
  64.                 if (c != 'r')
  65.                     b.append((char)c);
  66.             }
  67.         }
  68.         catch (IOException ioe) {
  69.             c = -1;
  70.         }
  71.         if (c == -1 && b.length() == 0)
  72.             return null;
  73.         else
  74.             return b.toString();
  75.     }
  76.     public void run()
  77.          throws DbException
  78.     {
  79.         long held;
  80.         int len = 0, locker;
  81.         int ret;
  82.         boolean did_get = false;
  83.         int lockid = 0;
  84.         InputStreamReader in = new InputStreamReader(System.in);
  85.         Vector locks = new Vector();
  86.         //
  87.         // Accept lock requests.
  88.         //
  89.         locker = lock_id();
  90.         for (held = 0;;) {
  91.             String opbuf = askForLine(in, System.out,
  92.                                       "Operation get/release [get]> ");
  93.             if (opbuf == null)
  94.                 break;
  95.             try {
  96.                 if (opbuf.equals("get")) {
  97.                     // Acquire a lock.
  98.                     String objbuf = askForLine(in, System.out,
  99.                                    "input object (text string) to lock> ");
  100.                     if (objbuf == null)
  101.                         break;
  102.                     String lockbuf;
  103.                     do {
  104.                         lockbuf = askForLine(in, System.out,
  105.                                              "lock type read/write [read]> ");
  106.                         if (lockbuf == null)
  107.                             break;
  108.                         len = lockbuf.length();
  109.                     } while (len >= 1 &&
  110.                              !lockbuf.equals("read") &&
  111.                              !lockbuf.equals("write"));
  112.                     int lock_type;
  113.                     if (len <= 1 || lockbuf.equals("read"))
  114.                         lock_type = Db.DB_LOCK_READ;
  115.                     else
  116.                         lock_type = Db.DB_LOCK_WRITE;
  117.                     Dbt dbt = new Dbt(objbuf.getBytes());
  118.                     DbLock lock;
  119.                     did_get = true;
  120.                     lock = lock_get(locker, Db.DB_LOCK_NOWAIT,
  121.                                     dbt, lock_type);
  122.                     lockid = locks.size();
  123.                     locks.addElement(lock);
  124.                 } else {
  125.                     // Release a lock.
  126.                     String objbuf;
  127.                     objbuf = askForLine(in, System.out,
  128.                                         "input lock to release> ");
  129.                     if (objbuf == null)
  130.                         break;
  131.                     lockid = Integer.parseInt(objbuf, 16);
  132.                     if (lockid < 0 || lockid >= locks.size()) {
  133.                         System.out.println("Lock #" + lockid + " out of range");
  134.                         continue;
  135.                     }
  136.                     did_get = false;
  137.                     DbLock lock = (DbLock)locks.elementAt(lockid);
  138.                     lock.put(this);
  139.                 }
  140.                 System.out.println("Lock #" + lockid + " " +
  141.                                    (did_get ? "granted" : "released"));
  142.                 held += did_get ? 1 : -1;
  143.             }
  144.             catch (DbException dbe) {
  145.                 switch (dbe.get_errno()) {
  146.                 case Db.DB_LOCK_NOTGRANTED:
  147.                     System.out.println("Lock not granted");
  148.                     break;
  149.                 case Db.DB_LOCK_DEADLOCK:
  150.                     System.err.println("LockExample: lock_" +
  151.                                        (did_get ? "get" : "put") +
  152.                                        ": returned DEADLOCK");
  153.                     break;
  154.                 default:
  155.                     System.err.println("LockExample: lock_get: " + dbe.toString());
  156.                 }
  157.             }
  158.         }
  159.         System.out.println();
  160.         System.out.println("Closing lock region " + String.valueOf(held) +
  161.                            " locks held");
  162.     }
  163.     private static void usage()
  164.     {
  165.         System.err.println("usage: LockExample [-u] [-h home] [-m maxlocks]");
  166.         System.exit(1);
  167.     }
  168.     public static void main(String argv[])
  169.     {
  170.         String home = LOCK_HOME;
  171.         boolean do_unlink = false;
  172.         int maxlocks = 0;
  173.         for (int i = 0; i < argv.length; ++i) {
  174.             if (argv[i].equals("-h")) {
  175.                 if (++i >= argv.length)
  176.                     usage();
  177.                 home = argv[i];
  178.             }
  179.             else if (argv[i].equals("-m")) {
  180.                 if (++i >= argv.length)
  181.                     usage();
  182.                 try {
  183.                     maxlocks = Integer.parseInt(argv[i]);
  184.                 }
  185.                 catch (NumberFormatException nfe) {
  186.                     usage();
  187.                 }
  188.             }
  189.             else if (argv[i].equals("-u")) {
  190.                 do_unlink = true;
  191.             }
  192.             else {
  193.                 usage();
  194.             }
  195.         }
  196.         try {
  197.             if (do_unlink) {
  198.                 // Create an environment that immediately
  199.                 // removes all files.
  200.                 LockExample tmp = new LockExample(home, maxlocks, do_unlink);
  201.             }
  202.             LockExample app = new LockExample(home, maxlocks, do_unlink);
  203.             app.run();
  204.             app.close(0);
  205.         }
  206.         catch (DbException dbe) {
  207.             System.err.println(progname + ": " + dbe.toString());
  208.         }
  209.         catch (Throwable t) {
  210.             System.err.println(progname + ": " + t.toString());
  211.         }
  212.         System.out.println("LockExample completed");
  213.     }
  214. }