Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Firebird Open Source J2EE Connector - JDBC Driver
   * 
   * Copyright (C) All Rights Reserved.
   * 
   * This file was created by members of the firebird development team.
   * All individual contributions remain the Copyright (C) of those
   * individuals.  Contributors to this file are either listed here or
   * can be obtained from a CVS history command.
  * 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *  
  *   - Redistributions of source code must retain the above copyright 
  *     notice, this list of conditions and the following disclaimer.
  *   - Redistributions in binary form must reproduce the above 
  *     copyright notice, this list of conditions and the following 
  *     disclaimer in the documentation and/or other materials provided 
  *     with the distribution.
  *   - Neither the name of the firebird development team nor the names
  *     of its contributors may be used to endorse or promote products 
  *     derived from this software without specific prior written 
  *     permission.
  * 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  * SUCH DAMAGE.
  */
 package org.firebirdsql.management;
 
 import java.util.List;
 
 
The FBMaintenanceManager class is responsible for replicating the functionality provided by the gfix command-line tool. Among the responsibilities of this class are:
  • Database shutdown
  • Extended database shutdown/online modes new with Firebird 2.5
  • Changing database mode to read-only or read-write
  • Enabling or disabling forced writes in the database
  • Changing the dialect of the database
  • Setting the cache size at database-level
  • Mending databases and making minor repairs
  • Sweeping databases
  • Activating and killing shadow files
  • Displaying, committing, or recovering limbo transactions

Author(s):
Gabriel Reid
Thomas Steinmaurer
 
 public class FBMaintenanceManager extends FBServiceManager 
                                 implements MaintenanceManager {


    
Create a new instance of FBMaintenanceManager based on the default GDSType.
 
     public FBMaintenanceManager()
     {
     	super();
     }

    
Create a new instance of FBMaintenanceManager based on a given GDSType.

Parameters:
gdsType type must be PURE_JAVA, EMBEDDED, or NATIVE
 
     public FBMaintenanceManager(String gdsType)
     {
     	super(gdsType);
     }

    
Create a new instance of FBMaintenanceManager based on a given GDSType.

Parameters:
gdsType The GDS implementation type to use
    public FBMaintenanceManager(GDSType gdsType){
        super(gdsType);
    }

    
Set the database to have read-write or read-only access.

Parameters:
mode Must be either ACCESS_MODE_READ_WRITE or ACCESS_MODE_READ_ONLY
Throws:
java.sql.SQLException if a database access error occurs
    public void setDatabaseAccessMode(int modethrows SQLException {
        if (mode !=  && mode != ){
            throw new IllegalArgumentException("mode must be one of "
                    + "ACCESS_MODE_READ_WRITE or ACCESS_MODE_READ_ONLY");
        }
        srb.addArgument(., (byte)mode);
        executeServicesOperation(srb);
    }

    
Set the database's dialect.

Parameters:
dialect The database dialect, must be either 1 or 3
Throws:
java.sql.SQLException if a database access error occurs
    public void setDatabaseDialect(int dialectthrows SQLException {
        if (dialect != 1 && dialect != 3){
            throw new IllegalArgumentException("dialect must be either "
                    + "1 or 3");
        }
        executeServicesOperation(srb);
    }

    
Set the default page-buffer count to be cached in the database. The Firebird default is 2048.

Parameters:
pageCount The number of pages to be cached, must be positive
Throws:
java.sql.SQLException If the given page count cannot be set, or a database access error occurs
    public void setDefaultCacheBuffer(int pageCountthrows SQLException {
        if (pageCount < 1){
            throw new IllegalArgumentException("page count must be positive");
        }
        srb.addArgument(.pageCount);
        executeServicesOperation(srb);
    }


    
Enable or disable forced (synchronous) writes in the database. Note, it is considered to be a very bad idea to use buffered writing on Windows platforms.

Parameters:
forced If true, forced writes will be used in the database, otherwise buffered writes will be used.
Throws:
java.sql.SQLException if a database access error occurs
    public void setForcedWrites(boolean forcedthrows SQLException {
                (byte) (forced ?  . 
                    : .));
        executeServicesOperation(srb);
    }

 
    
Set the page fill strategy for when inserting records. pageFill can be one of:
  • PAGE_FILL_FULL Fully fill database pages
  • PAGE_FILL_RESERVE Reserve 20% of page space for later record deltas

Parameters:
pageFill The page-filling strategy, either PAGE_FILL_FULL or PAGE_FILL_RESERVE
Throws:
java.sql.SQLException if a database access error occurs
    public void setPageFill(int pageFillthrows SQLException {
        if (pageFill !=  && pageFill != ){
            throw new IllegalArgumentException"Page fill must be either "
                    + "PAGE_FILL_FULL or PAGE_FILL_RESERVE");
        }
                        (byte)pageFill);
        executeServicesOperation(srb);
    }
    //----------- Database Shutdown -------------------

    
Shutdown the current database. Shutdown can be done in three modes:
  • SHUTDOWN_ATTACH - No new non-owner connections will be allowed to the database during the shutdown, and shutdown is cancelled if there are still processes connected at the end of the timeout.
  • SHUTDOWN_TRANSACTIONAL - No new transactions can be started during the timeout period, and shutdown is cancelled if there are still active transactions at the end of the timeout.
  • SHUTDOWN_FORCE - Forcefully shuts down the database at the end of the timeout.

Parameters:
shutdownMode One of SHUTDOWN_ATTACH, SHUTDOWN_TRANSACTIONAL, or SHUTDOWN_FORCE.
timeout The maximum amount of time allocated for the operation, in seconds
Throws:
java.sql.SQLException if the requested operation cannot be completed within the given timeout, or a database access error occurs
    public void shutdownDatabase(int shutdownModeint timeout
            throws SQLException {
        
        if (shutdownMode != 
                && shutdownMode != 
                && shutdownMode != ){
            throw new IllegalArgumentException("Shutdown mode must be "
                    + "one of: SHUTDOWN_ATTACH, SHUTDOWN_TRANSACTIONAL, " 
                    + "SHUTDOWN_FORCE");
        }
        if (timeout < 0){
            throw new IllegalArgumentException(
                    "Timeout must be >= 0");
        }
        srb.addArgument(shutdownModetimeout);
        executeServicesOperation(srb);
    }
    
    public void shutdownDatabase(byte operationModeint shutdownModeExint timeoutthrows SQLException {
    	if (
    			operationMode !=  
    			&& operationMode !=  
    			&& operationMode !=  
    		) {
    	    throw new IllegalArgumentException("Operation mode must be "
    	            + "one of: OPERATION_MODE_MULTI, " 
    	            + "OPERATION_MODE_SINGLE, OPERATION_MODE_FULL_SHUTDOWN");
    	}
    	if (
    			shutdownModeEx != 
    			&& shutdownModeEx !=  
    			&& shutdownModeEx !=  
    		) {
    	    throw new IllegalArgumentException("Extended shutdown mode must be "
    	            + "one of: SHUTDOWNEX_FORCE, SHUTDOWNEX_ATTACHMENTS, " 
    	            + "SHUTDOWNEX_TRANSACTIONS");
    	}
    	if (timeout < 0) {
    		throw new IllegalArgumentException("Timeout must be >= 0");
    	}
		srb.addArgument(shutdownModeExtimeout);
    }


    
Bring a shutdown database online.

Throws:
java.sql.SQLException if a database access error occurs
    public void bringDatabaseOnline() throws SQLException {
    }

    
Bring a shutdown database online with enhanced operation modes new since Firebird 2.5.

Throws:
java.sql.SQLException if a database access error occurs
    public void bringDatabaseOnline(byte operationModethrows SQLException {
    	if (
    			operationMode != 
    			&& operationMode !=  
    			&& operationMode !=  
    		) {
    	    throw new IllegalArgumentException("Operation mode must be "
    	            + "one of: OPERATION_MODE_NORMAL, OPERATION_MODE_MULTI, " 
    	            + "OPERATION_MODE_SINGLE");
    	}
    }
    //-------------- Database Repair ----------------------

    
Mark corrupt records in the database as unavailable. This operation ensures that the corrupt records are skipped (for example, during a subsequent backup). This method is the equivalent of gfix -mend.

Throws:
java.sql.SQLException if a database access error occurs
    public void markCorruptRecords() throws SQLException {
    }

    
Locate and release database pages that are allocated but unassigned to any data structures. This method also reports corrupt structures.

Throws:
java.sql.SQLException if a database access error occurs
    public void validateDatabase() throws SQLException {
    }

    
Locate and release database pages that are allocated but unassigned to any data structures. This method also reports corrupt structures. The value supplied for options must be one of the following:
  • 0 - Simple validation
  • VALIDATE_READ_ONLY - read-only validation, no repair
  • VALIDATE_FULL - full validation and repair
The value for options can additionally be combined in a bitmask with VALIDATE_IGNORE_CHECKSUM to ignore checksums while performing validation.

Parameters:
options Either 0, VALIDATE_READ_ONLY, or VALIDATE_FULL
Throws:
java.sql.SQLException if a database access error occurs
    public void validateDatabase(int optionsthrows SQLException {
        
        if (options < 0 
                || options != 0 && options != 
                && (options & ~) != 
                && (options & ~) != 
                && (options | 
                    ( | )) != options
                && (options |
                    ( | )) != options) {
            throw new IllegalArgumentException("options must be either 0, " 
                    + "VALIDATE_READ_ONLY, or VALIDATE_FULL, optionally "
                    + "combined with VALIDATE_IGNORE_CHECKSUM");
        }
        ServiceRequestBuffer srb = createRepairSRB
                options | .);
        executeServicesOperation(srb);
    }
   
    //----------- Sweeping -------------------------
   
    
Set the database automatic sweep interval to a given number of transactions. The Firebird default value is 20,000. If transactions is 0, automatic sweeping is disabled.

Parameters:
transactions The interval of transactions between automatic sweeps of the database. Can be set to 0, which disables automatic sweeping of the database.
Throws:
java.sql.SQLException if a database access error occurs
    public void setSweepThreshold(int transactionsthrows SQLException {
        if (transactions < 0){
            throw new IllegalArgumentException("transactions must be >= 0");
        }
        srb.addArgument(.transactions);
        executeServicesOperation(srb);
    }

    
Perform an immediate sweep of the database.

Throws:
java.sql.SQLException if a database access error occurs
    public void sweepDatabase() throws SQLException {
    }
    //----------- Shadow Files ------------------------------------
   
    
Activate a database shadow file to be used as the actual database. This method is the equivalent of gfix -activate.

Throws:
java.sql.SQLException if a database access error occurs
    public void activateShadowFile() throws SQLException {
    }

    
Remove references to unavailable shadow files. This method is the equivalent of gfix -kill.

Throws:
java.sql.SQLException if a database access error occurs
    public void killUnavailableShadows() throws SQLException {
    }
    //----------- Transaction Management ----------------------------
   
    
Retrieve the ID of each limbo transaction. The output of this method is written to the logger.

Throws:
java.sql.SQLException if a database access error occurs
    public void listLimboTransactions() throws SQLException {
        PrintStream ps = new PrintStream(getLogger());
        for (Integer trId : limboTransactionsAsList()) {
            ps.print(trId + "\n");
        }
    }
    
    public List<IntegerlimboTransactionsAsList() throws SQLException {
        // See also fbscvmgr.cpp method printInfo
        OutputStream saveOut = getLogger();
        try {
            List<Integerresult = new LinkedList<Integer>();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            setLogger(out);
            byte output[] = out.toByteArray();
            int idx = 0;
            while (idx < output.length) {
                switch (output[idx++]) {
                case .:
                case .:
                    int trId = getGds().iscVaxInteger(outputidx, 4);
                    idx += 4;
                    result.add(Integer.valueOf(trId));
                    break;
                // Information items we will ignore for now
                case .:
                    idx += 4;
                    break;
                case .:
                case .:
                    idx++;
                    break;
                case .:
                case .:
                case .:
                    int length = getGds().iscVaxInteger(outputidx, 2);
                    idx += 2;
                    idx += length;
                    break;
                default:
                    GDSException gdsException = new GDSException(.,
                            .);
                    gdsException.setNext(new GDSException(.output[idx - 1] & 0xFF));
                    throw new FBSQLException(gdsException);
                }
            }
            return result;
        } finally {
            setLogger(saveOut);
        }
    }
    
    public int[] getLimboTransactions() throws SQLException {
        List<IntegerlimboTransactions = limboTransactionsAsList();
        int[] trans = new int[limboTransactions.size()];
        int idx = 0;
        for (Integer trId : limboTransactions) {
            trans[idx++] = trId.intValue();
        }
        return trans;
    }

    
Commit a limbo transaction based on its ID.

Parameters:
transactionId The ID of the limbo transaction to be committed
Throws:
java.sql.SQLException if a database access error occurs or the given transaction ID is not valid
    public void commitTransaction(int transactionIdthrows SQLException {
        srb.addArgument(.transactionId);
        executeServicesOperation(srb);
    }

    
Rollback a limbo transaction based on its ID.

Parameters:
transactionId The ID of the limbo transaction to be rolled back
Throws:
java.sql.SQLException if a database access error occurs or the given transaction ID is not valid
    public void rollbackTransaction(int transactionIdthrows SQLException {
        srb.addArgument(
                .
                transactionId);
        executeServicesOperation(srb);
    }
    //----------- Private imlementation methods --------------------
    
    
Execute a isc_spb_rpr_* (repair) services operation.

Parameters:
operation The identifier for the operation to be executed
Throws:
java.sql.SQLException if a database access error occurs
    private void executeRepairOperation(int operationthrows SQLException {
        ServiceRequestBuffer srb = createRepairSRB(operation);
        executeServicesOperation(srb);
    }

    
Execute a isc_spb_prp_* (properties) services operation.

Parameters:
operation The identifier for the operation to be executed
Throws:
java.sql.SQLException if a database access error occurs
    private void executePropertiesOperation(int operation
            throws SQLException {
        ServiceRequestBuffer srb = createPropertiesSRB(operation);
        executeServicesOperation(srb);
    }
                

    
Get a mostly empty properties-operation buffer that can be filled in as needed. The buffer created by this method cannot have the options bitmask set on it.
        return createPropertiesSRB(0);
    }

    
Get a mostly empty repair-operation buffer that can be filled in as needed. The buffer created by this method cannot have the options bitmask set on it.
        return createRepairSRB(0);
    }


    
Get a mostly-empty properties-operation request buffer that can be filled as needed.

Parameters:
options The options bitmask for the request buffer
    private ServiceRequestBuffer createPropertiesSRB(int options){
        return createRequestBuffer(
                .
                options);
    }

    
Get a mostly-empty repair-operation request buffer that can be filled as needed.

Parameters:
options The options bitmask for the request buffer
    private ServiceRequestBuffer createRepairSRB(int options){
        return createRequestBuffer(.options);
    }
New to GrepCode? Check out our FAQ X