DriverManager unable to return connection after Ja

2019-08-26 05:32发布

问题:

I have an application that tries to connect to Impala and Oracle. Below are the 2 beans defined.

Impala Driver - ImpalaJDBC41-2.5.41.jar Oracle Driver - ojdbc6.jar

<bean id="ID1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.cloudera.impala.jdbc41.Driver"/>

<property name="url" value="jdbc:impala://impalahost:21050/;AuthMech=1;KrbRealm=myrealm;KrbServicName=impala;KrbHostFQDN=xxx" />

</bean>

<bean id="ID2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@//oraclehost:1523/DB" />
<property name="username" value="myuser" />
<property name="password" value="pwd" />
<property name="connectionProperties">
<props>
<prop key="defaultRowPrefetch">5000</prop>
</props>
</property>
</bean>

While getting the "Oracle" connection with a bean reference - ID2, getting this exception (surprisingly it goes to cloudera driver code)-

==java.lang.NullPointerException
===Stack trace...
java.util.Hashtable.put(Unknown Source)
com.cloudera.jdbc.common.AbstractDriver.copyProperties(Unknown Source)
com.cloudera.jdbc.common.AbstractDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source) org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFrom 
DriverManager(DriverManagerDataSource.java:173) org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFrom 
Driver(DriverManagerDataSource.java:164) org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149) org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119) org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)

The same configurations work fine with Java 1.7

回答1:

The issue occurs when Impala driver is loaded before the Oracle driver is loaded; since ImpalaJDBC41-2.5.41.jar is referred before ojdbc6.jar (alphabetic order). So I had 2 options to get the Oracle driver loaded first and both work fine -

  1. rename Impala jar to ZImpala so that it gets referred after ojdbc jar

  2. But the 'cleaner and better' approach was to load Oracle driver with custom module by configuring it as a JDBC Datasource inside Jboss standalone.xml; which will load Oracle driver even before our application EAR is deployed.