Skip to content

JDBC unwrapping doesn't work #21

imd opened this Issue Aug 23, 2012 · 5 comments

3 participants

imd commented Aug 23, 2012


I'm using Olap4j, and, as documented on page 11 of this PDF, Java is supposed to automatically unwrap JDBC connection objects to gain access to their true connection, which works under Jython but not PyJNIus. (This seems like magic to me, so I don't understand the details.)

Steps to reproduce

  1. Download Pentaho Mondrian. Create a Postgres table called 'foodmart', username 'postgres', password 'password'. You may need to load the sample data as described in the Mondrian documentation.
  2. Create the file given at the bottom of this report in the Mondrian directory. Run it. Under Jython 2.7, it runs fine, but under PyJNIus, it reports:
Traceback (most recent call last):
  File "", line 23, in <module>
    result = connection.prepareOlapStatement(mdx).executeQuery()
AttributeError: 'java.sql.Connection' object has no attribute 'prepareOlapStatement'

import sys

_is_jython = sys.platform.startswith("java")
if _is_jython:
    from java.sql import DriverManager
    from org.olap4j import OlapConnection
    from jnius import autoclass
    DriverManager = autoclass('java.sql.DriverManager')
    OlapConnection = autoclass('org.olap4j.OlapConnection')

url = 'jdbc:mondrian:JdbcDrivers=org.postgresql.Driver;Jdbc=jdbc:postgresql://localhost/foodmart?user=postgres&password=password;Catalog=demo/FoodMart.xml;'

mdx = '''
  [Measures].[Customer Count] ON COLUMNS,
  [Gender].[Gender].Members ON ROWS
FROM [Sales]

connection = DriverManager.getConnection(url)
result = connection.prepareOlapStatement(mdx).executeQuery()
Kivy member
tito commented Aug 24, 2012


Ok, i didn't tried the example, but a fast research on the internet show that DriverManager.getConnection(url) return a Connection from java.sql.Connection on which the method prepareOlapStatement doesn't exist. What you need is an OlapConnection.

The difference here is that we guess the type from the signature. DriverManager.getConnection have no indication about the OlapConnection. And since you don't declare the type of the variable... You need to cast.


connection = DriverManager.getConnection(url)
connection = cast('org.olap4j.OlapConnection', connection)
result = connection.prepareOlapStatement(mdx).executeQuery()

This might work, not tested. Try and report here :)

apalala commented Aug 24, 2012

I think that the problem is that Jnius handles objects returned from Java as JavaObject instead of registering their class an returning a proper JavaClass descendant instance.

I'm sorry I have so much to say, and so little time to contribute right now.

Kivy member
tito commented Aug 24, 2012

Can you do a "print connection" and give the result ?

Kivy member
tito commented Aug 24, 2012

Try to:

connection = DriverManager.getConnection(url)
print 'before', connection
connection = cast(OlapConnection, connection)
print 'after', connection
@imd imd closed this Aug 24, 2012
imd commented Aug 24, 2012

OK, the Jython worked by a fluke; the equivalent Java wasn't correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.