Project

General

Profile

« Previous | Next » 

Revision 667

Added by berkley over 23 years ago

closed all preparedStatement variables

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
179 179
    try
180 180
    {
181 181
      Connection conn = util.openDBConnection();
182
      PreparedStatement pstmt;
182
      PreparedStatement pstmt = null;
183 183
      if(subaction.equals("add"))
184 184
      {
185 185
        String replicate = ((String[])params.get("replicate"))[0];
......
249 249
        }
250 250
        out.println("</table></body></html>");
251 251
      }
252
      pstmt.close();
252 253
      conn.close();
253 254
    }
254 255
    catch(Exception e)
......
538 539
      
539 540
      doclist.append("</updates></replication>");
540 541
      MetaCatUtil.debugMessage("doclist: " + doclist.toString());
542
      pstmt.close();
541 543
      conn.close();
542 544
      response.setContentType("text/xml");
543 545
      out.println(doclist.toString());
......
575 577
                                                HttpServletResponse response,
576 578
                                                boolean printFlag)
577 579
  {
580
    Connection conn = null;
581
    PreparedStatement pstmt = null;
578 582
    try
579 583
    {
580
      Connection conn = util.openDBConnection();
581
      PreparedStatement pstmt = conn.prepareStatement("select entry_type, " +
584
      conn = util.openDBConnection();
585
      pstmt = conn.prepareStatement("select entry_type, " +
582 586
                              "source_doctype, target_doctype, public_id, " +
583 587
                              "system_id from xml_catalog");
584 588
      pstmt.execute();
......
604 608
        response.setContentType("text/xml");
605 609
        out.println(sb.toString());
606 610
      }
611
      pstmt.close();
607 612
      return sb.toString();
608 613
    }
609 614
    catch(Exception e)
610 615
    {
616
      try
617
      {
618
        pstmt.close();
619
        conn.close();
620
      }
621
      catch(Exception ee)
622
      {}
611 623
      System.out.println("error in handleGetCatalogRequest: " + e.getMessage());
612 624
      e.printStackTrace(System.out);
613 625
    }
......
685 697
        //System.out.println("server: " + rs.getString(1));
686 698
        return rs.getString(1);
687 699
      }
700
      pstmt.close();
688 701
      conn.close();
689 702
    }
690 703
    catch(Exception e)
......
704 717
   */
705 718
  public static int getServerCode(String server) throws Exception
706 719
  {
720
    Connection conn = null;
721
    PreparedStatement pstmt = null;
707 722
    try
708 723
    {
709
      Connection conn = util.openDBConnection();
710
      PreparedStatement pstmt = conn.prepareStatement("select serverid from " +
724
      conn = util.openDBConnection();
725
      pstmt = conn.prepareStatement("select serverid from " +
711 726
                                         "xml_replication where server " +
712 727
                                         "like '" + server + "'");
713 728
      pstmt.execute();
......
715 730
      boolean tablehasrows = rs.next();
716 731
      int serverCode = 0;
717 732
      if(tablehasrows)
718
      {
719
         return rs.getInt(1);
733
      {  
734
        int ret = rs.getInt(1);
735
        pstmt.close();
736
        conn.close();
737
        return ret;
720 738
      }
721 739
      else
722 740
      {
741
        pstmt.close();
742
        conn.close();
723 743
        return 0;
724 744
      }
725 745
    }
......
727 747
    {
728 748
      throw e;
729 749
    }
750
    finally
751
    {
752
       try
753
       {
754
         pstmt.close();
755
         conn.close();
756
       }
757
       catch(Exception ee) {}
758
    }
730 759
  }
731 760
  
732 761
  /**
......
783 812
   */
784 813
  public static boolean replToServer(String server)
785 814
  {
815
    Connection conn = null;
816
    PreparedStatement pstmt = null;
786 817
    try
787 818
    {
788
      Connection conn = util.openDBConnection();
789
      PreparedStatement pstmt = conn.prepareStatement("select replicate from " + 
790
                                "xml_replication where server like '" +
791
                                server + "'");
819
      conn = util.openDBConnection();
820
      pstmt = conn.prepareStatement("select replicate from " + 
821
                                    "xml_replication where server like '" +
822
                                     server + "'");
792 823
      pstmt.execute();
793 824
      ResultSet rs = pstmt.getResultSet();
794 825
      boolean tablehasrows = rs.next();
......
797 828
        int i = rs.getInt(1);
798 829
        if(i == 1)
799 830
        {
831
          pstmt.close();
832
          conn.close();
800 833
          return true;
801 834
        }
802 835
        else
803 836
        {
837
          pstmt.close();
838
          conn.close();
804 839
          return false;
805 840
        }
806 841
      }
......
809 844
    {
810 845
      System.out.println("error in replToServer: " + e.getMessage());
811 846
    }
847
    finally
848
    {
849
      try
850
      {
851
        pstmt.close();
852
        conn.close();
853
      }
854
      catch(Exception ee)
855
      {}
856
    }
812 857
    return false;
813 858
    //the default if this server does not exist is to not replicate to it.
814 859
  }
src/edu/ucsb/nceas/metacat/DBQuery.java
134 134
  {
135 135
    //System.out.println("in finddocuments");
136 136
      Hashtable   docListResult = new Hashtable();
137
      PreparedStatement pstmt;
137
      PreparedStatement pstmt = null;
138 138
      String docid = null;
139 139
      String docname = null;
140 140
      String doctype = null;
......
144 144
      String fieldname = null;
145 145
      String fielddata = null;
146 146
      String relation = null;
147
      Connection dbconn = null;
147 148
      int rev = 0;
148 149
      StringBuffer document = null; 
149 150
      Vector returndocVec = new Vector();
......
157 158
      }
158 159
      
159 160
      try {
161
        dbconn = util.openDBConnection();
160 162
        // Get the XML query and covert it into a SQL statment
161 163
        QuerySpecification qspec = new QuerySpecification(xmlquery, 
162 164
                                   parserName, 
163 165
                                   util.getOption("accNumSeparator"));
164 166
       // System.out.println(qspec.printSQL());
165
        pstmt = conn.prepareStatement( qspec.printSQL() );
167
        pstmt = dbconn.prepareStatement( qspec.printSQL() );
166 168

  
167 169
        // Execute the SQL query using the JDBC connection
168 170
        pstmt.execute();
169 171
        ResultSet rs = pstmt.getResultSet();
170 172
        boolean tableHasRows = rs.next();
171
        while (tableHasRows) {
173
        while (tableHasRows) 
174
        {
172 175
          docid = rs.getString(1);
173
          if ( !hasPermission(conn, user, group, docid) ) {
176
          if ( !hasPermission(dbconn, user, group, docid) ) {
174 177
            // Advance to the next record in the cursor
175 178
            tableHasRows = rs.next();
176 179
            continue;
......
181 184
          createDate = rs.getString(5);
182 185
          updateDate = rs.getString(6);
183 186
          rev = rs.getInt(7);
187
          
184 188
          //System.out.println("vec.size = " + returndocVec.size());
185 189
          if(returndocVec.size() != 0 && !returndocVec.contains(doctype))
186 190
          { //there are returndocs to match (backtracking can now be performed). 
......
203 207
            //btBuf.append("?docid=").append(docid).append("'");
204 208
            btBuf.append("%docid=").append(docid).append("'");
205 209
            //System.out.println("sql: " + btBuf.toString());
206
            pstmt = conn.prepareStatement(btBuf.toString());
210
            
211
            pstmt = dbconn.prepareStatement(btBuf.toString());
207 212
            pstmt.execute();
208 213
            ResultSet btrs = pstmt.getResultSet();
209 214
            boolean hasBtRows = btrs.next();
......
214 219
              MetacatURL objURL = new MetacatURL(btrs.getString(1));
215 220
              try
216 221
              {
217
                xmldoc = new DocumentImpl(conn, objURL.getParam(0)[1]);
222
                xmldoc = new DocumentImpl(dbconn, objURL.getParam(0)[1]);
218 223
              }
219 224
              catch(Exception e)
220 225
              {
......
262 267

  
263 268
          // Advance to the next record in the cursor
264 269
          tableHasRows = rs.next();
270
          System.out.println("next1");
265 271
        }
272
        System.out.println("rs1.close");
273
        rs.close();
266 274
        
267 275
        if(qspec.containsExtendedSQL())
268 276
        {
......
277 285
            doclist.append("',");
278 286
          }
279 287
          doclist.deleteCharAt(doclist.length()-1); //remove the last comma
280
          pstmt = conn.prepareStatement(qspec.printExtendedSQL(
288
          pstmt.close();
289
          pstmt = dbconn.prepareStatement(qspec.printExtendedSQL(
281 290
                                        doclist.toString()));
282 291
          pstmt.execute();
283 292
          rs = pstmt.getResultSet();
......
285 294
          while(tableHasRows) 
286 295
          {
287 296
            docid = rs.getString(1);
288
            if ( !hasPermission(conn, user, group, docid) ) {continue;}
297
            if ( !hasPermission(dbconn, user, group, docid) ) {continue;}
289 298
            fieldname = rs.getString(2);
290 299
            fielddata = rs.getString(3);
291 300
            
......
298 307
            document.append("</param>");
299 308

  
300 309
            tableHasRows = rs.next();
310
            System.out.println("next2");
301 311
            if(docListResult.containsKey(docid))
302 312
            {
303 313
              String removedelement = (String)docListResult.remove(docid);
......
308 318
              docListResult.put(docid, document.toString()); 
309 319
            }
310 320
          }
321
          System.out.println("rs2.close");
322
          rs.close();
311 323
        }
312 324

  
313 325
        //this loop adds the relation data to the resultdoc
......
320 332
          String docidkey = (String)docidkeys.nextElement();
321 333
          //System.out.println("relationsql: " + qspec.printRelationSQL(
322 334
          //                                           connstring + docidkey));
323
          pstmt = conn.prepareStatement(
335
          pstmt.close();
336
          pstmt = dbconn.prepareStatement(
324 337
                  qspec.printRelationSQL(connstring + docidkey));
325 338
          pstmt.execute();
326 339
          rs = pstmt.getResultSet();
......
355 368
              }
356 369
            }
357 370
            tableHasRows = rs.next();
371
            System.out.println("next3");
358 372
          }
373
          rs.close();
374
          System.out.println("rs3.close");
375
          System.out.println("pstmt.close()");
376
          pstmt.close();
359 377
        }
360
        pstmt.close();
378
        
361 379
      } catch (SQLException e) {
362
        System.err.println("Error getting id: " + e.getMessage());
380
        System.err.println("SQL Error in DBQuery.findDocuments: " + 
381
                           e.getMessage());
363 382
      } catch (IOException ioe) {
364 383
        System.err.println("Error printing qspec:");
365 384
        System.err.println(ioe.getMessage());
385
      } catch (Exception ee) {
386
        System.out.println("error in DBQuery.findDocuments: " + 
387
                           ee.getMessage());
366 388
      }
389
      finally {
390
        try
391
        {
392
          dbconn.close();
393
        }
394
        catch(SQLException sqle)
395
        {
396
          System.out.println("error closing conn in DBQuery.findDocuments");
397
        }
398
      }
367 399
    //System.out.println("docListResult: ");
368 400
    //System.out.println(docListResult.toString());
369 401
    return docListResult;
......
382 414
  {
383 415
    StringBuffer query = new StringBuffer();
384 416
    Vector result = new Vector();
385
    PreparedStatement pstmt;
417
    PreparedStatement pstmt = null;
386 418
    query.append("select nodedata from xml_nodes where parentnodeid in ");
387 419
    query.append("(select nodeid from xml_index where path like '");
388 420
    query.append(nodename);
......
405 437
    catch (SQLException e) 
406 438
    {
407 439
      System.err.println("Error getting id: " + e.getMessage());
408
    } 
409
    
440
    } finally {
441
      try
442
      {
443
        pstmt.close();
444
      }
445
      catch(SQLException sqle) {}
446
    }
410 447
    return result.toArray();
411 448
  }
412 449
  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1036 1036
      }
1037 1037
      
1038 1038
      fin.close();
1039
      pstmt.close();
1039 1040
      conn.close();
1040 1041
    }
1041 1042
    catch(Exception e)
src/edu/ucsb/nceas/metacat/DataFileUploadInterface.java
136 136
      Connection conn = util.openDBConnection();
137 137
      PreparedStatement pstmt = conn.prepareStatement(sql.toString());
138 138
      pstmt.execute();
139
      pstmt.close();
139 140
      conn.close();
140 141
    }
141 142
    catch(Exception e)
src/edu/ucsb/nceas/metacat/DocumentImpl.java
413 413
    if(tablehasrows)
414 414
    {
415 415
      int r = rs.getInt(1);
416
      pstmt.close();
416 417
      if(new Integer(rev).intValue() == r)
417 418
      { //the current revision in in xml_documents
418 419
        //System.out.println("returning false");
......
1025 1026
      parser = XMLReaderFactory.createXMLReader(parserName);
1026 1027

  
1027 1028
      // Turn on validation
1028
      parser.setFeature("http://xml.org/sax/features/validation", true);
1029
      parser.setFeature("http://xml.org/sax/features/validation", false);
1029 1030
      // Turn off Including all external parameter entities
1030 1031
      // (the external DTD subset also)
1031 1032
      // Doesn't work well, probably the feature name is not correct
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
103 103
    String update;
104 104
    ReplMessageHandler message = new ReplMessageHandler();
105 105
    Vector responses = new Vector();
106
    PreparedStatement pstmt;
106
    PreparedStatement pstmt = null;
107 107
    ResultSet rs;
108 108
    boolean tablehasrows;
109 109
    boolean flag=false;
......
239 239
          sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
240 240
          sql.append("server like '").append(server).append("'");
241 241
          //System.out.println("sql: " + sql.toString());
242
          pstmt.close();
242 243
          pstmt = conn.prepareStatement(sql.toString());
243 244
          pstmt.executeUpdate();
244 245
          //conn.commit();
......
252 253
      System.out.println("error in update2: " + e.getMessage());
253 254
      e.printStackTrace(System.out);
254 255
    }
256
    finally
257
    {
258
      try
259
      {
260
        pstmt.close();
261
      }
262
      catch(Exception ee) {}
263
    }
255 264
  }
256 265
  
257 266
  /**
......
321 330
            pstmt.setString(4, (String)v.elementAt(3));
322 331
            pstmt.setString(5, (String)v.elementAt(4));
323 332
            pstmt.execute();
333
            pstmt.close();
324 334
          }
325 335
        }
326 336
      }
......
365 375
      sb = new StringBuffer();
366 376
      sb.append("select docid from xml_documents where docid like '");
367 377
      sb.append(docid).append("'");
378
      pstmt.close();
368 379
      pstmt = conn.prepareStatement(sb.toString());
369 380
      pstmt.execute();
370 381
      rs = pstmt.getResultSet();
371 382
      tablehasrows = rs.next();
383
      pstmt.close();
372 384
      if(tablehasrows)
373 385
      {
374 386
        xml_docs = true;
......
396 408
      System.out.println("error in alreadyDeleted: " + e.getMessage());
397 409
      e.printStackTrace(System.out);
398 410
    }
411
    finally
412
    {
413
      
414
    }
399 415
    return false;
400 416
  }
401 417
  
......
418 434

  
419 435
      if(rs.next())
420 436
      {
437
        pstmt.close();
421 438
        conn.close();
422 439
        return "UPDATE";
423 440
      }
424 441
      else
425 442
      {
443
        pstmt.close();
426 444
        conn.close();
427 445
        return "INSERT";
428 446
      }
......
464 482
        }
465 483
        tableHasRows = rs.next();   
466 484
      }
485
      pstmt.close();
467 486
    }
468 487
    catch(Exception e)
469 488
    {
src/edu/ucsb/nceas/metacat/RelationHandler.java
102 102
        String object = rs.getString(3);
103 103
      
104 104
        //compare r to each relation in xml_relation
105
        pstmt.close();
105 106
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
106 107
                                      "relationship, object, objdoctype " +
107 108
                                      "from xml_relation");
src/edu/ucsb/nceas/metacat/DocumentIdentifier.java
104 104
      if(tablehasrows)
105 105
      {
106 106
        String retStr = rs.getString(1);
107
        pstmt.close();
107 108
        conn.close();
108 109
        return retStr;
109 110
      }
src/edu/ucsb/nceas/metacat/AccessControlList.java
430 430
  private void insertPermissions( String permType ) 
431 431
          throws SQLException 
432 432
  {
433
    PreparedStatement pstmt;
433
    PreparedStatement pstmt = null;
434 434
 
435 435
    try {
436 436
      pstmt = conn.prepareStatement(
......
461 461
    } catch (SQLException e) {
462 462
      throw new 
463 463
      SQLException("AccessControlList.insertPermissions(): " + e.getMessage());
464
    } finally {
465
      pstmt.close();
464 466
    }
465 467
  }
466 468

  

Also available in: Unified diff