sqljdbc_auth.dll已经加载到另一个类加载器中

时间:2015-05-28 10:33:18

标签: java sql-server jdbc eclipse-plugin eclipse-rcp

我有一个带有不同插件的RCP应用程序。在我的两个插件中,我需要使用Windows集成身份验证连接到SQL Server数据库。第一个加载的工作正常,但第二个抛出异常:

Native Library sqljdbc_auth.dll already loaded in another classloader

我的代码是

插件A - > SqlRead.java

static{
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      e.PrintStakeTrace();
    }
}

private Connection openConnection(String connectionString) throws SQLException{
    return DriverManager.getConnection(connectionString);
}  

插件B - > Mirror.java

static {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    init();
}

public void run(){
    try {
                this.source=DriverManager.getConnection(EaDbStringParser.eaDbStringToJdbc(this.sourceString));
                this.source.setReadOnly(true);              
            } catch (Exception e) {
                if (logView != null)
                    logView.log(new Status(Status.INFO, symbolicName, "cannot establish connection", e));
            }
}

在这两个插件中,我已经在classpath下的manifest文件中加载了sqljdbc4.jar,并且还加载了dll

清单如下:

  Bundle-ClassPath: .,
  external:$sqljdbc_driver_location$/sqljdbc4.jar
  Bundle-NativeCode: external:$sqljdbc_driver_location$/auth/x86/sqljdbc_auth.dll 

在插件A中它工作正常但在插件B中我得到以下异常

   com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:f4ad6643-4d5e-4cfd-9f9f-585af3707186
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:60)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2229)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.intel.imc.swa.jetdb.mirror.Mirror.run(Mirror.java:163)
at com.intel.imc.swa.featuremodel.swamodel.mirror.MirrorFeatureModel.run(MirrorFeatureModel.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: java.lang.UnsatisfiedLinkError: Native Library C:\sqljdbc_4.0\enu\auth\x86\sqljdbc_auth.dll already loaded in another classloader
at java.lang.ClassLoader.loadLibrary1(Unknown Source)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:35)
... 14 more

修改

在我的Eclipse安装下我有

C:/users/uiqbal/Desktop/Eclispe/sqljdbc_auth.dll 

dll存在于此位置以及

C:/sqldriver/enu/auth/x86/sqljdbc_auth.dll

如果我从我的eclipse安装中删除了dll,那么当我第一次尝试建立连接时,它会失败,抱怨在java类路径下没有找到dll。

我创建了一个新的插件,并在清单中添加了

 Bundle-NativeCode: external:$sqljdbc_driver_location$/auth/x86/sqljdbc_auth.dll

哪里

  $sqljdbc_driver_location = C:/sqldriver/enu

当我尝试使用

重新建立连接时
 DriverManager.getConnection(EaDbStringParser.eaDbStringToJdbc(this.sourceString));

我收到以下错误

 WARNING: Failed to load the sqljdbc_auth.dll cause : Native Library C:\Users\uiqbal\Desktop\FM_Eclipse\sqljdbc_auth.dll already loaded in another classloader

我知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

VM只能加载DLL,无论它是否作为OSGi / RCP运行。

您需要将DLL和sqljdbc4.jar的加载分成第三个插件C,然后A和B都依赖它。