Project

General

Profile

« Previous | Next » 

Revision 1063

Added by Jing Tao over 22 years ago

Fixed some DB connection bug.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
230 230
  {
231 231
    Connection dbConn = null;
232 232
    MetaCatUtil ut = new MetaCatUtil();
233
    dbConn = ut.getConnection();
234
    // get server location for this doc
235
    int serverLocation=getServerLocationNumber(dbConn,accnum);
236
    ut.returnConnection(dbConn);
237
    registerDocument(docname, doctype,accnum, user, serverLocation);
233
    try
234
    {
235
      dbConn = ut.openDBConnection();
236
      // get server location for this doc
237
      int serverLocation=getServerLocationNumber(dbConn,accnum);
238
      dbConn.close();
239
      registerDocument(docname, doctype,accnum, user, serverLocation);
240
    }
241
    catch (Exception e)
242
    {
243
      throw e;
244
    }
245
    finally
246
    {
247
      try
248
      {
249
        dbConn.close();
250
      }
251
      catch (Exception ee)
252
      {}
253
    }
238 254
    
239 255
  }
240 256
  /**
......
324 340
      pstmt.execute();
325 341
      pstmt.close();
326 342
      dbconn.close();
327
    } finally {
328
      util.returnConnection(dbconn);
343
    } 
344
    finally 
345
    {
346
      try
347
      {
348
        dbconn.close();
349
      }
350
      catch (SQLException sqlE)
351
      {
352
        util.debugMessage("Error in DocumentImpl.registerDocument:" +
353
                                sqlE.getMessage(), 30);
354
      }
329 355
    }    
330 356
  }
331 357
  
......
372 398
      //user specified rev should be great than 0
373 399
      if (revInDataBase==-1 && userSpecifyRev>0 )
374 400
      {
375
        //ture means for replication
401
       
376 402
        ac = new AccessionNumber(dbconn, accnum, "insert");
377 403
      }
378 404
      //rev is greater the last revsion number and revInDataBase isn't -1
......
420 446
      pstmt.execute();
421 447
      pstmt.close();
422 448
      dbconn.close();
423
    } finally {
424
      util.returnConnection(dbconn);
449
    } 
450
    finally 
451
    {
452
      try
453
      {
454
        dbconn.close();
455
      }
456
      catch (SQLException sqlE)
457
      {
458
        util.debugMessage("Error in DocumentImpl.registerDocument:" +
459
                                sqlE.getMessage());
460
      }
425 461
    }    
426 462
  }
427 463
  
......
442 478
   *        resides.
443 479
   */
444 480
 public static void writeDataFile(InputStream input, String filePath, 
445
    String docname, String doctype, String accnum, String user, int serverCode)
481
                                  String docname, String doctype, String accnum, 
482
                                  String user, String docHomeServer)
446 483
                     throws SQLException, AccessionNumberException, Exception
