This discussion is archived
7 Replies Latest reply: Aug 31, 2012 12:38 AM by 958983 RSS

Using Berkeley With Two Different Environment Simultaneously

958983 Newbie
Currently Being Moderated
I am trying to use Berkeley with two different environment simultaneously in one program. But I am getting an error message of Databases left open. The first environment close with no error but the 2nd environment, having an error like this,

Exception in thread "main" java.lang.IllegalStateException: Unclosed Database: element_primary_key_index\\192.168.150.211\glassfish3\Berkeley\environment\Testing11
Unclosed Database: class_catalog\\192.168.150.211\glassfish3\Berkeley\environment\Testing11
Unclosed Database: element_database\\192.168.150.211\glassfish3\Berkeley\environment\Testing11
Databases left open: 3

     at com.sleepycat.je.Environment.close(Environment.java:383)
     at com.svi.tools.gfs3v10domain.database.GFS3v10ReadWriteDatabase.closeUpload(GFS3v10ReadWriteDatabase.java:155)
     at com.svi.tools.gfs3v10domain.GFS3v10Domain.closeReadWrite(GFS3v10Domain.java:160)
     at com.svi.tools.gfs3v10.util.GFS3v10UploadUtil.closeUpload(GFS3v10UploadUtil.java:97)
     at com.svi.tools.gfs3v10.GFS3v10.closeUpload(GFS3v10.java:115)
     at com.svi.tools.gfs3v10uploader.util.Uploader.uploadFiles(Uploader.java:89)
     at com.svi.tools.gfs3v10uploader.GFS3v10Uploader.mainMethod(GFS3v10Uploader.java:109)
     at com.svi.tools.gfs3v10uploader.GFS3v10Uploader.main(GFS3v10Uploader.java:52)

