Project

General

Profile

« Previous | Next » 

Revision 1217

Added by Jing Tao almost 22 years ago

Merge DBConnection branch to head.

View differences:

ReplicationHandler.java
57 57
  int serverCheckCode = 1;
58 58
  MetaCatUtil util = new MetaCatUtil();
59 59
  Hashtable serverList = new Hashtable(); 
60
  Connection conn;
60
  //Connection conn;
61 61
  PrintWriter out;
62 62
  
63 63
  public ReplicationHandler(PrintWriter o)
......
82 82
    //xml_documents with an update_date > last_checked
83 83
    try
84 84
    {
85
      try
86
      { //this connection is prone to error for some reason so we 
85
      //try
86
      //{ //this connection is prone to error for some reason so we 
87 87
        //try to connect it three times before quiting.
88
        conn = util.openDBConnection();
89
      }
90
      catch(SQLException sqle)
91
      {
88
        //conn = util.openDBConnection();
89
      //}
90
      //catch(SQLException sqle)
91
      //{
92 92
        /*try
93 93
        {
94 94
          conn = util.openDBConnection();
......
101 101
          }
102 102
          catch(SQLException sqleee)
103 103
          {*/
104
            System.out.println("error getting db connection in " + 
105
                               "ReplicationHandler.run: " +
106
                               sqle.getMessage());
104
            //System.out.println("error getting db connection in " + 
105
                               //"ReplicationHandler.run: " +
106
                               //sqle.getMessage());
107 107
          //}
108 108
        //}
109
      }
110
      serverList = buildServerList(conn);
109
      //}
110
      serverList = buildServerList();
111 111
      //if serverList is null, metacat don't need to replication
112 112
      if (serverList==null||serverList.isEmpty())
113 113
      {
......
115 115
      }
116 116
      updateCatalog(serverList);
117 117
      update(serverList);
118
      conn.close();
119
    }
118
      //conn.close();
119
    }//try
120 120
    catch (Exception e)
121 121
    {
122
      //System.out.println("Error in replicationHandler.run():"+e.getMessage());
123
    }
122
      MetaCatUtil.debugMessage("Error in replicationHandler.run():"
123
                                                    +e.getMessage(), 30);
124
    }//catch
124 125
  }
125 126
  
126 127
  /**
......
142 143
       should be kept anyway)
143 144
    */
144 145
    
145
    Connection conn = null;
146
    try
146
    DBConnection dbConn = null;
147
    int serialNumber = -1;
148
    /*try
147 149
    {
148 150
      conn = util.getConnection();
149 151
    }
150 152
    catch (Exception e)
151 153
    {
152 154
      util.debugMessage("There is problem to get conncetion: "+e.getMessage());
153
    }
155
    }*/
154 156
    
155 157
    Enumeration keys;
156 158
    String server;
......
168 170
    
169 171
    try
