Cyntech’s Tech Blog

Icon

Technical tidbits; coding and I.T.

Can’t get that underlying connection?

While trying to create CLOBs for an Oracle database table, I was getting an Invalid Argument(s) error when trying to call CLOB.createTemporary().  I beat my head against a wall trying to figure out why I was getting null when you request the underlying connection from a Pooling Data Source but then I found some information that solved it for me.

For it to work, you need to enable access to the underlying connection to be able to get an instance of it.

There are two ways of doing this;

In your Tomcat configuration (or other Application Server), by enabling access to it in the <Context> of your web application:

<Context path="/ariweb" docBase="ariweb.war">
    <Resource name="jdbc/ari" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/ari">
    <parameter>
        <name>driverClassName</name>
        <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>
    <parameter>
        <!-- NOTE: This is necessary to enable access to the Oracle connection object -->
        <name>accessToUnderlyingConnectionAllowed</name>
        <value>true</value>
    </parameter>
    <!-- Other configuration parameters -->
    ...
    </ResourceParams>
</Context>

If you’re using Spring, you can also set it there in the parameters for your data source:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@dbhost:1521:DBSID</value></property>
        <property name="username"><value>user</value></property>
        <property name="password"><value>pass</value></property>
        <property name="accessToUnderlyingConnectionAllowed"><value>true</value></property>
</bean>

This means that when you use the getDelegate() or getInnermostDelegate() methods, you will get the underlying connection type rather than a null.