db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Transparent Persistence/Activation with ObjectServer

Last post 05-13-2008, 11:47 PM by chr1s. 1 replies.
Sort Posts: Previous Next
  •  05-13-2008, 06:31 PM 49019

    Transparent Persistence/Activation with ObjectServer

    Hi all,

    In the provided source code below, I have the following scenario. In the first execution, it creates data for the DB and retrieves it directly afterwards and ends with a commit. After this execution, I remove the Main.create(db) to see if everything was really made persistent.

    This works when you use the code below. But the disadvantage is, that the Db4o.configure().add(new TransparentPersistenceSupport()); is deprecated, so I need to switch do the new methods. These are provided in the code, too. The question is now, how do I need to set the server and the container, so that the example still works. I tried severel combinations, but they all did not work, which means, that the result in the second execution was just 0.

    Any ideas?

    I use db4o 7.2.31.10304 

     

     import java.util.Collection;
    import java.util.UUID;

    import com.db4o.Db4o;
    import com.db4o.ObjectContainer;
    import com.db4o.ObjectServer;
    import com.db4o.config.Configuration;
    import com.db4o.query.Query;
    import com.db4o.ta.TransparentActivationSupport;
    import com.db4o.ta.TransparentPersistenceSupport;

    public class Main {

       /**
        * @param args
        */
       public static void main(final String[] args) {
          // Db4o.configure().add(new TransparentActivationSupport());
          Db4o.configure().add(new TransparentPersistenceSupport());

          final Configuration conf = Db4o.newConfiguration();
          conf.add(new TransparentActivationSupport());
          conf.add(new TransparentPersistenceSupport());

          final Configuration confserver = Db4o.newConfiguration();
          conf.add(new TransparentPersistenceSupport());

          final Configuration confcont = Db4o.newConfiguration();
          conf.add(new TransparentActivationSupport());

          final ObjectServer server = Db4o.openServer("1bd98fc1-76cd-49ec-bf87-3ceea10238fb", 0);
          final ObjectContainer db = server.openClient();

          Main.create(db);
          Main.retrieve(db);

          db.close();
          server.close();
       }

       private static void create(final ObjectContainer db) {
          // create and store container
          final Container container = new Container();
          db.store(container);

          // populate container
          System.out.println(container.size());
          for (int i = 0; i < 10; i++) {
             container.add(UUID.randomUUID().toString());
          }
          System.out.println(container.size());
          db.commit();
       }

       private static void retrieve(final ObjectContainer db) {
          // do query
          final Query query = db.query();
          query.constrain(Container.class);
          final Collection< ? > result = query.execute();

          // output container size
          for (final Object object : result) {
             System.out.println(((Container) object).size());
          }
       }

    }

     

    import java.util.Iterator;
    import java.util.List;

    import com.db4o.activation.ActivationPurpose;
    import com.db4o.activation.Activator;
    import com.db4o.collections.ArrayList4;
    import com.db4o.ta.Activatable;

    public class Container implements Activatable, Iterable<String> {

       public final List<String> data;
       private transient Activator activator;

       public Container() {
          this.data = new ArrayList4<String>();
       }

       public void add(final String member) {
          this.activate(ActivationPurpose.WRITE);
          this.data.add(member);
       }

       public void remove(final String member) {
          this.activate(ActivationPurpose.WRITE);
          this.data.remove(member);
       }

       @Override
       public void activate(final ActivationPurpose arg0) {
          if (this.activator == null) {
             return;
          }
          this.activator.activate(arg0);
       }

       @Override
       public void bind(final Activator arg0) {
          if (this.activator == arg0) {
             return;
          }
          if (this.activator != null && arg0 != null) {
             throw new IllegalStateException();
          }
          this.activator = arg0;
       }

       @Override
       public Iterator<String> iterator() {
          this.activate(ActivationPurpose.READ);
          return this.data.iterator();
       }

       public int size() {
          this.activate(ActivationPurpose.READ);
          return this.data.size();
       }

    }

     

     

  •  05-13-2008, 11:47 PM 49023 in reply to 49019

    Re: Transparent Persistence/Activation with ObjectServer

    okay, got the solution. I need a static declaration of the activater:

     private static final TransparentPersistenceSupport TP_SINGLETON = new TransparentPersistenceSupport();

     
     

View as RSS news feed in XML