Error using lib commons-dbcp2 (BasicDataSource class) in multi module java project

huangapple go评论71阅读模式
英文:

Error using lib commons-dbcp2 (BasicDataSource class) in multi module java project

问题

我想使用BasicDataSource进行数据库互联。在“经典”Java项目(没有模块)中,一切都正常工作,但当我创建多模块项目时,编译时发生了一些错误。

错误:(...) java: 无法访问javax.management.MBeanRegistration

我以前从未遇到过这种错误。有趣的是,当我尝试调用BasicDataSource类的任何方法时出现错误。

Class.forName("com.mysql.cj.jdbc.Driver");
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUsername("username"); // 错误发生在这里

库的文档说:

DBCP 2.7.0 只能在 Java 8 下编译和运行(JDBC 4.2)

但是:

DBCP 2.7.0 二进制文件应该由在 Java 8 及以上版本上运行的应用程序使用。

参考链接:https://commons.apache.org/proper/commons-dbcp/

module-info.java:

module BusinessLogic {
    requires mysql.connector.java;
    requires java.sql;
    requires com.fasterxml.jackson.databind;
    
    requires commons.dbcp2;
    requires commons.pool2;
    requires commons.logging;
}

因此,我尝试使用二进制文件,但没有帮助,我该如何避免这个问题?

英文:

I want to use BasicDataSource for DB interconnection. In the "classic" java project (without modules) everything works correctly, but when i create multi module project some error occured at the compile time.

Error:(..) java: cannot access javax.management.MBeanRegistration

I have never met such errors before. Intersting then error occurs when i try to call any method of the BasicDataSource class

    Class.forName("com.mysql.cj.jdbc.Driver");
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUsername("username"); //Error occurs here 

Docs for lib says that:

> DBCP 2.7.0 compiles and runs under Java 8 only (JDBC 4.2)

But:

> DBCP 2.7.0 binaries should be used by applications running on Java 8 and above.

Ref: https://commons.apache.org/proper/commons-dbcp/

module-info.java:

module BusinessLogic {
    requires mysql.connector.java;
    requires java.sql;
    requires com.fasterxml.jackson.databind;

   requires commons.dbcp2;
   requires commons.pool2;
   requires commons.logging;
}

So, i tried to use binaries but it didn't help, how can i avoid this problem?

答案1

得分: 1

Solution: 需要在 module-info 中指定另一个依赖项:

requires java.management;

或者删除 module-info。

module-info.java:

module DBModule{
requires mysql.connector.java;
requires java.sql;
requires commons.dbcp2;
requires java.management;
}

英文:

Solution: It's necessary to specify one more dependency in module-info:

requires java.management;

or delete module-info.

module-info.java:

    module DBModule{
        requires mysql.connector.java;
        requires java.sql;
        requires commons.dbcp2;
        requires java.management;
    }

huangapple
  • 本文由 发表于 2020年8月2日 17:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定