Project

General

Profile

« Previous | Next » 

Revision 683

Added by berkley almost 24 years ago

fixed some problems with database connections getting a time out error. Also updated some catch statements

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
190 190
                                          HttpServletResponse response)
191 191
  {
192 192
    String subaction = ((String[])params.get("subaction"))[0];
193
    Connection conn = null;
193 194
    try
194 195
    {
195
      Connection conn = util.openDBConnection();
196
      conn = MetacatReplication.getDBConnection("MetacatReplication." + 
197
                                                "handleServerControlRequest");
196 198
      PreparedStatement pstmt = null;
197 199
      if(subaction.equals("add"))
198 200
      {
......
331 333
      String user = (String)docinfoHash.get("user_owner");
332 334
      String group = new String(user);
333 335
      //right now the user and group are the same.
334
      Connection conn = util.openDBConnection();
336
      Connection conn = null;
337
      
338
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
339
                                                "handleForceReplicateRequest");
340
    
335 341
      DocumentImpl.write(conn, new StringReader(xmldoc), null, dbaction, docid, 
336 342
                         user, group, serverCode, override);
337 343
      MetacatReplication.replLog("document " + docid + " added to DB with " +
......
355 361
  private void handleGetLockRequest(PrintWriter out, Hashtable params,
356 362
                                    HttpServletResponse response)
357 363
  {
364
    Connection conn = null;
358 365
    try
359 366
    {
360
      Connection conn = util.openDBConnection();
367
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
368
                                                "handleGetLockRequest");
361 369
      String docid = ((String[])params.get("docid"))[0];
362 370
      String remoteRev = ((String[])params.get("updaterev"))[0];
363 371
      DocumentImpl requestDoc = new DocumentImpl(conn, docid);
......
413 421
  {
414 422
    String docid = ((String[])(params.get("docid")))[0];
415 423
    StringBuffer sb = new StringBuffer();
424
    Connection conn = null;
416 425
    try
417 426
    {
418
      Connection conn = util.openDBConnection();
427
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
428
                                                "handleGetDocumentInfoRequest");
419 429
      DocumentImpl doc = new DocumentImpl(conn, docid);
420 430
      sb.append("<documentinfo><docid>").append(docid);
421 431
      sb.append("</docid><docname>").append(doc.getDocname());
......
445 455
  private void handleGetDocumentRequest(PrintWriter out, Hashtable params, 
446 456
                                        HttpServletResponse response)
447 457
  {
458
    Connection conn = null;
448 459
    try
449 460
    {
450 461
      String docid = ((String[])(params.get("docid")))[0];
451
      MetaCatUtil.debugMessage("incoming get request for document: " + docid);
452
      Connection conn = util.openDBConnection();
462
      System.out.println("incoming get request for document: " + docid);
463
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
464
                                                "handleGetDocumentRequest");
453 465
      DocumentImpl di = new DocumentImpl(conn, docid);
454 466
      response.setContentType("text/xml");
455 467
      out.print(di.toString());
......
461 473
    {
462 474
      System.out.println("error getting document from MetacatReplication." +
463 475
                         "handlGetDocumentRequest " + e.getMessage());
476
      e.printStackTrace(System.out);
464 477
    }
465 478
    
466 479
  }
......
482 495
  private void handleUpdateRequest(PrintWriter out, Hashtable params, 
483 496
                                    HttpServletResponse response)
484 497
  {
498
    Connection conn = null;
485 499
    try
486 500
    {
487 501
      System.out.println("received update request");
......
504 518
      delsql.append("xml_revisions where docid not in (select docid from ");
505 519
      delsql.append("xml_documents) and server_location = 1");
506 520
      
507
      Connection conn = util.openDBConnection();
521
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
522
                                                "handleUpdateRequest");
508 523
      PreparedStatement pstmt = conn.prepareStatement(docsql.toString());
509 524
      pstmt.execute();
510 525
      ResultSet rs = pstmt.getResultSet();
511 526
      boolean tablehasrows = rs.next();
512 527
      while(tablehasrows)
513 528
      {
514
        if(!rs.getString(3).equals(util.getOption("packagedoctype")))
515
        { //if this is a package file, put it at the end
516
          //because if a package file is read before all of the files it
517
          //refers to are loaded then there is an error
518
          doclist.append("<updatedDocument>");
519
          doclist.append("<docid>").append(rs.getString(1));
520
          doclist.append("</docid><rev>").append(rs.getInt(2));
521
          doclist.append("</rev>");
522
          doclist.append("</updatedDocument>");
529
        String recordDoctype = rs.getString(3);
530
        if(!recordDoctype.equals("BIN"))
531
        { //don't replicate data files
532
          if(!recordDoctype.equals(util.getOption("packagedoctype")))
533
          { //if this is a package file, put it at the end
534
            //because if a package file is read before all of the files it
535
            //refers to are loaded then there is an error
536
            doclist.append("<updatedDocument>");
537
            doclist.append("<docid>").append(rs.getString(1));
538
            doclist.append("</docid><rev>").append(rs.getInt(2));
539
            doclist.append("</rev>");
540
            doclist.append("</updatedDocument>");
541
          }
542
          else
543
          { //the package files are saved to be put into the xml later.
544
            Vector v = new Vector();
545
            v.add(new String(rs.getString(1)));
546
            v.add(new Integer(rs.getInt(2)));
547
            packageFiles.add(new Vector(v));
548
          }
523 549
        }
524
        else
525
        { //the package files are saved to be put into the xml later.
526
          Vector v = new Vector();
527
          v.add(new String(rs.getString(1)));
528
          v.add(new Integer(rs.getInt(2)));
529
          packageFiles.add(new Vector(v));
530
        }
531 550
        tablehasrows = rs.next();
532 551
      }
533 552
      
......
599 618
    Connection conn = null;
600 619
    PreparedStatement pstmt = null;
601 620
    try
602
    {
603
      conn = util.openDBConnection();
621
    { 
622
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
623
                                                "handleGetCatalogRequest");
604 624
      pstmt = conn.prepareStatement("select entry_type, " +
605 625
                              "source_doctype, target_doctype, public_id, " +
606 626
                              "system_id from xml_catalog");
......
702 722
  public static String getServer(int serverCode)
703 723
  {
704 724
    //System.out.println("serverid: " + serverCode);
725
    Connection conn = null;
705 726
    try
706 727
    {
707
      Connection conn = util.openDBConnection();
728
      conn = MetacatReplication.getDBConnection("MetacatReplication." +
729
                                                "getServer");
708 730
      String sql = new String("select server from " +
709 731
                              "xml_replication where serverid = " + 
710 732
                              serverCode);
......
742 764
    PreparedStatement pstmt = null;
743 765
    try
744 766
    {
745
      conn = util.openDBConnection();
767
      conn = MetacatReplication.getDBConnection("MetacatReplication.getServerCode");
746 768
      pstmt = conn.prepareStatement("select serverid from " +
747 769
                                         "xml_replication where server " +
748 770
                                         "like '" + server + "'");
......
838 860
    PreparedStatement pstmt = null;
839 861
    try
840 862
    {
841
      conn = util.openDBConnection();
863
      conn = MetacatReplication.getDBConnection("MetacatReplication.replToServer");
842 864
      pstmt = conn.prepareStatement("select replicate from " + 
843 865
                                    "xml_replication where server like '" +
844 866
                                     server + "'");
......
880 902
    return false;
881 903
    //the default if this server does not exist is to not replicate to it.
882 904
  }
905
  
906
  /**
907
   * A method for redundantly trying to connect.  this method will attempt to 
908
   * connect to the DB 3 times before failing.
909
   * @param methodname the methodname from which this method is called.
910
   */
911
  public static Connection getDBConnection(String methodname) throws Exception
912
  {
913
    Connection conn = null;
914
    try
915
    {
916
      try
917
      { //this connection is prone to error for some reason so we 
918
        //try to connect it three times before quiting.
919
        conn = util.openDBConnection();
920
        return conn;
921
      }
922
      catch(SQLException sqle)
923
      {
924
        try
925
        {
926
          conn = util.openDBConnection();
927
          return conn;
928
        }
929
        catch(SQLException sqlee)
930
        {
931
          try
932
          {
933
            conn = util.openDBConnection();
934
            return conn;
935
          }
936
          catch(SQLException sqleee)
937
          {
938
            System.out.println("error getting db connection in " + 
939
                               methodname +  ": " +
940
                               sqleee.getMessage());
941
            return conn;
942
          }
943
        }
944
      }
945
    }
946
    catch(Exception e)
947
    {
948
      throw new java.lang.Exception("error in " + methodname + ": " + 
949
                          e.getMessage());
950
    }
951
  }
883 952
}
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
82 82
    //xml_documents with an update_date > last_checked
83 83
    try
84 84
    {
85
      conn = util.openDBConnection();
85
      try
86
      { //this connection is prone to error for some reason so we 
87
        //try to connect it three times before quiting.
88
        conn = util.openDBConnection();
89
      }
90
      catch(SQLException sqle)
91
      {
92
        try
93
        {
94
          conn = util.openDBConnection();
95
        }
96
        catch(SQLException sqlee)
97
        {
98
          try
99
          {
100
            conn = util.openDBConnection();
101
          }
102
          catch(SQLException sqleee)
103
          {
104
            System.out.println("error getting db connection in " + 
105
                               "ReplicationHandler.run: " +
106
                               sqleee.getMessage());
107
          }
108
        }
109
      }
86 110
      serverList = buildServerList(conn);
87
      update(serverList, conn);
88
      updateCatalog(serverList, conn);
111
      update(serverList);
112
      updateCatalog(serverList);
89 113
      conn.close();
90 114
    }
91 115
    catch (Exception e)
......
97 121
  /**
98 122
   * Method that uses revision taging for replication instead of update_date.
99 123
   */
100
  private void update(Hashtable serverList, Connection conn)
124
  private void update(Hashtable serverList)
101 125
  {
102 126
    /*
103 127
     Pseudo-algorithm
......
112 136
       (this info is theoretically not needed using this system but probably 
113 137
       should be kept anyway)
114 138
    */
139
    
140
    Connection conn = null;
141
    try
142
    {
143
      try
144
      { //this connection is prone to error for some reason so we 
145
        //try to connect it three times before quiting.
146
        conn = util.openDBConnection();
147
      }
148
      catch(SQLException sqle)
149
      {
150
        try
151
        {
152
          conn = util.openDBConnection();
153
        }
154
        catch(SQLException sqlee)
155
        {
156
          try
157
          {
158
            conn = util.openDBConnection();
159
          }
160
          catch(SQLException sqleee)
161
          {
162
            System.out.println("error getting db connection in " + 
163
                               "ReplicationHandler.update: " +
164
                               sqleee.getMessage());
165
          }
166
        }
167
      }
168
    }
169
    catch(Exception e)
170
    {
171
      System.out.println("error in ReplicationHandler.update: " + 
172
                          e.getMessage());
173
    }
174
    
115 175
    Enumeration keys;
116 176
    String server;
117 177
    String update;
......
285 345
  /**
286 346
   * updates xml_catalog with entries from other servers.
287 347
   */
288
  private void updateCatalog(Hashtable serverList, Connection conn)
348
  private void updateCatalog(Hashtable serverList)
289 349
  {
350
    Connection conn = null;
290 351
    try
291 352
    {
353
      try
354
      { //this connection is prone to error for some reason so we 
355
        //try to connect it three times before quiting.
356
        conn = util.openDBConnection();
357
      }
358
      catch(SQLException sqle)
359
      {
360
        try
361
        {
362
          conn = util.openDBConnection();
363
        }
364
        catch(SQLException sqlee)
365
        {
366
          try
367
          {
368
            conn = util.openDBConnection();
369
          }
370
          catch(SQLException sqleee)
371
          {
372
            System.out.println("error getting db connection in " + 
373
                               "ReplicationHandler.update: " +
374
                               sqleee.getMessage());
375
          }
376
        }
377
      }
292 378
      String server;
293 379
      Enumeration keys = serverList.keys();
294 380
      while(keys.hasMoreElements())
src/edu/ucsb/nceas/metacat/RelationHandler.java
43 43
    //this.conn = conn;
44 44
    try
45 45
    {
46
      this.conn = util.openDBConnection();
46
      conn = MetacatReplication.getDBConnection("RelationHandler." +
47
                                                "relationHandler");
47 48
    }
48 49
    catch(Exception e)
49 50
    {
50
      System.out.println("error opening connection in " + 
51
                         "relationHandler.relationHandler: " + e.getMessage());
51
      System.out.println("unable to get db connection in relationhandler." + 
52
                         "relationhandler: " + e.getMessage());
52 53
    }
53
    
54 54
    this.xmldoc = xmldoc;
55 55
    if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
56 56
    { //make sure this doctype is a package document then run the thread
......
62 62
  
63 63
  /**
64 64
   * The thread handler
65
   */
65
   */ 
66 66
  public void run()
67 67
  {
68
    Connection dbconn = null;
68 69
    String docid = xmldoc.getDocID();
69

  
70 70
    // deletes all of the relations with a docid of @docid.
71 71
    deleteRelations(docid);
72 72
    //pseudo-code algorithm
......
77 77
    //    add a new row to xml_relation that represents this new relation
78 78
    try
79 79
    {
80
      PreparedStatement pstmt = conn.prepareStatement(
80
      dbconn = MetacatReplication.getDBConnection("RelationHandler." +
81
                                                  "run");
82
      PreparedStatement pstmt = dbconn.prepareStatement(
81 83
                                QuerySpecification.printPackageSQL(docid));
82 84
      pstmt.execute();
83 85
      
......
103 105
          paramDocid = subject;
104 106
        }
105 107
        
106
        DocumentImpl subDoc = new DocumentImpl(conn, paramDocid);
108
        DocumentImpl subDoc = new DocumentImpl(dbconn, paramDocid);
107 109
        subjectDoctype = subDoc.getDoctype();
108 110
        String relationship = rs.getString(2);
109 111
        String object = rs.getString(3);
110 112
      
111 113
        //compare r to each relation in xml_relation
112 114
        
113
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
115
        pstmt = dbconn.prepareStatement("select subject, subdoctype, " +
114 116
                                      "relationship, object, objdoctype " +
115 117
                                      "from xml_relation");
116 118
        pstmt.execute();
......
139 141
            insertTransRelation.append(subject).append("', '");
140 142
            insertTransRelation.append(subjectDoctype).append("')");
141 143
            //System.out.println("sql1: " + insertTransRelation.toString());
142
            pstmt = conn.prepareStatement(insertTransRelation.toString());
144
            pstmt = dbconn.prepareStatement(insertTransRelation.toString());
143 145
            pstmt.execute(); 
144 146
            
145 147
            insertTransRelation = new StringBuffer();
......
155 157
            insertTransRelation.append(currentSub).append("', '");
156 158
            insertTransRelation.append(currentSubDoctype).append("')");
157 159
            //System.out.println("sql2: " + insertTransRelation.toString());
158
            pstmt = conn.prepareStatement(insertTransRelation.toString());
160
            pstmt = dbconn.prepareStatement(insertTransRelation.toString());
159 161
            pstmt.execute();
160 162
          }
161 163
          
......
177 179
            subDocid = (String)subMurlParams.get("docid");
178 180
            if(subMurl.getProtocol().equals("metacat"))
179 181
            {
180
              subDoc = new DocumentImpl(conn, subDocid);
182
              subDoc = new DocumentImpl(dbconn, subDocid);
181 183
              subDoctype = subDoc.getDoctype();
182 184
            }
183 185
          }
......
200 202
            objDocid = (String)objMurlParams.get("docid");
201 203
            if(objMurl.getProtocol().equals("metacat"))
202 204
            {
203
              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
205
              DocumentImpl objDoc = new DocumentImpl(dbconn, objDocid);
204 206
              objDoctype = objDoc.getDoctype();
205 207
            }
206 208
          }
......
228 230
        insertStmt.append(object).append("', '");
229 231
        insertStmt.append(objDoctype).append("')");
230 232
        
231
        pstmt = conn.prepareStatement(insertStmt.toString());
233
        pstmt = dbconn.prepareStatement(insertStmt.toString());
232 234
        pstmt.execute(); 
233 235
        
234 236
        hasmorerows = rs.next();
235 237
      }
236 238
      
237
      conn.commit();
239
      dbconn.commit();
238 240
      pstmt.close();
239
      conn.close();
241
      dbconn.close();
240 242
      btThread = null;
241 243

  
242 244
    } 
243 245
    catch(Exception e) 
244 246
    { 
247
      System.out.println("Error in relationHandler.run: " + e.getMessage());
248
      util.debugMessage("Error in relationHandler.run: " + e.getMessage());
249
      e.printStackTrace(System.out);
250
      btThread = null;
245 251
      try 
246 252
      { 
247 253
        conn.rollback();
248 254
      } 
249 255
      catch (SQLException sqle) {}
250
      System.out.println("Error in relationHandler.run: " + e.getMessage());
251
      util.debugMessage("Error in relationHandler.run: " + e.getMessage());
252
      e.printStackTrace(System.out);
253
      btThread = null;
254 256
    }
255 257
  }
256 258
  
......
260 262
   */
261 263
  public void deleteRelations(String docid)
262 264
  {
265
    Connection dbconn = null;
263 266
    try
264 267
    {
265
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
268
      dbconn = MetacatReplication.getDBConnection("RelationHandler."+
269
                                                  "deleteRelations");
270
    }
271
    catch(Exception ee)
272
    {
273
      System.out.println("error in relationHandler.deleteRelations: " +
274
                          ee.getMessage());
275
    }
276
    
277
    try
278
    {
279
      PreparedStatement pstmt = dbconn.prepareStatement("delete from " +
266 280
                             "xml_relation where docid like '" + docid + "'");
267 281
      pstmt.execute();
268 282
      pstmt.close();
283
      dbconn.close();
269 284
    }
270 285
    catch(Exception e)
271 286
    {
287
      System.out.println("error in RelationHandler.deleteRelations(): " + 
288
                          e.getMessage());
289
      e.printStackTrace(System.out);
272 290
      try 
273 291
      { 
274
        conn.rollback();
292
        dbconn.rollback();
293
        dbconn.close();
275 294
      } 
276 295
      catch (SQLException sqle) {}
277
      System.out.println("error in RelationHandler.deleteRelations(): " + 
278
                          e.getMessage());
279
      e.printStackTrace(System.out);
280 296
    }
281 297
    
282 298
  }
src/edu/ucsb/nceas/metacat/DocumentIdentifier.java
109 109
    
110 110
    try
111 111
    {
112
      conn = util.openDBConnection();
112
      try
113
      { //this connection is prone to error for some reason so we 
114
        //try to connect it three times before quiting.
115
        conn = util.openDBConnection();
116
      }
117
      catch(SQLException sqle)
118
      {
119
        try
120
        {
121
          conn = util.openDBConnection();
122
        }
123
        catch(SQLException sqlee)
124
        {
125
          try
126
          {
127
            conn = util.openDBConnection();
128
          }
129
          catch(SQLException sqleee)
130
          {
131
            System.out.println("error getting db connection in " + 
132
                               "MetacatReplication.handleGetDocumentRequest: " +
133
                               sqleee.getMessage());
134
          }
135
        }
136
      }
113 137
      pstmt = conn.prepareStatement("select rev from xml_documents where " +
114 138
                                    "docid like '" + docid + "'");
115 139
      pstmt.execute();
......
122 146
        conn.close();
123 147
        return retStr;
124 148
      }
149
      conn.close();
125 150
    }
126 151
    catch(Exception e)
127 152
    {
153
      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
154
                          e.getMessage());
128 155
      try
129 156
      {
130 157
        conn.close();
131 158
      }
132 159
      catch(Exception e2)
133 160
      {}
134
      System.out.println("error closing db connection in " + 
135
                         "DocumentIdentifier.getNewestRev " + 
136
                          e.getMessage());
137 161
    }
138 162
    return "1";
139 163
  }

Also available in: Unified diff