Please someone help me with my problem. Thanks in advance.
  • 1. Re: Using Berkeley With Two Different Environment Simultaneously
    greybird Expert
    Currently Being Moderated
    This error simply means that you neglected to call Database.close for 3 databases before you called Environment.close. It is probably unrelated to the fact that you have two environments open.

    Note that if you call Environment.openDatabase twice for the same database (in the same environment), you get two Database instances/handles, and both must be closed.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  • 2. Re: Using Berkeley With Two Different Environment Simultaneously
    958983 Newbie
    Currently Being Moderated
    I am using the same close method for the two environment. The 1st closed the Databases and the environment with no problem but the 2nd environment had that error. I am sure that I close the Databases before closing the environment. But the error falls in closing the 2nd environment. The sequence is like this:

    Open the 1st Environment --> Open the 2nd Environment --> Close the 1st Environment --> Close the 2nd Environment (Error falls in closing the 2nd Environment)

    But if I am going to change the sequence like this:

    Open the 1st Environment --> Close the 1st Environment --> Open the 2nd Environment --> Close the 2nd Environment

    There is no error.

    The one that I need is the 1st sequence. Thank you.
  • 3. Re: Using Berkeley With Two Different Environment Simultaneously
    greybird Expert
    Currently Being Moderated
    I see. By "environment" you mean environment handle. It is possible to open two separate environments, with different directories. It is also possible to open two Environment instances, or handles, for the same environment directory. You are talking about the latter.

    I don't know of a problem like the one you describe. Could you please reproduce it with a small, standalone test program, that does nothing but open and close the databases and environment? If you can post that, we can take a look and see if you're doing something wrong, or there is a bug of some kind.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  • 4. Re: Using Berkeley With Two Different Environment Simultaneously
    958983 Newbie
    Currently Being Moderated
    Hi Mark,

    Here is my sample program for the problem:

    import java.io.File;

    import com.sleepycat.bind.serial.StoredClassCatalog;
    import com.sleepycat.je.Database;
    import com.sleepycat.je.DatabaseConfig;
    import com.sleepycat.je.Environment;
    import com.sleepycat.je.EnvironmentConfig;
    import com.sleepycat.je.EnvironmentLockedException;
    import com.sleepycat.je.SecondaryConfig;
    import com.svi.tools.gfs3v10domain.objects.GFS3v10DomainElementData;
    import com.svi.tools.gfs3v10domain.objects.GFS3v10DomainElementKey;
    import com.svi.tools.gfs3v10domain.views.utils.ElementByPrimaryKeyCreator;

    /**
    * Read Write Database used for every thing else.
    */
    public class MethodsSample implements GFS3v10Database {
         /**
         * Environment where the Database resides.
         */
         private Environment environment = null;
         
         private boolean isClose = false;
         String environmentString;
         
         /**
         * Class Catalog for Stored Classes.
         */
         private static StoredClassCatalog classCatalog;
         
         /**
         * Element Database.
         */
         private static Database elementDatabase;
         
         /**
         * Element Database by Primary Key.
         */
         private static Database elementByPrimaryKeyDatabase;
         
         private static Database catalogDatabase;
         /**
         * Default Constructor.
         */
         public MethodsSample() {
         }

    /**
    * Alternate Constructor.
    * @param homeDirectory Location where the Database is Located.
    */
    public MethodsSample(String homeDirectory) {
         environmentString = homeDirectory;
         openEnvironment(homeDirectory);
         openDatabase();
    }

    @Override
    /**
         * Opens the Read Write Database.
         * @param homeDirectory Location where the Database is Located.
         */
    public void openEnvironment(String homeDirectory) {
         EnvironmentConfig environmentConfig = new EnvironmentConfig();
    environmentConfig.setTransactional(true);
    environmentConfig.setAllowCreate(true);
    environmentConfig.setDurability(DURABILITY);

    while (environment == null) {
         try {
              environment = new Environment(new File(homeDirectory), environmentConfig);
         } catch(EnvironmentLockedException ele) {
              try {
                             Thread.sleep(500);
                        } catch (InterruptedException e) {
                        }
         }
    }
    }

    @Override
    /**
         * Opens the Database.
         */
    public void openDatabase() {
         DatabaseConfig databaseConfig = new DatabaseConfig();
         databaseConfig.setDeferredWrite(true);
    databaseConfig.setAllowCreate(true);

    catalogDatabase = environment.openDatabase(null, CLASS_CATALOG + environmentString, databaseConfig);
    classCatalog = new StoredClassCatalog(catalogDatabase);

    elementDatabase = environment.openDatabase(null, ELEMENT_DATABASE + environmentString, databaseConfig);

    SecondaryConfig secondaryConfig = new SecondaryConfig();
    secondaryConfig.setDeferredWrite(true);
    secondaryConfig.setAllowCreate(true);
    secondaryConfig.setSortedDuplicates(true);

    secondaryConfig.setKeyCreator(new ElementByPrimaryKeyCreator(classCatalog, GFS3v10DomainElementKey.class, GFS3v10DomainElementData.class, String.class));
    elementByPrimaryKeyDatabase = environment.openSecondaryDatabase(null, ELEMENT_PRIMARY_KEY_INDEX + environmentString, elementDatabase, secondaryConfig);

    }

    @Override
    /**
         * Gets the Environment.
         * @return Environment.
         */
    public Environment getEnvironment() {
         return environment;
    }

    @Override
    /**
         * Gets the Class Catalog.
         * @return Class Catalog.
         */
    public StoredClassCatalog getClassCatalog() {
         return classCatalog;
    }

    @Override
    /**
         * Gets Element Database.
         * @return Element Database.
         */
    public Database getElementDatabase() {
         return elementDatabase;
    }

    @Override
    /**
         * Gets Element By Primary Key Database.
         * @return Element By Primary Key Database.
         */
    public Database getElementByPrimaryKeyDatabase() {
         return elementByPrimaryKeyDatabase;
    }

    @Override
    /**
         * Closes Database and then Environment.
         */
    public void closeUpload() {
         System.out.println("1st Jar environment closing = " + environmentString);
         elementByPrimaryKeyDatabase.close();
         elementDatabase.close();
         classCatalog.close();
         catalogDatabase.close();
         environment.close();
         isClose = true;
    }

    public Boolean isClose() {
         return isClose;
    }

         @Override
         public void closeOthers() {
              
         }

    }

    for the Main:

    public class sample {
         public static void main(String[] args) {
              String environment1 = "\\\\192.168.160.184\\glassfish\\berkeley\\environment\\home\\Multiple\\Testing11";
              String environment2 = "\\\\192.168.150.211\\glassfish3\\Berkeley\\environment\\Testing11";

              openCloseEnvironment(environment1, environment2);
                   
              
         }
         
         public static void openCloseEnvironment(String environment1, String environment2) {
              MethodsSample forEnvironment1 = new MethodsSample(environment1); //Opens the Databases
              MethodsSample forEnvironment2 = new MethodsSample(environment2); //Opens the Databases
              
              
              forEnvironment1.closeUpload();
              forEnvironment2.closeUpload();
    // same error happens no matter what sequence for closing


         }
    }

    Thank you.
  • 5. Re: Using Berkeley With Two Different Environment Simultaneously
    greybird Expert
    Currently Being Moderated
    >
         private static StoredClassCatalog classCatalog;
         
         /**
         * Element Database.
         */
         private static Database elementDatabase;
         
         /**
         * Element Database by Primary Key.
         */
         private static Database elementByPrimaryKeyDatabase;
         
         private static Database catalogDatabase;
    >

    These fields are static -- they are globals -- so you're overwriting them when you open the 2nd environment.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  • 6. Re: Using Berkeley With Two Different Environment Simultaneously
    958983 Newbie
    Currently Being Moderated
    I removed the STATIC but still doesnt work.

    private StoredClassCatalog classCatalog;
         private Database elementDatabase;
         private Database elementByPrimaryKeyDatabase;
         private Database catalogDatabase;

    or even making the variables public.

    public StoredClassCatalog classCatalog;
         public Database elementDatabase;
         public Database elementByPrimaryKeyDatabase;
         public Database catalogDatabase;

    Same error.

    Unclosed Database: element_primary_key_index\\192.168.160.184\glassfish\berkeley\environment\home\Multiple\Testing11
    Unclosed Database: element_database\\192.168.160.184\glassfish\berkeley\environment\home\Multiple\Testing11
    Unclosed Database: class_catalog\\192.168.160.184\glassfish\berkeley\environment\home\Multiple\Testing11
    Databases left open: 3
  • 7. Re: Using Berkeley With Two Different Environment Simultaneously
    958983 Newbie
    Currently Being Moderated
    Hi Mark,

    Thanks for your support! I edited my codes based from your suggestions and it worked!

    Thank you very much.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points