⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 solutionlocktn.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

     //	   System.out.println("sql: "+sql);

           return  doSQL(sql);
    }


    /**
     * Returns collection of SolutionLocks for this id. Should only be one.
     */
     public static Collection getLockInfo (long id) {

           String sql =
                  "Select "+ColumnNames+" from " + TableName + " where evid = "+id;
           return getBySQL(sql);
    }

    /**
     * Returns collection of SolutionLocks based on the results of the SQL query to an
     * NCDCv1.5 data base. I Returns null if no data is found.  Uses default connection
     * created by DataSource.  */
    protected static Collection getBySQL(String sql)
    {
  if (DataSource.getConnection() == null)
      {
    System.err.println ("* No DataSource is open.");
    return null;
      }
  return getBySQL(DataSource.getConnection(), sql);
    }

    /**
     * Returns collection of SolutionLocks based on the results of the SQL query
     * to the default data base.  Returns empty list if no data is found.  */
    protected static synchronized Collection getBySQL(Connection conn, String sql)
    {

  ArrayList list = new ArrayList();

     if (!lockingWorks) return list;      // default behavior if no locking

  // Debug
  //	System.out.println ("SQL: "+sql);

  try {
      if ( conn.isClosed() )	// check that valid connection exists
    {
        System.err.println ("* DataSource connection is closed");
        return list;           // list will be empty
    }

      Statement sm = conn.createStatement();

      ResultSetDb rs = new ResultSetDb(ExecuteSQL.rowQuery(sm, sql));

      if (rs == null) return list;	// nothing found
      if (rs.getResultSet() == null) return list;	// nothing found

      while ( rs.getResultSet().next() ) {
    SolutionLockTN solLock = new SolutionLockTN();

    solLock.id             = rs.getDataLong("EVID").longValue();
    solLock.host           = rs.getStringEmpty("HOSTNAME");
    solLock.application    = rs.getStringEmpty("APPLICATION");
    solLock.username       = rs.getStringEmpty("USERNAME");
    // Convert from millisecs to seconds :. /1000.0
    // Note: time is dbase time (SYSTIME) :. probably local time not UTC.
    solLock.datetime       = rs.getDataTimestamp("LDDATE").doubleValue()/1000.0;

    list.add(solLock);
      }

      sm.close();

  }
  catch (SQLException ex) {
//	    System.err.println(ex);
//	    ex.printStackTrace();
         return null;
  }

  return (Collection) list;
    }

    /** */
    protected static boolean doSQL(String sql)
    {
  if (DataSource.getConnection() == null)
      {
    System.err.println ("* No DataSource is open.");
    return false;
      }
  return doSQL(DataSource.getConnection(), sql);
    }

    /** */
    // Synchronize to avoid conflicts.
    protected static synchronized boolean doSQL(Connection conn, String sql)
    {

     if (!lockingWorks) return false;      // default behavior if no locking

     boolean status = true;

  try {
      if ( conn.isClosed() )	// check that valid connection exists
    {
        System.err.println ("* DataSource connection is closed");
        return false;
    }

      Statement sm = conn.createStatement();

      int nrows = sm.executeUpdate(sql);
         if (nrows > 0) {
            status = true;
            conn.commit();
         }
      sm.close();

  }
  catch (SQLException ex) {
//	    System.err.println(ex);
//	    ex.printStackTrace();
         return false;
  }

  return status;
    }

// ///////////////////////////////////////////////////////
/*
  Tested 3 cases:

  1) Lock tables exist on DataSource dbase
  2) The connection is dead
  3) Connection is OK but no lock tables exist in the dbase
*/
    public static void main (String args[])
    {
      String driver = "oracle.jdbc.driver.OracleDriver";
  String url    = "jdbc:oracle:thin:@makalu.gps.caltech.edu:1521:makaludb";
  String user   = "browser";
  String passwd = "browser";


     int test = 1;

     DataSource ds;

// Case 1
     if (test == 1) {
      System.err.println (" *** Test with valid DataSource connection ***");
      System.err.println ("Making DataSource connection... "+url);
          ds = new DataSource(url, driver, user, passwd);
// Case 2
     } else if (test == 2) {
      System.err.println (" *** Test with NULL DataSource connection ***");
      System.err.println ("Making DataSource connection...");
          ds =  new DataSource();	// make connection

// Case 3
     } else {
      System.err.println (" *** Test with valid DataSource connection to dbase not supporting locking ***");

      //driver = "oracle.jdbc.driver.OracleDriver";
  url    = "jdbc:oracle:thin:@hotspot.gps.caltech.edu:1521:hotsptdb";

        System.out.println ("Making connection... "+url);

  ds = new DataSource (url, driver, user, passwd);    // make connection
     }

     EnvironmentInfo.setApplicationName("LockTest");

     System.err.println (ds.toString());

     SolutionLock solLock = SolutionLock.create();

     System.err.println ("solLock.isSupported() = "+solLock.isSupported() );

     ArrayList list = (ArrayList) solLock.getAllLocks();

       System.err.println("----- Current locks ------");
     if (list == null) {
       System.err.println("No locks.");
     } else {
    System.err.println ("Lock count = "+ list.size());

       for (int i = 0; i< list.size(); i++) {

         SolutionLock sl = (SolutionLock) list.get(i);
         System.out.println (sl.toString());
       }
     }

     int base = 123000;

      Solution sol = Solution.create();
      SolutionLock lock = SolutionLock.create();

      for (int i = 0; i< 5; i++) {
    sol.id.setValue(base + i);

    System.err.print("Lock solution evid = "+sol.id);

    lock.setSolution(sol);
    boolean status = lock.lock();

    System.out.println ("      Status = "+status);
      }

      list = (ArrayList) solLock.getAllLocks();


      System.err.println("----- Current locks ------");
      if (list == null) {
    System.err.println("No locks.");
      } else {
    System.err.println ("Lock count = "+ list.size());

    System.err.println (SolutionLock.getHeaderString());
    for (int i = 0; i< list.size(); i++) {

        System.out.println (((SolutionLock)list.get(i)).toFormattedString());
    }
      }
      System.err.println("----- Unlock ------");

      for (int i = 0; i< 5; i++) {
    sol.id.setValue(base + i);

    System.err.print("Lock solution evid = "+sol.id);


    lock.setSolution(sol);
    boolean status = lock.unlock();

    System.out.println ("      Status = "+status);
      }

      solLock.unlockAllMyLocks();

      System.err.println("----- Current locks ------");
      list = (ArrayList) solLock.getAllLocks();
      if (list == null) {
    System.err.println("No locks.");
      } else {
    System.err.println ("Lock count = "+ list.size());
    System.err.println (SolutionLock.getHeaderString());
    for (int i = 0; i< list.size(); i++) {

        System.out.println (((SolutionLock)list.get(i)).toFormattedString());
    }
      }

     long id =11027256;
     solLock.setSolution(id);

    if (solLock.lock()) {

   String str = "EVENT "+id+" just got LOCKED.\n\n" +
             "Username:    "+solLock.getUsername()+"\n"+
             "Hostname:    "+solLock.host+"\n"+
             "Application: "+solLock.application+"\n"+
             "Time:        "+EpochTime.toString(solLock.datetime).substring(0, 19);
      System.out.println (str);


   // lock failed, pop a dialog
    } else {
   String str = "EVENT "+id+" IS LOCKED.\n\n" +
             "Username:    "+solLock.getUsername()+"\n"+
             "Hostname:    "+solLock.host+"\n"+
             "Application: "+solLock.application+"\n"+
             "Time:        "+EpochTime.toString(solLock.datetime).substring(0, 19);
      System.out.println (str);
    }


    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -