Ignore:
Timestamp:
2005-05-12T14:52:45+12:00 (19 years ago)
Author:
kjdon
Message:

OK, changed my mind about making SQLConnection kill off the previous statement.
To make it more transparent what is happening, you now have to create a Statement (connection.createStatement()), then use the Statement to execute the query. This means that the thing doing the query owns the Statement, and can kill it off when finished with it, and nothing else can kill it off unexpectedly. The previous way this was all implemented meant that there was a large memory leak, and some functionality actually relied on this. A newer version of the mysql connector/J has fixed the bug where the statement wasn't closed on garbage collection, but it still seems better to close it explicitly.
Hopefully I have got it all back to working as well as it was bfore, and haven't introduced any bugs :-)

Location:
branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptive.java

    r9851 r9858  
    88
    99import java.sql.SQLException;
     10import java.sql.Statement;
    1011import java.sql.ResultSet;
    1112
     
    275276        } else {
    276277        // TODO: raise an error!
    277         System.out.println("Error: METSDescriiptive: unrecognised tag "+childName);
     278        System.out.println("Error: METSDescriptive: unrecognised tag "+childName);
    278279        }
    279280    }
     
    375376    select.setWhere(where);
    376377   
    377     connection.execute(select.toString());
    378    
    379378    try {
     379        Statement statement = connection.createStatement();
     380        ResultSet resultSet = statement.executeQuery(select.toString());
    380381        GS3SQLAction action;
    381382       
    382         ResultSet resultSet = connection.getResultSet();
    383        
    384         if (resultSet != null &&
    385         resultSet.first()) {
     383        if (resultSet.first()) {
    386384        // the node already exists - no need to do anything as such
    387385        GS3SQLUpdate update = new GS3SQLUpdate("metadata");
     
    390388        }
    391389        else { // Set result set to null, just in case first() didn't work above...
    392         resultSet = null;
    393        
    394390        // It is a new node and needs writing
    395391        action = new GS3SQLInsert("metadata");
     
    403399       
    404400        // execute the action as required
    405         connection.execute(action.toString());
     401        statement.execute(action.toString());
    406402       
    407403        // get the resultSet again
     
    409405        // 'MetadataRef'
    410406        // System.out.println(select.toString());
    411         connection.execute(select.toString());
    412        
    413         resultSet = connection.getResultSet();
    414         resultSet.first();
    415        
    416        
    417         // get the reference for this item...
    418         sqlId = resultSet.getInt("MetadataRef");
    419         connection.closeStatement();
     407        resultSet = statement.executeQuery(select.toString());
     408       
     409        if (resultSet.first()) {
     410        // get the reference for this item...
     411        sqlId = resultSet.getInt("MetadataRef");
     412        }
     413        statement.close();
    420414    }
    421415    catch (SQLException sql){
    422         System.out.println(sql);
     416        System.err.println("METSDescriptive.writeSQL(): "+sql);
     417    }
     418    if (sqlId == -1) {
     419        return false;
    423420    }
    424421   
     
    470467        select.setWhere(where);
    471468       
    472         connection.execute(select.toString());
    473        
    474         // parse through the namespaces, calling the namespace class to create instances
    475         // as it will
    476         ResultSet namespaceSet = connection.getResultSet();
    477 
    478         // clone the connection cos we are still usign the old result set
    479         GS3SQLConnection cloned_connection = connection.cloneConnection();
    480 
    481         if (namespaceSet != null && namespaceSet.first()) {
     469        Statement statement = connection.createStatement();
     470        ResultSet namespaceSet = statement.executeQuery(select.toString());
     471       
     472        // parse through the namespaces, calling the namespace class to create instances as it will
     473        if (namespaceSet.first()) {
    482474        do {
    483             METSNamespace namespace = METSNamespace.readSQL(cloned_connection, namespaceSet);
     475            METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet);
    484476            if (namespace != null) {
    485477            descriptive.addNamespace(namespace);
    486478            }
    487479            else {
    488             System.out.println("Null namespace output");
     480            System.err.println("Null namespace output");
    489481            }
    490482        }
    491483        while (namespaceSet.next());
    492484        }
    493         cloned_connection.close();
    494         cloned_connection = null;
     485        statement.close();
    495486        return descriptive;
    496487    }
    497488    catch (SQLException sqlEx){
    498         System.out.println(sqlEx);
    499         sqlEx.printStackTrace();
     489        System.err.println("METSDescriptive.readSQL(): "+sqlEx);
    500490        System.exit(1);
    501491    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptiveSet.java

    r9851 r9858  
    99
    1010import java.sql.SQLException;
     11import java.sql.Statement;
    1112import java.sql.ResultSet;
    1213
     
    236237    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    237238    select.setWhere(new GS3SQLWhere(whereItem));
    238     connection.execute(select.toString());
    239    
    240     // clone the connection cos we are still using the old result set
    241     GS3SQLConnection cloned_connection = connection.cloneConnection();
    242239
    243240    // start going through the matching metadata blocks
    244241    try {
    245         ResultSet resultSet = connection.getResultSet();
    246         resultSet.first();
    247         do {
    248         METSDescriptive descriptive = METSDescriptive.readSQL(document, cloned_connection, resultSet);
    249         if (descriptive != null) {
    250             set.addDescriptive(descriptive);
    251         }
    252         else {
    253             System.out.println("Null descriptive");
    254         }
    255         } while (resultSet.next());
    256        
    257         connection.closeStatement();
     242        Statement statement = connection.createStatement();
     243        ResultSet resultSet = statement.executeQuery(select.toString());
     244        if (resultSet.first()) {
     245        do {
     246            METSDescriptive descriptive = METSDescriptive.readSQL(document, connection, resultSet);
     247            if (descriptive != null) {
     248            set.addDescriptive(descriptive);
     249            }
     250            else {
     251            System.out.println("Null descriptive");
     252            }
     253        } while (resultSet.next());
     254        }
     255        statement.close();
    258256    }
    259257    catch (SQLException sqlEx) {
    260         System.out.println(sqlEx);
    261         sqlEx.printStackTrace();
     258        System.err.println("METSDescriptiveSet.readSQL(): "+sqlEx);
    262259        System.exit(1);
    263260    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDivision.java

    r9851 r9858  
    1010
    1111import java.sql.SQLException;
     12import java.sql.Statement;
    1213import java.sql.ResultSet;
    1314
     
    475476    select.setWhere(where);
    476477   
    477     connection.execute(select.toString());
     478    Statement statement = null;
     479    ResultSet resultSet = null;
    478480   
    479481    // Do the actual writing
    480482    GS3SQLAction action;
    481483   
    482     ResultSet resultSet = connection.getResultSet();
    483484    try {
    484         if (resultSet == null ||
    485         !resultSet.first()) {
    486         if (resultSet != null) {
    487             resultSet.close();
    488             resultSet = null;
    489         }
     485        statement = connection.createStatement();
     486        resultSet = statement.executeQuery(select.toString());
     487        if (!resultSet.first()) {
    490488       
    491489        GS3SQLInsert insert = new GS3SQLInsert("divisions");
     
    517515    action.addValue("UserLabel", this.userLabel);
    518516   
    519     if (!connection.execute(action.toString())){
     517    try {
     518        statement.execute(action.toString());
     519    } catch (SQLException e) {
     520        System.err.println("METSDivision.writeSQL(): "+e);
    520521        return false;
    521522    }
    522523   
    523524    // if doing a fresh item, get the new structure reference...
    524     if (resultSet == null){
     525    if (sqlRef == -1){
    525526        // get the new structure reference
    526         connection.execute(select.toString());
    527        
    528         // get the sql reference for the division
    529527        try {
    530528        // read in the structure reference
    531         resultSet = connection.getResultSet();
    532         if (resultSet == null) {
    533             return false;
     529        resultSet = statement.executeQuery(select.toString());
     530        if (resultSet.first()) {
     531            sqlRef = resultSet.getInt("DivisionRef");
    534532        }
    535         resultSet.first();
    536         sqlRef = resultSet.getInt("DivisionRef");
    537         resultSet.close();
    538         resultSet = null;
    539533        }
    540534        catch (SQLException sqlex) {
    541         System.err.println("Unable to retrieve reference for Division " + sqlex);
     535        System.err.println("METSDIVISION.writeSQL(): Unable to retrieve reference for Division " + sqlex);
    542536        return false;
    543537        }
    544538    }
    545     // close the open resultSet, as we don't need it any longer...
    546     else {
    547         try {
    548         resultSet.close();
    549         }
    550         catch (SQLException sql) {
    551         System.err.println(sql + " " + select.toString());
    552         }
    553     }
    554539   
    555540    // delete the old file/metadata references
    556     GS3SQLWhere referenceWhere =
    557         new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
    558                         GS3SQLField.INTEGER_TYPE));
    559    
    560     GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs");
    561     delete.setWhere(referenceWhere);
    562     connection.execute(delete.toString());
    563    
    564     delete = new GS3SQLDelete("divisionmetarefs");
    565     delete.setWhere(referenceWhere);
    566     connection.execute(delete.toString());
    567    
    568     // write the new file references
    569     if (this.fileRefs.size() > 0){
    570         Iterator iterator = this.fileRefs.iterator();
    571        
    572         while (iterator.hasNext()) {
    573         GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
    574         fileinsert.addValue("DocID", docId.toString());
    575         fileinsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    576         fileinsert.addValue("DivisionType", "Group");
    577         fileinsert.addValue("FileID", iterator.next().toString());
    578         connection.execute(fileinsert.toString());
    579         }
    580     }
    581    
    582     // write the metadata references
    583     if (this.metadataRefs.size() > 0){
    584         Iterator iterator = this.metadataRefs.iterator();
    585        
    586         while (iterator.hasNext()) {
    587         GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
    588         metainsert.addValue("DocID", docId.toString());
    589         metainsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    590         metainsert.addValue("DivisionType", "Group");
    591         metainsert.addValue("MetaID", iterator.next().toString());
    592         connection.execute(metainsert.toString());
    593         }
    594     }
    595    
     541    try {
     542        GS3SQLWhere referenceWhere =
     543        new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
     544                            GS3SQLField.INTEGER_TYPE));
     545       
     546        GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs");
     547        delete.setWhere(referenceWhere);
     548       
     549        statement.execute(delete.toString());
     550       
     551        delete = new GS3SQLDelete("divisionmetarefs");
     552        delete.setWhere(referenceWhere);
     553        statement.execute(delete.toString());
     554   
     555        // write the new file references
     556        if (this.fileRefs.size() > 0){
     557        Iterator iterator = this.fileRefs.iterator();
     558       
     559        while (iterator.hasNext()) {
     560            GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
     561            fileinsert.addValue("DocID", docId.toString());
     562            fileinsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     563            fileinsert.addValue("DivisionType", "Group");
     564            fileinsert.addValue("FileID", iterator.next().toString());
     565            statement.execute(fileinsert.toString());
     566        }
     567        }
     568       
     569        // write the metadata references
     570        if (this.metadataRefs.size() > 0){
     571        Iterator iterator = this.metadataRefs.iterator();
     572       
     573        while (iterator.hasNext()) {
     574            GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
     575            metainsert.addValue("DocID", docId.toString());
     576            metainsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     577            metainsert.addValue("DivisionType", "Group");
     578            metainsert.addValue("MetaID", iterator.next().toString());
     579            statement.execute(metainsert.toString());
     580        }
     581        }
     582        statement.close();
     583    } catch (SQLException e) {
     584        System.err.println("METSDIVISION.writeSQL(): "+e);
     585        return false;
     586    }
    596587    // write out any children in turn
    597588    Iterator groups = this.children.values().iterator();
     
    635626        select.setWhere(where);
    636627
    637         connection.execute(select.toString());
     628        Statement statement = connection.createStatement();
     629        ResultSet childSet = statement.executeQuery(select.toString());
    638630     
    639631        // circulate through to obtain further children
    640         ResultSet childSet = connection.getResultSet();
    641         // clone the connection cos we are still usign the old result set
    642         GS3SQLConnection cloned_connection = connection.cloneConnection();
    643 
    644632        if (childSet.first())
    645633        {
    646634            do {
    647             METSDivision childDivision = METSDivision.readSQL(cloned_connection, childSet);
     635            METSDivision childDivision = METSDivision.readSQL(connection, childSet);
    648636            division.addDivision(childDivision);
    649637            }
    650638            while (childSet.next());
    651639        }
    652 
    653         cloned_connection.close();
    654         cloned_connection = null;
    655640        select = new GS3SQLSelect("divisionfilerefs");
    656641        select.addField("*");
     
    659644        select.setWhere(where);
    660645
    661         connection.execute(select.toString());
    662 
    663         ResultSet fileSet = connection.getResultSet();
    664         if (fileSet != null && fileSet.first()){
     646        ResultSet fileSet = statement.executeQuery(select.toString());
     647
     648        if (fileSet.first()){
    665649        do {
    666650            String reference = fileSet.getString("FileID");
     
    669653        while (fileSet.next()); 
    670654        }
    671 
     655       
    672656        select = new GS3SQLSelect("divisionmetarefs");
    673657        select.addField("*");
     
    676660        select.setWhere(where);
    677661
    678         connection.execute(select.toString());
    679 
    680         ResultSet metaSet = connection.getResultSet();
    681         if (metaSet != null && metaSet.first()){
     662        ResultSet metaSet = statement.executeQuery(select.toString());
     663
     664        if (metaSet.first()){
    682665        do {
    683666            String reference = metaSet.getString("MetaID");
     
    687670        }
    688671       
    689         connection.closeStatement();
     672        statement.close();
    690673        return division;
    691674    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFile.java

    r9851 r9858  
    1111import java.sql.ResultSet;
    1212import java.sql.SQLException;
     13import java.sql.Statement;
    1314
    1415import org.w3c.dom.Document;
     
    201202    where.add(whereItem);
    202203    select.setWhere(where);
    203     connection.execute(select.toString());
     204
     205    Statement statement = null;
    204206   
    205207    // if not, then make an insert action
    206208    try {
    207         ResultSet results = connection.getResultSet();
    208         if (results == null ||
    209         !results.first()){
     209        statement = connection.createStatement();
     210        ResultSet results = statement.executeQuery(select.toString());
     211        if (!results.first()){
    210212        GS3SQLInsert insert = new GS3SQLInsert("files");
    211213       
     
    223225    }
    224226    catch (SQLException ex){
    225         System.err.println(ex);
     227        System.err.println("METSFile.writeSQL(): "+ex);
    226228        return false;
    227229    }
     
    230232    action.addValue("MIMEType", this.MIMEType);
    231233   
    232     boolean return_val = connection.execute(action.toString());
    233     connection.closeStatement();
    234     return return_val;
     234    try {
     235        statement.execute(action.toString());
     236        statement.close();
     237       
     238    } catch (SQLException e) {
     239        System.err.println("METSFile.writeSQL():"+e);
     240        return false;
     241    }
     242    return true;
    235243    }
    236244   
     
    250258    }
    251259    catch (SQLException ex){
    252         System.out.println(ex);
     260        System.out.println("METSFile.readSQL(): "+ex);
    253261    }
    254262    catch (java.net.MalformedURLException urlEx){
    255         System.out.println(urlEx);
     263        System.out.println("METSFile.readSQL(): "+urlEx);
    256264    }
    257265    return null;
     
    311319    } catch (java.net.MalformedURLException ex) {
    312320        // TODO: raise error
    313         System.err.println(ex);
     321        System.err.println("METSFile.parse_flocateXML(): "+ex);
    314322    }
    315323    return thisFilePos;
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileGroup.java

    r9851 r9858  
    88
    99import java.sql.SQLException;
     10import java.sql.Statement;
    1011import java.sql.ResultSet;
    1112
     
    271272    {
    272273    int sqlId = -1;
    273    
    274274    // check if this node is in the database already
    275275    GS3SQLSelect select = new GS3SQLSelect("filegroups");
     
    281281    select.setWhere(where);
    282282   
    283     connection.execute(select.toString());
    284    
    285     ResultSet selectResult = connection.getResultSet();
     283    ResultSet results = null;
     284    Statement statement = null;
    286285   
    287286    try {
    288         if (selectResult == null ||
    289         !selectResult.first()) {
     287        statement = connection.createStatement();
     288        results = statement.executeQuery(select.toString());
     289        if (!results.first()) {
    290290        GS3SQLInsert insert = new GS3SQLInsert("filegroups");
    291291       
     
    295295        insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
    296296       
    297         if (!connection.execute(insert.toString())) {
    298             return false;
    299         }
     297        statement.execute(insert.toString());
    300298        }
    301299        else {
     
    304302    }
    305303    catch (SQLException ex){
    306         System.err.println(ex);
     304        System.err.println("METSFileGroup.writeSQL(): "+ex);
    307305        return false;
    308306    }
    309307   
    310308    // get the filegroup reference now
    311     connection.execute(select.toString());
    312    
    313309    try {
    314         ResultSet results = connection.getResultSet();
    315         if (results == null) {
     310        results = statement.executeQuery(select.toString());
     311        if (results.first()) {
     312        sqlId = results.getInt("FileGroupRef");
     313        } else {
     314        statement.close();
    316315        return false;
    317316        }
    318         results.first();
    319         sqlId = results.getInt("FileGroupRef");
    320     }
    321     catch (SQLException sqlex) {
    322         System.out.println(sqlex);
     317        statement.close();
     318    } catch (SQLException sqlex) {
     319        System.err.println("METSFileGroup.writeSQL(): "+sqlex);
    323320        return false;
    324321    }
    325    
    326322    // iterate over the child groups
    327323    Iterator childIter = childGroups.iterator();
     
    330326       
    331327        if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection)){
     328        System.err.println("METSFileGroup.writeSQL(): Couldn't write FileGroup");
    332329        return false;
    333330        }
     
    337334    childIter = children.iterator();
    338335    while (childIter.hasNext()){
    339         METSFile file = (METSFile) childIter.next();
    340        
     336        METSFile file = (METSFile) childIter.next();       
    341337        if (!file.writeSQL(sqlId, connection)){
     338        System.err.println("METSFileGroup.writeSQL(): Couldn't write File");
    342339        return false;
    343340        }
     
    371368        select.setWhere(where);
    372369       
    373         connection.execute(select.toString());
     370        Statement statement = connection.createStatement();
    374371       
    375372        // parse through the child groups
    376         ResultSet childSet = connection.getResultSet();
    377         // clone the connection cos we are still usign the old result set
    378         GS3SQLConnection cloned_connection = connection.cloneConnection();
     373        ResultSet childSet = statement.executeQuery(select.toString());
    379374        if (childSet.first()) {
    380375        do {
    381             METSFileGroup fileGroup = METSFileGroup.readSQL(document, cloned_connection, childSet);
     376            METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
    382377            if (fileGroup != null) {
    383378            group.addGroup(fileGroup);
     
    386381        while (childSet.next());
    387382        }
    388        
    389383        // now scan for file members
    390384        select = new GS3SQLSelect("files");
    391385        select.addField("*");
    392         whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
    393                         GS3SQLField.INTEGER_TYPE);
     386        whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef), GS3SQLField.INTEGER_TYPE);
    394387        where = new GS3SQLWhere(whereItem);
    395388        select.setWhere(where);
    396         connection.execute(select.toString());
    397        
    398         ResultSet childFileSet = connection.getResultSet();
    399         if (childFileSet != null && childFileSet.first()) {
     389        ResultSet childFileSet = statement.executeQuery(select.toString());
     390        if (childFileSet.first()) {
    400391        do {
    401             METSFile file = METSFile.readSQL(cloned_connection, childFileSet);
     392            METSFile file = METSFile.readSQL(connection, childFileSet);
    402393            if (file != null) {
    403394            group.addFile(file);
     
    406397        while (childFileSet.next());
    407398        }
    408         cloned_connection.close();
    409         cloned_connection = null;
     399        statement.close();
    410400        return group;
    411401    }
    412402    catch (SQLException sqlEx){
    413         System.out.println(sqlEx);
     403        System.err.println("METSFileGroup.readSQL(): "+sqlEx);
    414404        System.exit(1);
    415405    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java

    r9851 r9858  
    1616
    1717import java.sql.SQLException;
     18import java.sql.Statement;
    1819import java.sql.ResultSet;
    1920
     
    232233        insert.addValue("FileSecID", "test"); // TODO: remove magic string
    233234       
    234         if (!connection.execute(insert.toString())){
     235        Statement statement = null;
     236        try {
     237        statement = connection.createStatement();
     238        statement.execute(insert.toString());
     239        } catch (SQLException e) {
     240        System.err.println("METSFileSet.writeSQL(): "+e);
    235241        return false;
    236242        }
     
    246252       
    247253        try {
    248         connection.execute(select.toString());
    249        
    250         ResultSet set = connection.getResultSet();
    251         set.first();
    252         int sectionRef = set.getInt("FileSectionRef");
    253        
    254         this.reference = Integer.toString(sectionRef);
     254        ResultSet set = statement.executeQuery(select.toString());
     255        if (set.first()) {
     256            int sectionRef = set.getInt("FileSectionRef");
     257           
     258            this.reference = Integer.toString(sectionRef);
     259        }
     260        statement.close();
    255261        }
    256262        catch (SQLException ex){
    257         System.out.println(ex);
     263        System.out.println("METSFileSet.writeSQL(): "+ex);
    258264        return false;
    259265        }
    260266    }
    261267   
     268    if (this.reference == null) {
     269        return false;
     270    }
    262271    // write out the children
    263272    while (groups.hasNext()){
     
    281290    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    282291    select.setWhere(new GS3SQLWhere(whereItem));
    283     connection.execute(select.toString());
     292
     293    Statement statement = null;
    284294   
    285295    // Get the identifier for this file set, etc.
    286     ResultSet sections = connection.getResultSet();
    287     int fileSetRef;
     296    int fileSetRef = -1;
    288297    try {
    289         sections.first();
    290         fileSetRef     = sections.getInt("FileSectionRef");
    291         set.reference = Integer.toString(fileSetRef);
     298        statement = connection.createStatement();
     299        ResultSet sections = statement.executeQuery(select.toString());
     300        if (sections.first()) {
     301        fileSetRef = sections.getInt("FileSectionRef");
     302        set.reference = Integer.toString(fileSetRef);
     303        } else {
     304        statement.close();
     305        return null;
     306        }
     307       
    292308    }
    293309    catch (SQLException ex){
    294         System.out.println(ex);
     310        System.err.println("METSFileSet.readSQL() "+ex);
    295311        return null;
    296312    }
    297    
    298313    // Get child file groups
    299314    select = new GS3SQLSelect("filegroups");
     
    307322    where.add(whereItem);
    308323    select.setWhere(where);
    309     connection.execute(select.toString());
    310    
    311324    // start going through the matching file groups
    312     // clone the connection cos we are still usign the old result set
    313     GS3SQLConnection cloned_connection = connection.cloneConnection();
    314325    try {
    315         ResultSet resultSet = connection.getResultSet();
    316         resultSet.first();
    317         do {
    318         METSFileGroup filegroup = METSFileGroup.readSQL(document, cloned_connection, resultSet);
    319         if (filegroup != null) {
    320             set.addGroup(filegroup);
    321         }
    322         } while (resultSet.next());
    323 
    324         cloned_connection.close();
    325         cloned_connection = null;
     326        ResultSet resultSet = statement.executeQuery(select.toString());
     327        if (resultSet.first()) {
     328        do {
     329            METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
     330            if (filegroup != null) {
     331            set.addGroup(filegroup);
     332            }
     333        } while (resultSet.next());
     334        }
     335        statement.close();
    326336    }
    327337    catch (SQLException sqlEx) {
    328         System.out.println(sqlEx);
    329         sqlEx.printStackTrace();
     338        System.out.println("METSFileSet.readSQL(): "+sqlEx);
    330339        System.exit(1);
    331340    }
    332    
    333341    return set;
    334342    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSNamespace.java

    r8742 r9858  
    66import java.util.Iterator;
    77import java.sql.SQLException;
     8import java.sql.Statement;
    89import java.sql.ResultSet;
    910
     
    150151
    151152    // Execute the action
    152     connection.execute(action.toString());
    153 
     153    Statement statement = null;
     154    try {
     155        statement = connection.createStatement();
     156        statement.execute(action.toString());
     157    } catch (SQLException e) {
     158        System.err.println("METSNamespace.writeSQL(): "+e);
     159        return false;
     160    }
    154161    // then get the namespace reference number if needsbe...
    155162    if (this.id == null) {
     
    161168        where.add(where);
    162169        try {
    163         connection.execute(select.toString());
    164      
    165         ResultSet result = connection.getResultSet();
    166         result.last();
    167         this.id = Integer.toString(result.getInt("NamespaceRef"));
     170        ResultSet result  = statement.executeQuery(select.toString());
     171            if (result.last()) {
     172            this.id = Integer.toString(result.getInt("NamespaceRef"));
     173        }
     174        statement.close();
    168175        }
    169176        catch (SQLException sqlex){
    170177        this.id = null;
    171         System.err.println(sqlex);
     178        System.err.println("METSNamespace.writeSQL(): "+sqlex);
    172179        return false;
    173180        }
     
    201208        select.setWhere(where);
    202209
    203         connection.execute(select.toString());
    204      
    205         ResultSet valuesSet = connection.getResultSet();
    206         if (valuesSet != null && valuesSet.first()) {
     210        Statement statement = connection.createStatement();
     211        ResultSet valuesSet = statement.executeQuery(select.toString());
     212        if (valuesSet.first()) {
    207213        do {
    208214            String label = valuesSet.getString("Label");
     
    213219        while (valuesSet.next());
    214220        }
    215 
     221        statement.close();
    216222        return namespace;
    217223    }
    218224    catch (java.net.MalformedURLException urlEx){
    219         System.out.println(urlEx);
     225        System.err.println("METSNamespace.readSQL(): "+urlEx);
    220226    }
    221227    catch (SQLException sqlEx){
    222         System.out.println(sqlEx);
     228        System.err.println("METSNamespace.readSQL(): "+sqlEx);
    223229    }   
    224230    return null;
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructure.java

    r9851 r9858  
    1010
    1111import java.sql.SQLException;
     12import java.sql.Statement;
    1213import java.sql.ResultSet;
    1314
     
    180181    {
    181182    int sqlRef = -1;
    182     ResultSet results;
    183 
     183    ResultSet results = null;
     184    Statement statement = null;
     185   
    184186    // Prepare query to see if this structure has already been written
    185187    GS3SQLSelect select = new GS3SQLSelect("structure");
     
    190192    where.add(item);
    191193    select.setWhere(where);
    192 
     194   
    193195    // attempt to execute the query & get the current structure's reference
    194196    try {
    195         connection.execute(select.toString());
    196         results = connection.getResultSet();
    197         if (results != null &&
    198         results.first()){
     197        statement = connection.createStatement();
     198        results = statement.executeQuery(select.toString());
     199        if (results.first()){
    199200        sqlRef = results.getInt("structureRef");
    200201        }
    201         else {
    202         results = null;
    203         }
    204        
    205         if (results != null) {
    206         results.close();
    207         }
    208202    }
    209203    catch (SQLException sqlEx) {
    210         System.err.print(sqlEx);
     204        System.err.print("METSStructure.writeSQL(): "+sqlEx);
    211205        return false;
    212206    }
    213207   
    214208    // insert a new structure if it wasn't there previously
    215     if (results == null) {
     209    if (sqlRef == -1) {
    216210        GS3SQLInsert insert = new GS3SQLInsert("structure");
    217211        insert.addValue("DocID", document.getID().toString());
     
    220214        insert.addValue("Label", this.label);
    221215       
    222         if (!connection.execute(insert.toString())) {
     216        try {
     217        statement.execute(insert.toString());
     218       
     219        // get the new structure reference by re-running the original select object
     220        results = statement.executeQuery(select.toString());
     221        if (results.first()) {
     222            sqlRef = results.getInt("StructureRef");
     223        }
     224        } catch (SQLException sqlEx) {
     225        System.err.print("METSStructure.writeSQL(): "+sqlEx);
    223226        return false;
    224227        }
    225228       
    226         // get the new structure reference by re-running the original select object
    227         connection.execute(select.toString());
    228 
    229         try {
    230         results = connection.getResultSet();
    231         results.first();
    232         sqlRef = results.getInt("StructureRef");
    233         }
    234         catch (SQLException sql) {
    235         System.err.println(sql);
    236         return false;
    237         }
    238229    }
    239230    else {
     
    244235        update.addValue("Label", this.label);
    245236       
    246         connection.execute(update.toString());
    247     }
    248    
     237        try {
     238        statement.execute(update.toString());
     239        } catch (SQLException sqlEx) {
     240        System.err.print("METSStructure.writeSQL(): "+sqlEx);
     241        return false;
     242        }
     243    }
     244   
     245    // close the statement
     246    try {
     247        statement.close();
     248    } catch (SQLException e) {
     249        System.err.println("METSStructure.writeSQL(): "+e);
     250    }
    249251    // write out the child groups (Divisions) now...   
    250252    Iterator groups = this.children.values().iterator();
     
    296298        select.setWhere(where);
    297299
    298         connection.execute(select.toString());
     300        Statement statement = connection.createStatement();
     301        ResultSet divisionSet = statement.executeQuery(select.toString());
    299302
    300303        // parse through the divisions
    301         ResultSet divisionSet = connection.getResultSet();
    302         // use a new connection for the following bit, cos we still want to use results from the last execute
    303         GS3SQLConnection cloned_connection = connection.cloneConnection();
    304         if (divisionSet != null && divisionSet.first()) {
     304        if (divisionSet.first()) {
    305305        do {
    306             METSDivision division = METSDivision.readSQL(cloned_connection, divisionSet);
     306            METSDivision division = METSDivision.readSQL(connection, divisionSet);
    307307            if (division != null) {
    308308            structure.addDivision(division);
     
    311311        while (divisionSet.next());
    312312        }
    313         cloned_connection.close();
    314         cloned_connection = null;
     313        statement.close();
    315314        return structure;
    316315    }
    317     catch (SQLException sqlEx)
    318         { System.out.println(sqlEx + " " + select.toString());
    319         sqlEx.printStackTrace();
     316    catch (SQLException sqlEx) {
     317        System.err.println("METSStructure.readSQL(): "+ sqlEx + " " + select.toString());
    320318        System.exit(1);
    321         }
     319    }
    322320    return null;
    323321    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructureSet.java

    r9851 r9858  
    1010
    1111import java.sql.SQLException;
     12import java.sql.Statement;
    1213import java.sql.ResultSet;
    1314
     
    157158    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    158159    select.setWhere(new GS3SQLWhere(whereItem));
    159     connection.execute(select.toString());
    160     // clone the connection cos we are still usign the old result set
    161     GS3SQLConnection cloned_connection = connection.cloneConnection();
    162 
     160   
    163161    // start going through the matching metadata blocks
    164162    try {
    165         ResultSet resultSet = connection.getResultSet();
    166         resultSet.first();
    167         do {
    168         METSStructure structure = METSStructure.readSQL(document, cloned_connection, resultSet);
    169         if (structure != null) {
    170             set.addStructure(structure);
    171         }
    172         } while (resultSet.next());
    173         cloned_connection.close();
    174         cloned_connection = null;
     163        Statement statement = connection.createStatement();
     164        ResultSet resultSet = statement.executeQuery(select.toString());
     165        if (resultSet.first()) {
     166        do {
     167            METSStructure structure = METSStructure.readSQL(document, connection, resultSet);
     168            if (structure != null) {
     169            set.addStructure(structure);
     170            }
     171        } while (resultSet.next());
     172        }
     173        statement.close();
    175174    }
    176175    catch (SQLException sqlEx) {
    177         System.out.println(sqlEx);
    178         sqlEx.printStackTrace();
     176        System.err.println("METSStructureSet.readSQL(): "+sqlEx);
    179177        System.exit(1);
    180178    }
  • branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/SimpleNamespace.java

    r8742 r9858  
    99
    1010import java.sql.SQLException;
     11import java.sql.Statement;
    1112import java.sql.ResultSet;
    1213
     
    281282    }
    282283   
     284    Statement statement = null;
     285    ResultSet results = null;
    283286    try {
     287        statement = connection.createStatement();
    284288        if (this.id == null) {
    285289        GS3SQLSelect select = new GS3SQLSelect("namespaces");
    286290        select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE)));
    287291        select.addField("NamespaceRef");
    288         connection.execute(select.toString());
    289        
    290         ResultSet results = connection.getResultSet();
    291         results.first();
    292         sqlId = Integer.toString(results.getInt("NamespaceRef"));
     292        results = statement.executeQuery(select.toString());
     293        if (results.first()) {
     294            sqlId = Integer.toString(results.getInt("NamespaceRef"));
     295        } else {
     296            statement.close();
     297            return false;
     298        }
    293299        }
    294300        else {
     
    300306        GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", sqlId, GS3SQLField.INTEGER_TYPE));
    301307        delete.setWhere(where);
    302         connection.execute(delete.toString());
     308        statement.execute(delete.toString());
    303309       
    304310        // write out the metadata for this namespace
     
    316322            insert.addValue("Label", thisKey);
    317323            insert.addValue("Value", value);
    318             connection.execute(insert.toString());
    319         }
    320         }
     324            statement.execute(insert.toString());
     325        }
     326        }
     327        statement.close();
    321328    }
    322329    catch (SQLException sql) {
    323         System.out.println(sql);
     330        System.out.println("SimpleNamespace.writeSQL(): "+sql);
    324331    }
    325332    return true;
Note: See TracChangeset for help on using the changeset viewer.