Changeset 21321


Ignore:
Timestamp:
2009-12-09T17:04:54+13:00 (14 years ago)
Author:
xiao
Message:

fixed bug of derby db holding files handles, which causes on Windows platform in particular, collections cannot be deleted

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java

    r21303 r21321  
    2424    protected SQLStatements sqlState = null;
    2525    protected Connection connection = null;
    26     protected Statement statement = null;
    2726    protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
    2827
     
    5857
    5958    public boolean openConnection(String databasepath){
     59    if(connection != null) return true;
     60
    6061    connection = sqlServer.connect(databasepath);
    61    
    6262    if (connection == null) return false;
    63 
    6463    try{
    65         connection.setAutoCommit(false);   
    66         statement = connection.createStatement();
     64        connection.setAutoCommit(true);
    6765    }
    6866    catch(SQLException sqle){
     
    7472
    7573    public boolean openAndCreateConnection(String databasepath){
     74    if(connection != null) return true;
    7675        connection = sqlServer.connectAndCreate(databasepath);
    77 
     76   
    7877        if (connection == null) {
    7978            logger.error("sql connection is null. database path="+databasepath);
     
    8281
    8382        try{
    84             connection.setAutoCommit(true);
    85             //by passing the two arguments, the ResultSet returned from executeQuery is updatable
    86             statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    87             if (statement == null) {
    88                 logger.error("statement is null");
    89                 return false;
    90             }
    91 
    92         }
     83        connection.setAutoCommit(true);
     84    }
    9385        catch(SQLException sqle){
    9486            logger.debug("Database Error occured when creating a statement object",sqle);
     
    10395    ResultSet rs = null;
    10496    try{   
    105        
    106         if (statement == null){
    107             logger.info("the database hasn't been correct yet");
    108             return new ArrayList();
    109         }
    110        
    111         rs = statement.executeQuery(query_statement);
     97        //by passing the two arguments, the ResultSet returned from executeQuery is updatable
     98        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
     99       rs = statement.executeQuery(query_statement);
    112100        ResultSetMetaData rsmd = rs.getMetaData();
    113101        int numOfColumns = rsmd.getColumnCount();
     
    119107        results.add(arow); 
    120108        }
    121 
    122         rs.close();
    123        
     109        statement.close();
    124110    }
    125111    catch(SQLException sqle){
     
    140126    public synchronized ResultSet queryResultSet(String stat){
    141127        ResultSet rs = null;
     128    //by passing the two arguments, the ResultSet returned from executeQuery is updatable
     129
    142130        try{           
     131            Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    143132            if (statement == null){
    144133                logger.info("Null sql statement provided.");
     
    165154    public synchronized boolean execute(String stat) {
    166155        boolean rs;
     156
    167157        try{           
     158        Statement statement = connection.createStatement();
    168159            if (statement == null){
    169160                logger.info("statement is null.");
     
    173164            rs = statement.execute(stat);
    174165        statement.close();
    175         //          connection.commit();
    176         }
     166    }
    177167        catch(SQLException sqle){           
    178168            logger.debug("Database Error occured when execute query " + stat, sqle);
     
    190180    public synchronized boolean executeUpdate(String stat) {
    191181        int rs;
    192         try{           
     182    try{           
     183        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    193184            if (statement == null){
    194185                logger.info("statement is null.");
     
    199190            rs = statement.executeUpdate(stat);
    200191        statement.close();
    201 //          connection.commit();
    202192        logger.debug("sql stat="+stat+ " result="+rs);
    203193        }
     
    218208     */
    219209    public void check4Table(String stat) throws SQLException {
    220         statement.executeQuery(stat);
     210    Statement statement = connection.createStatement();
     211    statement.executeQuery(stat);
    221212    statement.close();
    222213    }
     
    224215    public void closeConnection(String databasepath){
    225216    try{
    226         statement.close();
    227         connection.close();
    228         sqlServer.disconnect(databasepath);
     217        if(connection != null){
     218        connection.close();
     219        connection = null;
     220        sqlServer.disconnect(databasepath);
     221        }
    229222    }
    230223    catch(SQLException sqle){
Note: See TracChangeset for help on using the changeset viewer.