2016-03-14 75 views
3

我有一个后端Spring应用程序和Orientdb图形数据库。我使用Tinkerpop框架将orientdb顶点映射到java对象,而OPS4J用于弹簧事务管理。现在我想实现一个多租户,其中几个客户(租户)使用这个应用程序实例。此应用程序完全按照REST原则工作,并向几个Angular应用程序开放 - 每个客户。因此,与我们的客户一样多的前端Angular应用程序以及只有一个后端REST Spring应用程序。后端通过HTTP请求识别租户。Orientdb分区图java实现

现在我不知道最好的解决办法...

首先解决

当我看到Orientdb文档,我发现有一种方法如何实现orientdb多租户 - http://orientdb.com/docs/2.1/Partitioned-Graphs.html。但是我不知道如何通过Java API使用它,除非我不想为每个请求创建一个新的数据库连接。因为现在Spring事务管理器从Spring事务管理配置中集中设置的连接池中获取连接。我没有找到任何Java的例子。

Spring事务管理配置:

@Configuration 
@EnableTransactionManagement 
public class TransactionConfig { 

    @Bean 
    @Qualifier("graphDbTx") 
    public OrientTransactionManager graphDbTransactionManager() { 
     OrientTransactionManager bean = new OrientTransactionManager(); 
     bean.setDatabaseManager(graphDatabaseFactory()); 
     return bean; 
    } 

    @Bean 
    public OrientBlueprintsGraphFactory graphDatabaseFactory() { 
     OrientBlueprintsGraphFactory dbf = new OrientBlueprintsGraphFactory(); 
     dbf.setMaxPoolSize(6); 
     dbf.setUrl(DbConfig.DATABASE_URL); 
     dbf.setUsername("admin"); 
     dbf.setPassword("admin"); 
     return dbf; 
    } 

    @Bean 
    public FramedGraphFactory framedGraphFactory() { 
     return new FramedGraphFactory(new JavaHandlerModule()); 
    } 

} 

获取连接:

protected FramedGraph<OrientGraph> framedGraph() { 
    return framedGraphFactory.create(gdbf.graph()); 
} 

解决方法二

另一种解决方案是使用TinkerPop有关

PartitionGraph

在Orientdb上工作的类,但在Orientdb文档中没有找到任何有关这种可能性的句子。就在Tinkerpop - https://github.com/tinkerpop/blueprints/wiki/Partition-Implementation。它可以工作,但最终它只是在每个orientdb顶点创建一个没有索引的属性,所以我害怕在这里查询的性能。

有没有人有这方面的经验?任何建议?

回答

1

使用Java API来创建分区数据库(如果我明白你感兴趣的)宏步骤是:

  • 得到连接(使用DB的istance被重用池);
  • 修改V类和E类;创建新的用户启用写入;
  • 当你登录数据库时,user1可以写入顶点, user2不可见并且相反;

    //写入您的控制器:创建用户启用写入DB .............. Connection con = new Connection(); OrientGraph noTx = con.getConnection();

    //create partition 
        noTx.begin(); 
        noTx.command(new OCommandSQL("ALTER CLASS V superclass orestricted")).execute(); 
        noTx.command(new OCommandSQL("ALTER CLASS E superclass orestricted")).execute(); 
        noTx.commit(); 
    
        //create different users 
        noTx.begin(); 
        String ridRule = ""; 
        Iterable<Vertex> rule = noTx.command(new OCommandSQL("select from ORole where name = 'writer'")).execute(); 
        ridRule = rule.iterator().next().getId().toString(); 
        noTx.command(new OCommandSQL("INSERT INTO ouser SET name = 'user1', status = 'ACTIVE', password = 'user1', roles = ["+ridRule+"]")).execute(); 
        noTx.command(new OCommandSQL("INSERT INTO ouser SET name = 'user2', status = 'ACTIVE', password = 'user2', roles = ["+ridRule+"]")).execute(); 
        noTx.commit(); 
    
        //will not close the graph instance, but will keep open and available for the next requester 
        noTx.shutdown(); 
    
        //finally To release all the instances and free all the resources 
        con.clodeAllConnect(); 
    
    
        //WRITE IN YOUR CONTROLLER: LOGIN WITH USER APPROPRIATE ..................... 
        //CODE to login with user1 or user2, CREATE VERTEX SET label = 'food', name = 'Pizza' etc.... 
    } 
    
    
    //beans 
    public static class Connection { 
    
        private OrientGraphFactory factory = null; 
    
        public Connection() { 
         //recyclable pool of instances 
         factory = new OrientGraphFactory("remote:localhost/blog").setupPool(1, 10); 
        } 
    
        //return the connection 
        public OrientGraph getConnection() { 
         OrientGraph txGraph = factory.getTx(); 
         return txGraph; 
        } 
    
        public void clodeAllConnect(){ 
         factory.close(); 
    
        } 
    } 
    

为适应这些步骤,并在春季插入他们可能是有用的这个链接是OrientDB - spring implementation。它不是很多,但我希望能有所帮助。

+0

是的,我知道当我用user1(tenant1)登录时,他可以编写对其他用户(租户)不可见的顶点,并且我知道如何在如此低水平的Java实现上做到这一点。但我不想写自己的交易经理 - 这很复杂,我没有太多时间。但是恐怕我不得不向OPS4J事务管理器提出请求,这对于一个租户来说工作得很好,但它还没有为更多租户准备好并在那里实施。我认为Spring Data Orientdb仍处于开发阶段,没有稳定的版本,不是吗? – sovanegger

+0

我将分叉OPS4J项目(将spring事务与orientdb集成)并实施Orientdb分区图的解决方案 - 与您的类似或相同。无论如何,现在还没有人做这件事。它不应该是这样的问题,它是一个小型图书馆。 – sovanegger