简体   繁体   English

Neo4j jdbc驱动程序中的数据库连接池

[英]DB connection pooling in Neo4j jdbc driver

In my application I'm using neo4j-community-2.3.0-M02 with neo4j-jdbc 2.3.2 . 在我的应用程序中,我使用neo4j-community-2.3.0-M02和neo4j-jdbc 2.3.2。 It creates large number of threads each should execute 3 or 4 cypher queries. 它创建大量线程,每个线程应执行3或4个密码查询。 To execute queries I use following method, 要执行查询,我使用以下方法,

    private ResultSet executeCypher(String queryString) throws Exception {
    try {
        String restUrl = getPropertiesCache().getCofigProperty("neo4j_url");
        String driver = getPropertiesCache().getCofigProperty("neo4j_driver");
        String userName = getPropertiesCache().getCofigProperty("neo4j_user");
        String passWord = getPropertiesCache().getCofigProperty("neo4j_pwd");
        Class.forName(driver);
        try{
            Neo4jConnection connection = (Neo4jConnection) 
                    DriverManager.getConnection(restUrl, userName, passWord);
            try {
                PreparedStatement stmt = connection.prepareStatement(queryString);
                try{
                    ResultSet rs = (ResultSet) stmt.executeQuery(queryString);
                    stmt.close();
                    connection.close();
                    return rs;
                }catch (SQLException e){
                    e.printStackTrace();
                } catch (NullPointerException e1){
                    e1.printStackTrace();
                } catch (RuntimeException e2){
                    e2.printStackTrace();
                }
                if(!stmt.isClosed()){
                    stmt.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            if(!connection.isClosed()){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Which means I'm creating db connection for each cypher query. 这意味着我正在为每个cypher查询创建数据库连接。 I think it's very bad idea because when creating large number of connections most of them start to be timeout. 我认为这是一个非常糟糕的主意,因为在创建大量连接时,大多数连接都会开始超时。 I think db connection pooling will help on this case or is there any better idea regarding this than connection pool? 我认为数据库连接池将有助于这种情况,还是有关于此的更好的想法比连接池?

If connection pooling solve this issue, please give an example of how to create db connection pool using neo4j jdbc driver. 如果连接池解决了这个问题,请举例说明如何使用neo4j jdbc驱动程序创建数据库连接池。

neo4j-jdbc 2.3X驱动程序使用REST API连接到数据库,基本上它们是使用Restlet API的http调用,因此在创建/关闭JDBC连接时不会打开或关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM