黏贴主要代码
刚开始想着是 本地使用的是 mysql8的jar 包 ,可能和 doris 有冲突索引手动引入java包
public static Connection getUnPooledConnection_Doris(DataSourceDto dataSource) throws SQLException {
String jarFilePath =dataSource.getDictPath();
String className = "com.mysql.jdbc.Driver";
try (URLClassLoader loader = new URLClassLoader(new URL[]{new URL(jarFilePath)})) {
Class<?> driverClass = Class.forName(className, true, loader);
Driver driver = (Driver) driverClass.newInstance();
Properties info = new Properties();
info.put("user", dataSource.getUsername());
info.put("password", dataSource.getPassword());
Connection connection = driver.connect(dataSource.getJdbcUrl(), info);
if (connection != null) {
LOGGER.info("Connected to database version: {}", connection.getMetaData().getDatabaseProductVersion());
LOGGER.info("Using JDBC driver version: {}", connection.getMetaData().getDriverVersion());
}
return connection;
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) {
throw new SQLException("Failed to load JDBC driver or establish a connection", e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
但是线上运行发现,内存一直在增加,手动引入的jar不释放,尝试很多办法依旧没见成效
偶然发现
可以class 给 搞出来 简单还省事
public Connection getUnPooledConnection_Doris(DataSourceDto dataSource) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException, MalformedURLException {
String className = "com.mysql.jdbc.Driver"; // MySQL 5.x 的驱动程序类名,MySQL 8.x 使用另一个类名
Class<?> CA = Class.forName(className);
Driver driver = (Driver) CA.newInstance();
Connection connection = null;
Properties info = new Properties();
info.put("user", dataSource.getUsername());
info.put("password", dataSource.getPassword());
connection = driver.connect(dataSource.getJdbcUrl(), info);
System.out.println(connection.getMetaData().getDatabaseProductVersion());
System.out.println(connection.getMetaData().getDriverVersion());
return connection;
}
记录下-学艺不精,还得加油文章来源:https://www.toymoban.com/news/detail-855636.html
当时参考网上大佬文章 大佬代码如下文章来源地址https://www.toymoban.com/news/detail-855636.html
import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
public class test {
public static void main(String[] args) {
Connection pooledConnection = null;
try{
Thread.sleep(10000);
int count = 0;
while (true) {
System.out.println("start");
String dir = "jar:file:D:/jar/mysql-connector-java-5.1.47.jar!/";
URL url = new URL(dir);
URL[] urls2 = {url};
MyUrlClassLoader myUrlClassLoader = new MyUrlClassLoader(urls2);
Class<?> CA = myUrlClassLoader.loadClass("com.mysql.jdbc.Driver");
Driver driver = (Driver) CA.newInstance();
Properties info = new Properties();
info.put("user", "root");
info.put("password", "xxx");
Connection connection = driver.connect("jdbc:mysql://xxxx:9030/scv", info);
System.out.println("--------------------"+connection.getMetaData().getDatabaseProductVersion());
System.out.println("--------------------"+connection.getMetaData().getDriverVersion());
pooledConnection = connection;
PreparedStatement statement = pooledConnection.prepareStatement(" select * from wms_inbound");
ResultSet rs = statement.executeQuery();
int columnCount = rs.getMetaData().getColumnCount();
List<String> columns = new ArrayList<>();
for (int i = 1; i <= columnCount; i++) {
String columnName = rs.getMetaData().getColumnLabel(i);
columns.add(columnName);
}
List<JSONObject> list = new ArrayList<>();
while (rs.next()) {
JSONObject jo = new JSONObject();
columns.forEach(t -> {
try {
Object value = rs.getObject(t);
//数据类型转换
Object result = dealResult(value);
jo.put(t, result);
} catch (SQLException throwable) {
// log.error("error",throwable);
throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage());
}
});
list.add(jo);
}
System.out.println(list.toString());
System.out.println("done");
// Thread.sleep(5000);
myUrlClassLoader = null;
System.out.println("release");
System.gc();
++ count;
if(count > 5)
break;
}
System.out.println("alldone");
} catch (Exception e) {
System.out.println("found exception");
e.printStackTrace();
} catch (Throwable throwable) {
System.out.println("found throwable");
throwable.printStackTrace();
}
}
private static Object dealResult(Object result) throws SQLException {
if (null == result) {
return result;
}
String type = result.getClass().getName();
if ("oracle.sql.TIMESTAMP".equals(type)) {
//oracle.sql.TIMESTAMP处理逻辑
return new Date((Long) JSONObject.toJSON(result));
}
return result;
}
private static class MyUrlClassLoader extends URLClassLoader {
public MyUrlClassLoader(URL[] urls) {
super(urls);
}
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
return super.findClass(name);
}
@Override
protected void finalize() throws Throwable {
System.out.println("回收");
super.finalize();
}
}
}
到了这里,关于记录 动态代理doris的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!