170 172
    {
173
      dbConn=DBConnectionPool.
174
                  getDBConnection("ReplicationHandler.update");
175
      serialNumber=dbConn.getCheckOutSerialNumber();
171 176
      MetaCatUtil.debugMessage("init parser");
172 177
      parser = initParser(message);
173 178
      keys = serverList.keys();
......
235 240
          String docServer = (String)w.elementAt(2);
236 241
          
237 242
    
238
          pstmt = conn.prepareStatement("select rev from xml_documents where "+
243
         pstmt = dbConn.prepareStatement("select rev from xml_documents where "+
239 244
                                        "docid like '" + docid + "'");
240 245
          pstmt.execute();
241 246
          rs = pstmt.getResultSet();
......
292 297
            try
293 298
            {
294 299
             
295
              String newDocid = DocumentImpl.writeReplication(conn, 
300
              String newDocid = DocumentImpl.writeReplication(dbConn, 
296 301
                              new StringReader(newxmldoc),
297 302
                              (String)docinfoHash.get("public_access"),
298 303
                              null,  /* the dtd text */
......
302 307
                              null, /* null for groups[] */
303 308
                              docHomeServer, 
304 309
                              false /* validate */);
310
              //increase usage
311
              dbConn.increaseUsageCount(1);
305 312
              MetacatReplication.replLog("wrote doc " + docid + " from " + 
306 313
                                         docServer);
307 314
              /*System.out.println("wrote doc " + docid + " from " + 
......
379 386
        { //delete the deleted documents;
380 387
          Vector w = new Vector((Vector)d.elementAt(k));
381 388
          String docid = (String)w.elementAt(0);
382
          if(!alreadyDeleted(docid, conn))
389
          if(!alreadyDeleted(docid))
383 390
          {
384 391
          
385 392
            //because delete method docid should have rev number
386 393
            //so we just add one for it. This rev number is no sence.
387 394
            String accnum=docid+util.getOption("accNumSeparator")+"1";
388 395
            //System.out.println("accnum: "+accnum);
389
            DocumentImpl.delete(conn, accnum, null, null);
396
            DocumentImpl.delete(accnum, null, null);
390 397
            MetacatReplication.replLog("doc " + docid + " deleted");
391 398
          }
392 399
        }//for delete docs
......
409 416
            sql.append("server like '").append(server).append("'");
410 417
            //System.out.println("sql: " + sql.toString());
411 418
            pstmt.close();
412
            pstmt = conn.prepareStatement(sql.toString());
419
            pstmt = dbConn.prepareStatement(sql.toString());
420
            //increase usage count
421
            dbConn.increaseUsageCount(1);
413 422
            pstmt.executeUpdate();
414
            //conn.commit();
423
            dbConn.commit();
415 424
            System.out.println("last_checked updated to " + datestr + " on " +
416 425
                            server);
417 426
          }
......
429 438
      try
430 439
      {
431 440
        pstmt.close();
432
        util.returnConnection(conn);
433
      }
434
      catch(Exception ee) {}
441
      }//try
442
      catch(Exception ee) 
443
      {
444
        MetaCatUtil.debugMessage("Error in ReplicationHandler.update to close"+
445
                                " pstmt: "+ee.getMessage(), 30);
446
      }//catch
447
      finally
448
      {
449
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
450
      }//finally
435 451
    }//finally
436 452
  }//update
437 453
  
......
440 456
   */
441 457
  private void updateCatalog(Hashtable serverList)
442 458
  {
443
    Connection conn = null;
459
    DBConnection dbConn = null;
460
    int serialNumber = -1;
461
    PreparedStatement pstmt = null;
444 462
    try
445 463
    {
446
      try
447
      { //this connection is prone to error for some reason so we 
448
        //try to connect it three times before quiting.
464
      /*try
465
      { 
449 466
        conn = util.openDBConnection();
450 467
      }
451 468
      catch(SQLException sqle)
......
467 484
                               sqleee.getMessage());
468 485
          }
469 486
        }
470
      }
487
      }*/
488
      
489
      dbConn=DBConnectionPool.
490
                  getDBConnection("ReplicationHandler.updateCatalog");
491
      serialNumber=dbConn.getCheckOutSerialNumber();
471 492
      String server;
472 493
      Enumeration keys = serverList.keys();
473 494
      while(keys.hasMoreElements())
......
521 542
            sql.append("target_doctype, public_id, system_id) values (?,?,?,");
522 543
            sql.append("?,?)");
523 544
            //System.out.println("sql: " + sql.toString());
524
            PreparedStatement pstmt = conn.prepareStatement(sql.toString());
545
            pstmt = dbConn.prepareStatement(sql.toString());
546
            //increase usage count 1
547
            dbConn.increaseUsageCount(1);
525 548
            pstmt.setString(1, (String)v.elementAt(0));
526 549
            pstmt.setString(2, (String)v.elementAt(1));
527 550
            pstmt.setString(3, (String)v.elementAt(2));
......
532 555
          }
533 556
        }
534 557
      }
535
      conn.close();
558
      //conn.close();
536 559
    }
537 560
    catch(Exception e)
538 561
    {
539 562
      System.out.println("error in ReplicationHandler.updateCatalog: " + 
540 563
                          e.getMessage());
541 564
      e.printStackTrace(System.out);
565
    }//catch
566
    finally
567
    {
542 568
      try
543 569
      {
544
        conn.close();
545
      }
546
      catch (Exception ee)
570
        pstmt.close();
571
      }//try
572
      catch (SQLException ee)
547 573
      {
548
      }
549
    }
574
        MetaCatUtil.debugMessage("Error in ReplicationHandler.updateCatalog "+
575
                      "to close pstmt: "+ee.getMessage(), 30);
576
      }//catch
577
      finally
578
      {
579
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
580
      }//finally
581
    }//finall
550 582
  }
551 583
  
552 584
  /**
......
560 592
   *   _             X             TRUE
561 593
   *   _             _             TRUE
562 594
   */
563
  private static boolean alreadyDeleted(String docid, Connection conn)
595
  private static boolean alreadyDeleted(String docid)
564 596
  {
597
    DBConnection dbConn = null;
598
    int serialNumber = -1;
599
    PreparedStatement pstmt = null;
565 600
    try
566 601
    {
602
      dbConn=DBConnectionPool.
603
                  getDBConnection("ReplicationHandler.alreadyDeleted");
604
      serialNumber=dbConn.getCheckOutSerialNumber();
567 605
      boolean xml_docs = false;
568 606
      boolean xml_revs = false;
569 607
      
570 608
      StringBuffer sb = new StringBuffer();
571 609
      sb.append("select docid from xml_revisions where docid like '");
572 610
      sb.append(docid).append("'");
573
      PreparedStatement pstmt = conn.prepareStatement(sb.toString());
611
      pstmt = dbConn.prepareStatement(sb.toString());
574 612
      pstmt.execute();
575 613
      ResultSet rs = pstmt.getResultSet();
576 614
      boolean tablehasrows = rs.next();
......
583 621
      sb.append("select docid from xml_documents where docid like '");
584 622
      sb.append(docid).append("'");
585 623
      pstmt.close();
586
      pstmt = conn.prepareStatement(sb.toString());
624
      pstmt = dbConn.prepareStatement(sb.toString());
625
      //increase usage count
626
      dbConn.increaseUsageCount(1);
587 627
      pstmt.execute();
588 628
      rs = pstmt.getResultSet();
589 629
      tablehasrows = rs.next();
......
618 658
    }
619 659
    finally
620 660
    {
621
      
622
    }
661
      try
662
      {
663
        pstmt.close();
664
      }//try
665
      catch (SQLException ee)
666
      {
667
        MetaCatUtil.debugMessage("Error in replicationHandler.alreadyDeleted "+
668
                          "to close pstmt: "+ee.getMessage(), 30);
669
      }//catch
670
      finally
671
      {
672
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
673
      }//finally
674
    }//finally
623 675
    return false;
624 676
  }
625 677
  
......
629 681
   */
630 682
  private static String getAction(String docid)
631 683
  {
684
    DBConnection dbConn = null;
685
    int serialNumber = -1;
686
    PreparedStatement pstmt = null;
632 687
    try
633 688
    {
634
      MetaCatUtil util = new MetaCatUtil();
689
      dbConn=DBConnectionPool.
690
                  getDBConnection("ReplicationHandler.getAction");
691
      serialNumber=dbConn.getCheckOutSerialNumber();
692
      //MetaCatUtil util = new MetaCatUtil();
635 693
      StringBuffer sql = new StringBuffer();
636 694
      sql.append("select docid from xml_documents where docid like '");
637 695
      sql.append(docid).append("'");
638
      Connection conn = util.openDBConnection();
639
      PreparedStatement pstmt = conn.prepareStatement(sql.toString());
696
      //Connection conn = util.openDBConnection();
697
      pstmt = dbConn.prepareStatement(sql.toString());
640 698
      pstmt.execute();
641 699
      ResultSet rs = pstmt.getResultSet();
642 700

  
643 701
      if(rs.next())
644 702
      {
645 703
        pstmt.close();
646
        conn.close();
704
        //conn.close();
647 705
        return "UPDATE";
648 706
      }
649 707
      else
650 708
      {
651 709
        pstmt.close();
652
        conn.close();
710
        //conn.close();
653 711
        return "INSERT";
654 712
      }
655 713
    }
......
658 716
      System.out.println("error in replicationHandler.getAction: " + 
659 717
                          e.getMessage());
660 718
    }