447 484
 {
485
   int serverCode=-2;
448 486
    if (filePath==null||filePath.equals(""))
449 487
    {
450 488
      throw new 
......
454 492
    {
455 493
      throw new Exception("Please specify the stored file name");
456 494
    }
495
    
496
    //get server code for the home server
497
    serverCode=getServerCode(docHomeServer);
498
    //if the server is not in the xml replication table, insert it.
499
    if (serverCode==-1)
500
    {
501
      insertServerIntoReplicationTable(docHomeServer);
502
      //get server code again
503
      serverCode=getServerCode(docHomeServer);
504
    }
505
    
457 506
    //make sure user have file lock grant(local metacat means have it too)
458 507
    if (getDataFileLockGrant(accnum))
459 508
    {
......
481 530
      bis.close();
482 531
	    outPut.close();
483 532
	    fos.close();
484
      //force replication to other server
485
      /*during replciation, don't need force replication again.
486
      MetaCatUtil util = new MetaCatUtil();
487
      if ((util.getOption("replicationdata")).equals("on"))
488
      {
489
        Connection dbConn = util.getConnection();
490
       
491
        if (serverCode==1)
492
        {
493
          
494
          ForceReplicationHandler frh = new ForceReplicationHandler(accnum,
495
                  "insert",false,ReplicationHandler.buildServerList(dbConn));
496
          util.returnConnection(dbConn);
497
        }
498
        else
499
        {
500
          
501
          if ((util.getOption("hub")).equals("super"))
502
          {
503
            ForceReplicationHandler frh = new ForceReplicationHandler(accnum, 
504
                true, ReplicationHandler.buildServerList(dbConn));
505
            util.returnConnection(dbConn);
506
          }
507
          else
508
          {
509
            
510
            String docId=util.getDocIdFromString(accnum);
511
            ForceReplicationHandler frh = new ForceReplicationHandler(accnum, 
512
                true, ReplicationHandler.getHomeServer(docId));
513
          }
514
        }
515
      }*/
533
    
516 534
    }//if
517 535
 }
518 536
  
......
523 541
  { 
524 542
    Connection dbConn = null;
525 543
    MetaCatUtil ut= new MetaCatUtil();
526
    dbConn = ut.getConnection();
527
    int serverLocation=getServerLocationNumber(dbConn,accnum);
528
    ut.returnConnection(dbConn);
529
    return getDataFileLockGrant(accnum,serverLocation);
544
    try
545
    {
546
      dbConn = ut.openDBConnection();
547
      int serverLocation=getServerLocationNumber(dbConn,accnum);
548
      dbConn.close();
549
      return getDataFileLockGrant(accnum,serverLocation);
550
    }
551
    catch (Exception e)
552
    {
553
      try
554
      {
555
        dbConn.close();
556
      }
557
      catch (Exception ee)
558
      {}
559
      throw e;
560
    }
530 561
  }
531 562
    
532 563
  /**
......
1758 1789
  /** Save a document entry in the xml_revisions table */
1759 1790
  private static void archiveDocRevision(Connection conn, String docid,
1760 1791
                                         String user) 
1761
                                         throws SQLException {
1792
 {
1762 1793
    String sysdate = dbAdapter.getDateTimeFunction();
1763 1794
    // create a record in xml_revisions table 
1764 1795
    // for that document as selected from xml_documents
1765
    PreparedStatement pstmt = conn.prepareStatement(
1796
   
1797
   try
1798
   {
1799
     PreparedStatement pstmt = conn.prepareStatement(
1766 1800
      "INSERT INTO xml_revisions " +
1767 1801
        "(docid, rootnodeid, docname, doctype, " +
1768 1802
        "user_owner, user_updated, date_created, date_updated, " +
......
1772 1806
        "server_location, rev, public_access, catalog_id " +
1773 1807
      "FROM xml_documents " +
1774 1808
      "WHERE docid = ?");
1775
    // Bind the values to the query and execute it
1776
    pstmt.setString(1, docid);
1777
    pstmt.setString(2, user);
1778
    pstmt.setString(3, docid);
1779
    pstmt.execute();
1780
    pstmt.close();
1809
      // Bind the values to the query and execute it
1810
      pstmt.setString(1, docid);
1811
      pstmt.setString(2, user);
1812
      pstmt.setString(3, docid);
1813
      pstmt.execute();
1814
      pstmt.close();
1815
   }
1816
   catch (SQLException e)
1817
   {
1818
   }
1819
   
1781 1820

  
1782 1821
  }
1783 1822
  
......
1889 1928
    try
1890 1929
    {
1891 1930
      //check xml_replication table
1892
      dbConn=util.getConnection();
1931
      dbConn=util.openDBConnection();
1893 1932
      pStmt = dbConn.prepareStatement
1894 1933
      ("SELECT serverid FROM xml_replication WHERE server='" + serverName +"'");
1895 1934
      pStmt.execute();
......
1918 1957
    {
1919 1958
      try
1920 1959
      {
1921
        util.returnConnection(dbConn);
1960
        dbConn.close();
1922 1961
      }
1923 1962
      catch (Exception ee)
1924 1963
      {}
......
1940 1979
    MetaCatUtil util = new MetaCatUtil();
1941 1980
    try
1942 1981
    {
1943
       dbConn=util.getConnection();
1982
       dbConn=util.openDBConnection();
1944 1983
       pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
1945 1984
                                      "(server, last_checked, replicate) " +
1946 1985
                                      "VALUES ('" + server + "', to_date(" +
......
1960 1999
      try
1961 2000
      {
1962 2001
        pStmt.close();
1963
        util.returnConnection(dbConn);
2002
        dbConn.close();
1964 2003
      }
1965 2004
      catch (Exception ee)
1966 2005
      {}

Also available in: Unified diff