BLOBs, Weblogic and Spring

For those not in the know:

  • Oracle LOBs are implemented like crap and, instead of using the standard interface suggested by JDBC (coughlike everyone elsecough) obliges to use their proprietary classes, something that inspired pages like this one on the hibernate wiki.

  • Weblogic (and other app servers) wraps your JDBC connection with their own when using a Connection Pool.

  • Other frameworks like p6spy and commons-dbcp may wrap Connections, creating a beautiful onion of library dependencies for contemplative rejoyce.
    Before implementing your own Hibernate type mapping, there is a better option provided by Spring: a separation of concepts into two classes that can be injected into the LocalSessionFactory.

  • LobHandler: the class that handles all the LOB stuff, and

  • NativeJdbcExtractor: responsible for the onion peeling.
    Since the extractor is only needed inside the app server, a new separate configuration would be handy for the testsuite:

hibernate-common-config.xml:
<pre> &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> &lt;property name="dataSource" ref="dataSource" /> &lt;property name="lobHandler" ref="lobHandler"/> &lt;property name="mappingResources"> &lt;list> ...my class files... &lt;/list> &lt;/property> &lt;/bean> </pre>

test-hibernate-config.xml:
`


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@hostname:1521:database"/>
<property name="username" value="john"/>
<property name="password" value="doe"/>
</bean>

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"/>

`

appserver-hibernate-config:
`


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDS" />
</bean>

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor">
<bean class="org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor"/>
</property>
</bean>

`

Now you only have to combine a set of config files for testing or deployment. Order is not important.

One last note: if working with weblogic, be sure to uncheck "Remove Infected Connections Enabled" on your Connection Pool since it would disable connection reuse.