0
votes

Hey i'm a newer to anrdoid.Nearly I got a problem in db4o. when I use

public static void main(String[] args) {

            ServerConfiguration configuration=
                Db4oClientServer.newServerConfiguration();
        configuration.common().allowVersionUpdates(true);
            configuration.common().activationDepth(4);
        try{


        server=Db4oClientServer.openServer(
                configuration,"test",0);
        System.out.println("It's ok");

        }catch(Exception e){
            System.out.println(e.toString());
        }
     }

just a simple java file.It works all right.(I don't have the file named "test").And it helps me create the test file .But when I use the same code in an android project .

private ServerDDBB(){
    try{
        if(QSLog.DEBUG_I)
            QSLog.i(CLASS_NAME,"Before to open the DDBB");

        ServerConfiguration configuration=
            Db4oClientServer.newServerConfiguration();
    configuration.common().allowVersionUpdates(true);
        configuration.common().activationDepth(DEEP);
    try{
    server=Db4oClientServer.openServer(
            configuration,"ddbb",0);
    }catch(Exception e){
        Log.e("Can't pass the openServer",e.toString());
    }
        if(server==null)
            if(QSLog.DEBUG_W)
                QSLog.w(CLASS_NAME, "Serer Null");

    }catch(Exception e){
        if (QSLog.DEBUG_E)QSLog.e("hey", ExceptionUtils.exceptionTraceToString(
                e.toString(),
                e.getStackTrace()));
    }
}

It prompt me there is a IO Exception.

It's my error warning: com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:26) com.db4o.io.FileStorage$FileBin.(FileStorage.java:43) com.db4o.io.FileStorage.open(FileStorage.java:22) com.db4o.io.StorageDecorator.open(StorageDecorator.java:27) com.db4o.io.CachingStorage.open(CachingStorage.java:52) com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:56) com.db4o.internal.ObjectContainerBase$1.run(ObjectContainerBase.java:134) com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:47) com.db4o.foundation.Environments.runWith(Environments.java:28) com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:155) com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:125) com.db4o.internal.IoAdaptedObjectContainer.(IoAdaptedObjectContainer.java:35) com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18) com.db4o.Db4o.openFile(Db4o.java:224) com.db4o.cs.internal.config.StandardClientServerFactory.openServer(StandardClientServerFactory.java:35) com.db4o.cs.Db4oClientServer.openServer(Db4oClientServer.java:52) com.hlh.ddbb.ServerDDBB.(ServerDDBB.java:37) com.hlh.ddbb.ServerDDBB.createInstance(ServerDDBB.java:84) com.hlh.ddbb.ServerDDBB.getServer(ServerDDBB.java:78) com.hlh.ddbb.ClientDDBB.(ClientDDBB.java:20) com.hlh.SettingsTab.initActivity(SettingsTab.java:196) com.hlh.SettingsTab.onCreate(SettingsTab.java:91) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) android.app.ActivityThread.access$2200(ActivityThread.java:119) android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:123) android.app.ActivityThread.main(ActivityThread.java:4363) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:521) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) dalvik.system.NativeStart.main(Native Method)

Any reply will be nice.

1

1 Answers

0
votes

Looks like that you just try to use a regular file name. In Android you need to store the database in a location, where you have access to:

String filePath = this.getFilesDir() + "/android.db4o";
    final EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
    config.common().add(new AndroidSupport());
    ObjectContainer db = Db4oEmbedded.openFile(config,filePath);

Btw. You don't need the server jar for embedded client server. You can open a new session container with the regular container:

ObjectContainer session = rootContainer.ext().openSession();
try {
    // do the operations on the session-container
    session.store(new Person("Joe"));
} finally {
    // close the container. For example when the request ends
    session.close();
}