2016-03-14 72 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