设为首页 加入收藏

TOP

Mondiran创建连接(一)
2015-11-21 01:52:45 来源: 作者: 【 】 浏览:0
Tags:Mondiran 创建 连接
以前使用jdbc创建连接的时候使用的url是这样的形式:jdbc:mysql://hostname:port/database?key1=value1&key2=value2,在URL需要以"jabc:mysql"开头的,其中需要声明 数据库服务器的地址和端口、数据库名还有一些其它的属性,以"?"分割,每一个属性使用"&"字符分割,但是mondrian作为OLAP服务器和 mysql这类的关系型数据库还是有所区别的,毕竟它本身不保存任何数据,它更像是一个工具类的东西(类似于hive),能够把MDX翻译成一串SQL语句再交由关系数据库执行,所以它不能独立得称为一个服务器(在我的理解中,对于服务器的使用只需要知道IP和port以及一些其他的参数就足够了)。 为了能够使用java自带的DriverManager创建connection,mondrian做了这个兼容,在使用mysql这样的数据库的时候我们会首先执行如下的操作:
Class. forName(driver);
connection = DriverManager. getConnection(url , username ,password );

getConnection函数返回的是java.sql.Connection对象,这个Connection是通用的数据库连接,但是mondrian也是用了相同的方式创建到OLAP引擎的连接,它的driver为mondrian.olap4j.MondrianOlap4jDriver,它的url有自己独特的结构,下面就看一下在mondrian中是如何创建连接的,除了之上的两部,还需要再进行其他的操作:
Class. forName(driver);
connection = DriverManager. getConnection(url , username ,password );
OlapConnection olapConnection = connection.unwrap(OlapConnection.class);

在mondrian中的url格式如下:jdbc:mondrian:Jdbc=jdbc:mysql://10.241.20.157:3306/foodmart?user=root&password=root;Catalog=C:\\Users\\Administrator\\Desktop\\nrtp\\FoodMart.xml;可以看出url中的每一项是通过";"分割的,类似于mysql的开头是"jdbc:mysql",mondrian连接的url的开头必须是"jdbc:mondrian:",另外还包括"Jdbc"和"Catalog"属性字段,在DriverManager的getConnection静态方法中执行如下操作:
    public static Connection getConnection(String url,
        String user, String password) throws SQLException {
        java.util.Properties info = new java.util.Properties();
        if (user != null) {
            info.put("user", user);
        }
        if (password != null) {
            info.put("password", password);
        }
        return (getConnection(url, info, Reflection.getCallerClass()));
    }

这个函数是所有的数据库连接创建的方式,其实就是创建一个Properties对象,然后加上user和password属性,然后调用其它的getConnection方法:
    private static Connection getConnection(
        String url, java.util.Properties info, Class
   caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized (DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }
            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }
        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            thr
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB安装和简介 下一篇操作ACCESS数据库总结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: