代码拉取完成,页面将自动刷新
【标题描述】: org.postgresql.core.v3.ConnectionFactoryImpl#CLIENT_ENCODING 字符集变量非线程安全导致多字符集数据库连接并发导致数据乱码
【测试类型:接口功能】
【测试版本:MASTER】
问题描述:
org.postgresql.core.v3.ConnectionFactoryImpl#CLIENT_ENCODING 字符集变量非线程安全导致多字符集数据库连接并发导致数据乱码
【原因分析】:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
优化前版本:compiled at 2023-02-23-00:44:15 build 803aed5
优化后版本:compiled at 2023-09-13-09:44:22 build 5ec362a
验收结论:通过
优化前:
优化后:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.opengauss.Driver;
public class JDBC_Connection_Case0001 {
public static void main(String[] args) throws SQLException {
System.out.println(Driver.getGSVersion());
if (args.length < 4) {
System.out.print("need para: host:ip,db_b,user,password");
return;
}
String url1 = "jdbc:opengauss://" + args[0] + "/" + args[1]
+ "?loggerLevel=off&allowEncodingChanges=true&characterEncoding=" + args[4];
String url2 = "jdbc:opengauss://" + args[0] + "/" + args[1]
+ "?LoggerLevel=off&allowEncodingChanges=true&characterEncoding=" + args[5];
String t_name1 = "t_jdbc_connection_01_1";
String t_name2 = "t_jdbc_connection_01_2";
String create_sql1 = "drop table if exists " + t_name1 + "; create table " + t_name1
+ "(id varchar(1000), name text);";
String create_sql2 = "drop table if exists " + t_name2 + "; create table " + t_name2
+ "(id varchar(1000), name text);";
System.out.println("----新建连接1----");
Connection conn1 = GetConnection(args[2], args[3], url1);
Statement statement1 = conn1.createStatement();
System.out.println("----连接1插入数据----");
statement1.executeUpdate(create_sql1);
String sql1 = "insert into " + t_name1 + "(id,name) values (?,?)";
PreparedStatement preparedStatement = conn1.prepareStatement(sql1);
preparedStatement.setString(1, "只有一个connection1,connection1再次插入数据");
preparedStatement.setString(2, "%%¥¥##");
preparedStatement.execute();
System.out.println("----新建连接2----");
Connection conn2 = GetConnection(args[2], args[3], url2);
Statement statement2 = conn2.createStatement();
System.out.println("----连接1再次插入数据----");
preparedStatement = conn1.prepareStatement(sql1);
preparedStatement.setString(1, "新建connection2后,connection1再次插入数据");
preparedStatement.setString(2, "%%¥¥##");
preparedStatement.execute();
System.out.println("----连接1查询数据----");
String select_sql1 = "select * from " + t_name1;
try (ResultSet resultSet = statement1.executeQuery(select_sql1)) {
while (resultSet.next()) {
System.out.print("connection1: col1 = " + resultSet.getObject(1).toString());
System.out.println(" connection1: col2 = " + resultSet.getObject(2));
}
}
System.out.println("----连接2插入数据,查询数据----");
statement2.executeUpdate(create_sql2);
String sql2 = "insert into " + t_name2 + "(id,name) values (?,?)";
preparedStatement = conn2.prepareStatement(sql2);
preparedStatement.setString(1, "连接2插入数据");
preparedStatement.setString(2, "……99*%#%");
preparedStatement.execute();
String select_sql2 = "select * from " + t_name2;
try (ResultSet resultSet = statement2.executeQuery(select_sql2)) {
while (resultSet.next()) {
System.out.print("connection2: col1 = " + resultSet.getObject(1).toString());
System.out.println(" connection2: col2 = " + resultSet.getObject(2));
}
}
System.out.println("----删除表----");
String drop_sql = "drop table if exists " + t_name1 + "drop table if exists " + t_name2;
statement2.executeUpdate(drop_sql);
statement1.close();
statement2.close();
conn1.close();
conn2.close();
}
public static Connection GetConnection(String username, String passwd, String sourceURL) {
Connection conn = null;
try {
conn = DriverManager.getConnection(sourceURL, username, passwd);
System.out.println(sourceURL);
System.out.println("Connection succeed");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return conn;
}
}
验收日期:2023-10-19
验收版本:compiled at 2023-10-19-10:19:59 build 6061008
验收结论:通过
登录 后才可以发表评论