WSJdbcConnection does not wrap any objects of type com.microsoft.sqlserver.jdbc.SQLServerConnection

时间:2019-06-01 14:04:35

标签: sql-server jdbc datasource websphere-liberty server.xml

I am using SQLServerBulkCopy API to store millions of records in SQL Server database. I get an error stating

"com.microsoft.sqlserver.jdbc.SQLServerException: Destination connection must be a connection from Microsoft JDBC Driver for SQL Server"

during initializing it like

SQLServerBulkCopy bulkCopy =  new SQLServerBulkCopy(conn);

So I unwrapped the connection

SQLServerConnection conn = connection.unwrap(SQLServerConnection.class);

The original connection is com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@8da2f3e2

After going over the following questions previously asked
1. WSJDBCConnection does not wrap objects of type Oracle jdbc Connection
2. I get the `DSRA9122E: com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@d3t7e556 does not wrap any objects of type oracle.jdbc.OracleConnection

I found that it is because of classLoader for sqljdbc42.jar, mismatch between datasource and the application

So I changed my server.xml like this

 <library id="global">
   <fileset dir="${server.config.dir}/lib/global" includes="*.jar"/>
 </library>

 <jdbcDriver id="SqlJdbcDriver"              
                javax.sql.DataSource = 
 "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource"
                 libraryRef="global"/>

 <application id="myApp" name="myApp" type="ear" location="myApp.ear">
   <classloader commonLibraryRef="global"/>
 </application>

The datasource tag resides in another server.xml that is specific for env by env -

<dataSource id="myDS" jdbcDriverRef="SqlJdbcDriver" jndiName="jdbc/myDS">
    <connectionManager agedTimeout="2m" connectionTimeout="2s" maxPoolSize="50" minPoolSize="0" />
    <properties.microsoft.sqlserver databaseName="myDB" serverName="xxx.com" /> 
</dataSource>

The sqljdbc42.jar also resides in lib/global dir.
Also, I removed the dropins dir and placed the myApp in apps dir, as the application tag will not work if the app the present in dropins dir.

After all these changes too, I get the exception

"com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@8155ea2d does not wrap any objects of type com.microsoft.sqlserver.jdbc.SQLServerConnection."

Can anybody figure out what's the solution or what still I am missing?

1 个答案:

答案 0 :(得分:1)

JDBC包装模式java.sql.Wrapper.unwrap(c)仅作为接口类而不是具体的实现类进行包装。

com.microsoft.sqlserver.jdbc.SQLServerConnection是一个实现,但幸运的是,Microsoft JDBC驱动程序还为其提供了一个接口:com.microsoft.sqlserver.jdbc.ISQLServerConnection

尝试展开为界面,

ISQLServerConnection conn = connection.unwrap(ISQLServerConnection.class);