Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerEntityClasses once with ODatabaseObjectPool should be enough [moved] #113

Closed
lvca opened this issue Dec 10, 2012 · 0 comments
Closed

Comments

@lvca
Copy link
Member

lvca commented Dec 10, 2012

This is Issue 113 moved from a Google Code project.
Added by 2010-09-18T07:28:17.000Z by jani.laa...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Closed (Fixed).

Original labels: Type-Defect, Priority-Medium, v0.9.23

Original description

If I use ODatabaseObjectPool to acquire multiple simultaneous connections in multiple threads I still need to register my entities for every pool. I'd hope registering entities once in Java process is the correct pattern.

Example here:


package com.janilaakso;

import com.janilaakso.domain.Company;
import com.janilaakso.domain.Person;
import com.orientechnologies.orient.core.db.object.ODatabaseObjectPool;
import com.orientechnologies.orient.core.db.object.ODatabaseObjectTx;
import com.orientechnologies.orient.core.tx.OTransaction.TXTYPE;

public class ClientTest extends Thread {

    private static int THREAD_COUNT = 10;

    private static String dbUrl = "local:/tmp/orient-database-test/foo";

    private ODatabaseObjectTx db;

    @Override
    public void run() {
        log("THREAD START");
        try {
            db = ODatabaseObjectPool.global().acquire(dbUrl, "admin", "admin");

            // ISSUE: This should work through main method (define entities
            // once). We are on same Java process.
            db.getEntityManager()
                    .registerEntityClasses("com.janilaakso.domain");

            db.begin(TXTYPE.OPTIMISTIC);
            for (int i = 0; i < 1000; i++) {
                Company company = new Company();
                company.setName(&quot;Vaadin Ltd &quot; + i);
                Person person = new Person();
                person.setFirstname(&quot;Jani &quot; + i);
                person.setCompany(company);
                db.save(person);
            }
            db.commit();
            log(showElements());
            log(&quot;THREAD END&quot;);
        } catch (Exception e) {
            log(&quot;THREAD FAILED: &quot; + e.getMessage());
        } finally {
            if (db != null && !db.isClosed())
                db.close();
        }

    }

    private String showElements() {
        StringBuffer desc = new StringBuffer();
        for (String name : db.getClusterNames()) {
            desc.append(name).append(&quot;=&quot;).append(db.countClusterElements(name))
                    .append(&quot;, &quot;);
        }
        return desc.toString();
    }

    public static void main(String[] args) {
        // ISSUE: all the same if I try to use
        // db = ODatabaseObjectPool.global().acquire(dbUrl, &quot;admin&quot;, &quot;admin&quot;);
        // here
        ODatabaseObjectTx db = new ODatabaseObjectTx(dbUrl);
        // Registering entities do no work here when threads afterwards acquire
        // connections using ODatabaseObjectPool object.
        // acquire. This should be enough to do once in here:
        // db.getEntityManager().registerEntityClasses(&quot;com.janilaakso.domain&quot;);
        try {
            if (!db.exists()) {
                log(&quot;Creating new database&quot;);
                db.create();
            }
        } finally {
            if (db != null && !db.isClosed())
                db.close();
        }

        // Create threads and start them right away
        for (int i = 0; i < THREAD_COUNT; i++) {
            new ClientTest().start();
        }
    }

    public static void log(String msg) {
        System.err.println(Thread.currentThread() + &quot;: &quot; + msg);
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant