java.lang.IllegalArgumentException: interface com.mysql.jdbc.JDBC4MySQLConnection is not visible from class loader

Exception in thread "main" java.lang.ExceptionInInitializerError
at szq.main.hbasetohive.HBaseReadTest.main(HBaseReadTest.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:894)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:198)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:228)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:137)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.IllegalArgumentException: interface com.mysql.jdbc.JDBC4MySQLConnection is not visible from class loader

这个错误是连接mysql时候出现的
主要问题: 在这里: conn.getClass().getInterfaces() 底下连接代码已经改正了正确方式
/**
* 获取数据库连接
*/
public Connection getConnection() throws SQLException {
if(connPool.size() > 0){
//从集合中获取一个连接
final Connection conn = connPool.removeFirst();
//返回Connection的代理对象
//这是错误的方式,获取接口的问题 conn.getClass().getInterfaces()
// (Connection) Proxy.newProxyInstance(ConnPool.class.getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() {
//正确的方式 采用new Class[]{com.mysql.jdbc.Connection.class}的方式
return (Connection) Proxy.newProxyInstance(ConnPool.class.getClassLoader(), new Class[]{com.mysql.jdbc.Connection.class}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(!"close".equals(method.getName())){
return method.invoke(conn, args);
}else{
connPool.add(conn);
System.out.println("池中连接数为 " + connPool.size());
return null;
}
}
});
}else{
throw new RuntimeException("数据库繁忙,稍后再试");
}
}

posted @ 2021-08-11 11:16  堕落先锋  阅读(160)  评论(0编辑  收藏  举报