719
    finally
720
    {
721
      try
722
      {
723
        pstmt.close();
724
      }
725
      catch (SQLException sqlE)
726
      {
727
        MetaCatUtil.debugMessage("error in replicationHandler.getAction: " + 
728
                     "to close pstmt: "+ sqlE.getMessage(), 30);
729
      }
730
      finally
731
      {
732
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
733
      }
734
    }//finally
661 735
    return "";
662 736
  }
663 737
  
......
666 740
   * and it's last update time.
667 741
   * @param conn a connection to the database
668 742
   */
669
  public static Hashtable buildServerList(Connection conn)
743
  public static Hashtable buildServerList()
670 744
  {
745
    DBConnection dbConn = null;
746
    int serialNumber = -1;
671 747
    Hashtable sl = new Hashtable();
672
    PreparedStatement pstmt;   
748
    PreparedStatement pstmt = null;   
673 749
    try
674 750
    {
675
      pstmt = conn.prepareStatement("select server, last_checked, replicate " +
751
      dbConn=DBConnectionPool.
752
                  getDBConnection("ReplicationHandler.buildServerList");
753
      serialNumber=dbConn.getCheckOutSerialNumber();
754
      pstmt = dbConn.prepareStatement("select server, last_checked, replicate "+
676 755
                                    "from xml_replication");
677 756
      pstmt.execute();
678 757
      ResultSet rs = pstmt.getResultSet();
......
697 776
      System.out.println("error in replicationHandler.buildServerList(): " +
698 777
                         e.getMessage());
699 778
    }
779
    finally
780
    {
781
      try
782
      {
783
        pstmt.close();
784
      }//try
785
      catch (SQLException sqlE)
786
      {
787
        MetaCatUtil.debugMessage("Error in ReplicationHandler.buildServerList: "
788
                                +sqlE.getMessage());
789
      }//catch
790
      finally
791
      {
792
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
793
      }//finally
794
    }//finally
795
     
700 796
    return sl;
701 797
  }
702 798
   /**
......
706 802
  public static Hashtable getHomeServer(String docId)
707 803
  {
708 804
    Hashtable sl = new Hashtable();
709
    Connection conn = null;
710
    MetaCatUtil ut=new MetaCatUtil();
711
    docId=ut.getDocIdFromString(docId);
805
    DBConnection dbConn = null;
806
    int serialNumber = -1;
807
    //MetaCatUtil ut=new MetaCatUtil();
808
    docId=MetaCatUtil.getDocIdFromString(docId);
712 809
    PreparedStatement pstmt=null;
713 810
    int serverLocation;
714 811
    try
715 812
    {
716
      conn=ut.getConnection();
813
      //get conection
814
      dbConn=DBConnectionPool.
815
                  getDBConnection("ReplicationHandler.getHomeServer");
816
      serialNumber=dbConn.getCheckOutSerialNumber();
717 817
      //get a server location from xml_document table
718
      pstmt = conn.prepareStatement("select server_location from xml_documents "
818
      pstmt=dbConn.prepareStatement("select server_location from xml_documents "
719 819
                                            +"where docid = ?");
720 820
      pstmt.setString(1, docId);
721 821
      pstmt.execute();
......
729 829
      else
730 830
      {
731 831
        pstmt.close();
732
        ut.returnConnection(conn);
832
        //ut.returnConnection(conn);
733 833
        return null;
734 834
      }
735
      pstmt = conn.prepareStatement("select server, last_checked, replicate " +
835
      pstmt=dbConn.prepareStatement("select server, last_checked, replicate " +
736 836
                        "from xml_replication where serverid = ?");
837
      //increase usage count
838
      dbConn.increaseUsageCount(1);
737 839
      pstmt.setInt(1, serverLocation);
738 840
      pstmt.execute();
739 841
      ResultSet rs = pstmt.getResultSet();
......
752 854
      else
753 855
      {
754 856
        pstmt.close();
755
        ut.returnConnection(conn);
857
        //ut.returnConnection(conn);
756 858
        return null;
757 859
      }
758 860
      pstmt.close();
759 861
    }
760 862
    catch(Exception e)
761 863
    {
762
      System.out.println("error in replicationHandler.buildServerList(): " +
864
      System.out.println("error in replicationHandler.getHomeServer(): " +
763 865
                         e.getMessage());
764 866
    }
765 867
    finally
......
767 869
      try
768 870
      {
769 871
        pstmt.close();
770
        ut.returnConnection(conn);
872
        //ut.returnConnection(conn);
771 873
      }
772 874
      catch (Exception ee)
773 875
      {
876
        MetaCatUtil.debugMessage("Eror irn rplicationHandler.getHomeServer() "+
877
                          "to close pstmt: "+ee.getMessage(), 30);
774 878
      }
775
      
776
    }
879
      finally
880
      {
881
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
882
      }
883
    
884
    }//finally
777 885
    return sl;
778 886
  }
779 887
  
......
784 892
   */
785 893
  public static int getServerLocation(String accNum) throws Exception
786 894
  {
787
    Connection conn = null;
895
    DBConnection dbConn = null;
896
    int serialNumber = -1;
788 897
    PreparedStatement pstmt = null;
789 898
    int serverCode = 1;
790
    MetaCatUtil ut = new MetaCatUtil();
791
    String docId=ut.getDocIdFromString(accNum);
899
    //MetaCatUtil ut = new MetaCatUtil();
900
    String docId=MetaCatUtil.getDocIdFromString(accNum);
792 901

  
793 902
    try 
794 903
    {
795 904

  
796
      conn = ut.openDBConnection();
797
      pstmt = conn.prepareStatement("SELECT server_location FROM xml_documents " 
905
      //conn = ut.openDBConnection();
906
      //get conection
907
      dbConn=DBConnectionPool.
908
                  getDBConnection("ReplicationHandler.getServerLocation");
909
      serialNumber=dbConn.getCheckOutSerialNumber();
910
      pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents " 
798 911
                              + "WHERE docid LIKE '" + docId + "'");
799 912
      pstmt.execute();
800 913
      ResultSet rs = pstmt.getResultSet();
......
804 917
      {  
805 918
        serverCode = rs.getInt(1);
806 919
        pstmt.close();
807
        conn.close();
920
        //conn.close();
808 921
        return serverCode;
809 922
      }
810 923
      //if couldn't find in xml_documents table, we think server code is 1
......
812 925
      else
813 926
      {
814 927
        pstmt.close();
815
        conn.close();
928
        //conn.close();
816 929
        return serverCode;
817 930
      }
818 931
      
......
828 941
      try 
829 942
      {
830 943
        pstmt.close();
831
        conn.close();
832
        return serverCode;
944
        //conn.close();
945
        
833 946
      } 
834 947
      catch(Exception ee) 
835 948
      {
836
        
949
        MetaCatUtil.debugMessage("Erorr in Replication.getServerLocation "+
950
                     "to close pstmt"+ee.getMessage(), 30);
837 951
      }
952
      finally
953
      {
954
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
955
      }//finally
838 956
    }//finally
839
  
957
   //return serverCode;
840 958
  }
841 959
  
842 960
  

Also available in: Unified diff