klehmann / domino-jna

Java project to access the IBM/HCL Domino C API using Java Native Access (JNA)
Apache License 2.0
66 stars 16 forks source link

Unable to open local DB when notes client isn't started #76

Open beranPro opened 2 years ago

beranPro commented 2 years ago

Hi, i'm currently building a standalone application which uses domino jna for replicating local DBs and i experienced that i'm not able to open a database until i open up the notes client. Is this really necessary or i'm doing the session initialization wrong? Are there multiple ways to create a notes session maybe with different consequences?

here the simplified code:

    public boolean replicate(Request newReplication) {
        final boolean ret = false;
        final List<NotesReplicationStats> replicationStats = new ArrayList<>();
        EnumSet<ReplicateOption> replOptions = EnumSet.noneOf(ReplicateOption.class);
        replOptions.add(ReplicateOption.RECEIVE_NOTES);
        replOptions.add(ReplicateOption.SEND_NOTES);
        replOptions.add(ReplicateOption.CLOSE_SESSION);

        try {

            //initial Notes/Domino access for current thread (running single-threaded here)
            NotesThread.sinitThread();

            //launch run method within runWithAutoGC block to let it collect/dispose C handles
            NotesGC.runWithAutoGC(new Callable<Object>() {

                public Object call() throws Exception {
                    boolean dontSetEnvVar = true;
                    IDUtils.switchToId(notesId, notesIdPass, dontSetEnvVar);

                    for (Job job : newReplication.getJobs()) {
                        try {

                            if( NotesDatabase.exists("", job.getPath()) ) { //Check if DB exists in local (returns false if notes client isn't open)
                                NotesDatabase dbNames = new NotesDatabase("", job.getPath(), "");
                                NotesReplicationStats nrs = dbNames.replicateWithServer(job.getServer(), replOptions, 10);
                                dbNames.recycle();
                            } else {
                                logger.error("couldn't open DB");
                            }

                        } catch (Exception e) {
                            logger.error("couldn't open DB");
                        }
                    }
                    return new Object();
                }

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //terminate Notes/Domino access for current thread
            NotesThread.stermThread();
        }
        return true;
    }

I'm pretty sure i'm doing something wrong here.

klehmann commented 2 years ago

Looks ok. Which error do you get and which OS are you using?

beranPro commented 2 years ago

i tried it with a current Windows 10 x64 and Notes 12 (x86) and with a Windows 7 x86 and a Notes 9 Client x86. I'm using the static "exists" method to check if the local replica exists and when i have no notes apps running he returns false

https://github.com/klehmann/domino-jna/blob/master/domino-jna/src/main/java/com/mindoo/domino/jna/NotesDatabase.java#L241

When i start my notes client and execute the same stuff again it works.

beranPro commented 2 years ago

Hi @klehmann , it seems that the NotesDatabases.exists() method is the problem. When i don't call this and open the DB directly then it works.

klehmann commented 2 years ago

Thanks for the info. Looks like the Notes Client does not have modified dates of a database if it's not already open. That method uses NSFDbModifiedTimeByName internally. Somehow this rings a bell but I cannot remember in which context I noticed this before.